Maven私服搭建

时间:2022-07-24
本文章向大家介绍Maven私服搭建,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到这台私服上,这样的话就可以通过这台搭建了nexus私服的电脑访问maven的远程仓库。

1.首先确定我们的环境安装好maven,jdk等必须的环境

2.这些都准备好之后,去下载最新版本的nexus 下载地址:http://www.sonatype.org/nexus/go

我本地安装的是 nexus-2.2-01-bundle,最新的版本是nexus-2.4.0-09-bundle

3.打开目录nexus-2.4.0-09-bundlenexus-2.4.0-09binjsw 这个目录下面你会发现有很多系统版本的nexus环境

我的电脑是win7 (64)为的系统,所以我选择的是windows-x86-64这个版本,当然可以根据个人的电脑系统选择对应的版本

打开一个版本你会看到如下:

我一般都是将nexus安装成windows服务,所以点击install-nexus.bat这个,访问http://localhost:8081/nexus/ 启动后如下页面,在右上角有个Log in 的超链接,点击登录

默认的用户名是 admin 密码是 admin123

登录后你可以在左侧修改登录信息:

4.接下来,我们配置一下maven的代理服务器(前提是你的电脑不能连接外网,如果可以上外网,这里也没有意思,只是介绍一下)

在左侧菜单找到如图:

点击查看右边有很多选项,找到这里

添加你的代理服务器就可以了。

5:接下来,好像这里都不需要怎么配置的,反正我没有用到很多的功能,可能是技术学得不好,不会用,呵呵....

对了,这里还有一个可能需要注意一下的,就是3rd party、Snapshots、Releases这三个,分别用来保存第三方jar(典型的oracle数据库的j驱动包),项目组内部的快照、项目组内部的发布版.

我目前只是用3rd party这个第三方的功能,将maven仓库中没有构件的jar包上传到服务器。如何将第三方的jar上传到nexus上面呢?如下:举例上传oracle的驱动包

那么现在nexus已经安装好,怎么使用上传的jar包吧,很简单的,前提是你已经建立了一个maven的项目。含有pom.xml这个文件,在这个文件中添加如下:

先去服务器看看jar的maven构件

然后在pom.xml中添加如下的本地仓库地址:

 <!-- 配置私服工厂 -->
 
  <repositories>
        <repository>
            <id>nexus</id>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
  </repositories>      
  <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
           </snapshots>
        </pluginRepository>
  </pluginRepositories>

  <!-- 配置发布到私服 -->
  <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://localhost:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
  </distributionManagement>     
  <properties>

到此,完成了nexus私服的搭建,项目组开发人员开发时,只要在项目的pom.xml文件中,添加如下pom.xml信息即可获取私服的jar.

下面是Maven的配置文件Setting.xml的内容变动

<?xml version="1.0" encoding="UTF-8"?>



<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- 第一处改动:设置jar包的存储位置 -->
  <localRepository>J:/Architecture/m2/repository</localRepository>
  <pluginGroups>
  </pluginGroups>

  <proxies>

  </proxies>

  <!--第二处改动:设置私服的发布权限,用户名密码为初始值-->
  <servers>
    <server>
      <id>nexus-releases</id>
      <username>deployment</username>
      <password>deployment123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>deployment</username>
      <password>deployment123</password>
    </server>
  </servers>

 
  <mirrors>

  </mirrors>
  
    <!--第三处改动:设置私服的名称、java版本、url等信息-->
  <profiles>
    <profile>
      <id>nexus-bhz</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.7</jdk>
      </activation>
     <properties>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
     </properties>
     <repositories>
        <repository>
            <id>nexus</id>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
       <pluginRepositories>
            <pluginRepository>
                <id>nexus</id>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
               </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>
<!-- 第四处改动:激活私服 -->
<activeProfiles>
    <activeProfile>nexus-bhz</activeProfile>
</activeProfiles>
</settings>