Linux服务器安装Nexus构建maven私有仓库(maven私服)

一,下载nexus(linux版本)

下载nexus(maven管理库),选择linux版本进行下载(,下载好之后也上传到/usr/local/nexus目录下。

地址:http://www.sonatype.org/nexus/go/

使用wget命令下载 wget 想要下载nexus版本的地址

wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar.gz

二,安装配置nexus

解压nexus压缩包到当前文件夹

nexus-2.11.4-01-bundle.tar.gz

生成了两个目录(一个 nexus 服务,一个私有库目录)

[root@iZ2ze9ipfjhle3cz3cgb0rZ nexus]# ls
nexus-2.11.4-01 nexus-2.11.4-01-bundle.tar.gz sonatype-work

配置访问nexus端口

在nexus.properties文件中(该文件位于nexus-2.11.2-03/conf/),默认为8081端口

切换到目录:/nexus/nexus-2.11.2-03/bin/下,打开文件nexus(vim nexus)

NEXUS_HOME=".."
改为(不修改默认也可以):
NEXUS_HOME="nexus安装目录"
RUN_AS_USER=
改为:
RUN_AS_USER=root

切换到/usr/local/nexus/nexus-2.11.2-03/bin/目录下,启动nexus

./nexux start

回到浏览器,访问 http://101.201.101.200:8081/nexus/,就进入到了maven管理器。
登录,默认用户名 admin 默认密码 admin123,到此安装配置结束。

三,添加jar包到maven管理器

首先添加一个用户

给用户添加一些权限

Respositories3rd party中添加jar包和填写依赖信息。

注意:以后添加的第三方jar包都是上传到3rd party这个仓库的!

注意下面

添加成功!

成功之后,在左上角进行搜索,可以搜到上传的jar包信息,把右边的依赖拷贝到pom.xml文件中,该jar包就可以使用了。

这一点不要忘记,把Central仓库的Configuration配置中,Download Remote Indexes项改为true

四,新建maven项目,进行测试

首先在pom.xml文件中加入指定私服仓库的地址的配置。

url标签的地址/nexus/content/groups/public/就是这样子搞的!

 <repositories>
      <repository>
            <id>nexus</id>
            <name>nexus</name>
            <url>http://101.201.101.200:8081/nexus/content/groups/public/</url>
             <releases>
                 <enabled>true</enabled>
             </releases>
             <snapshots>
                 <enabled>true</enabled>
             </snapshots>
      </repository>
 </repositories>

然后再添加刚才的jar包对应的依赖,你会发现已经下载了对应的jar包并且可以使用了。

   <dependency>
        <groupId>com.test.fantongxue</groupId>
        <artifactId>ftx-test</artifactId>
        <version>1.0.0</version>
   </dependency>

写测试方法进行测试,TestUtil工具类是可以使用的。

 public static void main(String[] args) {
        TestUtil testUtil=new TestUtil();
        Integer add = testUtil.add(1, 2);
        System.out.println(add);
    }

效果如下图

五,maven私服仓库的扩展

下面是对maven私服仓库的扩展!

项目使用nexus私服的插件,在项目的pom.xml文件中指定插件仓库

<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>nexus</name>
        <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

如果想本机所有的maven项目都使用私服的组件,可以在maven的设置文件settings.xml中添加属性,并激活

<profiles>
    <profile>
        <id>nexusProfile</id>
        <repositories>
            <repository>
                <id>nexus</id>
                <name>nexus</name>
                <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>
</profiles>
<!-- 激活 -->
<activeProfiles>
    <activeProfile>nexusProfile</activeProfile>
</activeProfiles>

项目发布到私服,maven项目使用命令:mvn clean deploy;需要在pom文件中配置一下代码;

<distributionManagement>
        <repository>
            <id>user-release</id>
            <name>User Project Release</name>
            <url>http://192.168.1.103:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>user-snapshots</id>
            <name>User Project SNAPSHOTS</name>
            <url>http://192.168.1.103:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
</distributionManagement>

了解maven全局配置文件settings.xml可以参考这篇帖子!

猜你喜欢

转载自www.cnblogs.com/fantongxue/p/12686821.html