小白学习将maven项目部署到nexus私服

708 阅读3分钟

一、创建Nexsus的Maven私服

参考小白搭建Nexus的Maven私服

二、Maven配置使用Nexus私服

Maven配置私服下载依赖方式

maven配置私服下载有两种方式

  • setting.xml:该文件配置的是全局模式
  • pom.xml:该文件的配置的是项目独享模式

若pom.xml和setting.xml同时配置了,以pom.xml为准

setting.xml文件配置

位置:D:\backend\apache-maven-3.8.3\conf\setting.xml

这个时候不需要再配置pom.xml文件,即可使用私服下载jar依赖包

配置私服镜像
<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <!-- <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror> -->

    <mirror>
      <!--该镜像的唯一标识符。id用来区分不同的mirror元素。 -->
      <id>nexus-releases</id>
      <!--*指的是访问任何仓库都使用我们的私服-->
      <mirrorOf>*</mirrorOf>    
      <!--该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。 -->
      <url>http://192.168.57.128:9081/repository/maven-public/</url>     
    </mirror>    
    <mirror>     
      <id>nexus-snapshots</id>     
      <mirrorOf>*</mirrorOf>     
      <url>http://192.168.57.128:9081/repository/maven-snapshots/</url>     
    </mirror>
  </mirrors>

镜像的URL可以从页面中的copy按钮直接复制

image.png

配置从私服下载jar包
  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
    <profile>
        <!--profile的id-->
        <id>nexus</id>
        <repositories>
            <repository>
                <!--仓库id,repositories可以配置多个仓库,保证id不重复-->
                <id>nexus-releases</id>
                <!--仓库地址,即nexus仓库组的地址-->
                <url>http://nexus-releases</url>
                <releases>
                    <!--是否下载releases构件-->
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>nexus-snapshots</id>
                <url>http://nexus-snapshots</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
            <pluginRepository>
                <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
                <id>nexus-releases</id>
                <url>http://nexus-releases</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>nexus-snapshots</id>
                <url>http://nexus-snapshots</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
  </profiles>
  
  <!--激活profile--> 
  <activeProfiles>
      <activeProfile>nexus</activeProfile>
  </activeProfiles>

pom.xml文件配置

如果你配置了pom.xml,则以pom.xml为准

<repositories>
	<repository>
		<id>maven-nexus</id>
		<name>maven-nexus</name>
		<url>http://10.172.0.201:8081/repository/maven-public/</url>
		<releases>
			<enabled>true</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</repository>
</repositories>

三、配置Maven连接私服打包上传项目

第一步,修改setting.xml文件,指定releases和snapshots server的用户名和密码

<servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
    <!--我本地虚拟服务器搭建的Nexus的登陆账户是admin,下面的account就填admin,password就是你用admin登录Nexus的密码-->
    <server>
        <id>releases</id>
        <username>account</username>
        <password>password</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>account</username>
        <password>password</password>
    </server>
  </servers>

第二步,在项目的pom.xml文件中加入distributionManagement节点

注意:repository里的id需要和第一步里的server id名称保持一致

<!-- 配置发布到个人Nexus私服 -->
<distributionManagement>
  <repository>
    <id>releases</id>
    <name>releases</name>
    <url>http://192.168.57.128:9081/repository/personal-maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>snapshots</id>
    <name>snapshots</name>
    <url>http://192.168.57.128:9081/repository/peronsal-maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

这里我自己新建了两个nexus仓库: personal-maven-releasesperonsal-maven-snapshots

创建步骤(根据默认的maven-releases和maven-snapshots,发现两者仓库类型都属于hosted):

1、创建personal-maven-releases:点击create repositories,选择maven2(hosted),取名personal-maven-releases,Version policy选择Release,Deployment policy选择Allow redeploy,Blob store选择defalut,最后保存。

image.png

image.png

image.png

2、创建personal-maven-snapshots:点击create repositories,选择maven2(hosted),取名personal-maven-snapshots,Version policy选择Snapshot,Deployment policy选择Allow redeploy,Blob store选择defalut,最后保存。

image.png

image.png

image.png

第三步,执行发布

这里需要注意一下:如果你的项目里面有@Test测试代码,要运行的命令是:mvn clean deploy -DskipTests

mvn clean deploy -DskipTests

登录Nexus,查看对应的仓库已经有相关的依赖包了。

注意以下几点:

  • 若项目版本号末尾带有 -SNAPSHOT,则会发布到snapshots快照版本仓库
  • 若项目版本号末尾带有 -RELEASES 或什么都不带,则会发布到releases正式版本仓库

上传第三方jar包

待续

常见问题

status: 401 Unauthorized

原因:

  • pom.xml中distributionManagementrepository的id和setting.xml中server的id不一致
  • server的账号密码权限不够

status: 400 Repository version policy: RELEASE does not allow version: 1.0-20211205.034457-1

原因:

  • 仓库的Deployment policy未设置成Allow redeploy
  • 仓库的类型和pom.xml中的<version>1.0-SNAPSHOT</version>类型不一致,这里是SNAPSHOT,所以仓库类型应该设置成Snapshot