简要记录搭建Nexus私服过程(发布和使用)

上一篇文章(简要记录搭建Nexus私服过程(配置))记录了配置nexus仓库过程,本文主要记录发布代码和使用。

项目结构:

方便起见,测试例子用了一个父子结构,其中maven-demo是父模块,maven-deploy和maven-pull是子模块。下面以snapshot的发布为例,记录发布与使用。

1. maven-demo的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>maven-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-demo</name>
    <description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>

    <modules>
        <module>maven-deploy</module>
        <module>maven-pull</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--  deploy地址  -->
    <distributionManagement>
        <!--  Release,id要和settings.xml中匹配  -->
        <repository>
            <id>mingo-releases</id>
            <url>http://172.19.10.17:7001/repository/mingo-releases/</url>
        </repository>
        <!--  Snapshot,id要和settings.xml中匹配 -->
        <snapshotRepository>
            <id>mingo-snapshots</id>
            <url>http://172.19.10.17:7001/repository/mingo-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <build>
        <plugins>
            <!--  编译插件  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
        </plugins>
    </build>

</project>

2. maven-deploy的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>maven-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example.mavendeploy</groupId>
    <artifactId>maven-deploy</artifactId>
    <!--  含-SNAPSHOT时表示是Snapshot版本, 不含时会识别为Release版本  -->
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-deploy</name>
    <description>Demo project for Spring Boot</description>

    <!--
    父模块中的<distributionManagement>和<build>标签会被子模块继承
    -->
</project>

3. 配置settings.xml

可以是安装目录confsettings.xml,也可以是用户目录.m2settings.xml,只是相同的内容后者会覆盖前者,作用的范围大小区别。将Nexus私服账户和密码保存在settings.xml中,其中<server>下相关<id>字段要与pom.xml中<distributionManagement><id>匹配。添加下图方框中的内容

4. 发布maven-deploy

写了一个测试类,里面定义了一个常量

下面是发布过程的信息

[INFO] ------------------------------------------------------------------------
[INFO] Building maven-deploy 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ maven-deploy ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory K:java-projectmaven-demomaven-deploysrcmain
esources
[INFO] skip non existing resourceDirectory K:java-projectmaven-demomaven-deploysrcmain
esources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-deploy ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to K:java-projectmaven-demomaven-deploy	argetclasses
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ maven-deploy ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory K:java-projectmaven-demomaven-deploysrc	est
esources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-deploy ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ maven-deploy ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ maven-deploy ---
[INFO] Building jar: K:java-projectmaven-demomaven-deploy	argetmaven-deploy-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ maven-deploy ---
[INFO] Installing K:java-projectmaven-demomaven-deploy	argetmaven-deploy-0.0.1-SNAPSHOT.jar to C:Usersdoflamingo.m2
epositorycomexamplemavendeploymaven-deploy.0.1-SNAPSHOTmaven-deploy-0.0.1-SNAPSHOT.jar
[INFO] Installing K:java-projectmaven-demomaven-deploypom.xml to C:Usersdoflamingo.m2
epositorycomexamplemavendeploymaven-deploy.0.1-SNAPSHOTmaven-deploy-0.0.1-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ maven-deploy ---
Downloading: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/0.0.1-SNAPSHOT/maven-metadata.xml
Uploading: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/0.0.1-SNAPSHOT/maven-deploy-0.0.1-20200613.121600-1.jar
Uploaded: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/0.0.1-SNAPSHOT/maven-deploy-0.0.1-20200613.121600-1.jar (3 KB at 13.0 KB/sec)
Uploading: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/0.0.1-SNAPSHOT/maven-deploy-0.0.1-20200613.121600-1.pom
Uploaded: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/0.0.1-SNAPSHOT/maven-deploy-0.0.1-20200613.121600-1.pom (674 B at 5.7 KB/sec)
Downloading: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/maven-metadata.xml
Uploading: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/0.0.1-SNAPSHOT/maven-metadata.xml (785 B at 5.3 KB/sec)
Uploading: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/maven-metadata.xml
Uploaded: http://172.19.10.17:7001/repository/mingo-snapshots/com/example/mavendeploy/maven-deploy/maven-metadata.xml (295 B at 2.7 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看出发布过程除了编译前的相关操作,依次还有package、install的操作。发布的jar包在输出目录、本地仓库和远程仓库中都存在,说明已成功将代码打包发布到私服。

本地仓库文件

远程仓库(mingo-snapshots)

5. 引用刚发布的jar包

将maven-deploy的pom.xml注释掉,并删除本地仓库中刚install的maven-deploy版本。只添加依赖配置是不能定位到包

引用私服仓库的配置方式主要有两种:一是项目pom.xml配置,二是在settings.xml中做全局配置。下面分别介绍下:

5.1 maven-pull的pom.xml添加配置

<!--  只需写mingo-public仓库的地址就行,因为在创建该group的时候,已经包含建立的snapshot、release和proxy仓库  -->
    <repositories>
        <repository>
            <id>mingo-public</id>
            <url>http://172.19.10.17:7001/repository/mingo-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>mingo-public</id>
            <url>http://172.19.10.17:7001/repository/mingo-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

reimport依赖即可下载maven-deploy的jar包

执行测试代码

5.2 settings.xml中做全局配置

 完整的settings.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">
    <pluginGroups></pluginGroups>
    <proxies></proxies>

    <servers>
        <!--  如果只有一个账户就不用配置多个server  -->
        <server>
            <id>mingo-releases</id>
            <username>mingo-user</username>
            <password>5a77f325943b1024</password>
        </server>
        <server>
            <id>mingo-snapshots</id>
            <username>mingo-user</username>
            <password>5a77f325943b1024</password>
        </server>
        <server>
            <id>mingo-public</id>
            <username>mingo-user</username>
            <password>5a77f325943b1024</password>
        </server>
    </servers>

    <profiles>
        <profile>
            <id>mingo-public-1</id>
            <repositories>
                <!--  远程mingo-public仓库  -->
                <repository>
                    <id>mingo-public</id>
                    <name>mingo-public</name>
                    <!--  远程mingo-public仓库地址,已经包含建立的snapshot、release和proxy仓库  -->
                    <url>http://172.19.10.17:7001/repository/mingo-public/</url>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

    </profiles>

    <!--  被激活profile中的repository将作为远程仓库  -->
    <activeProfiles>
        <!--  激活id为mingo-public-1的profile  -->
        <activeProfile>mingo-public-1</activeProfile>
    </activeProfiles>
</settings>

执行结果:

 完。

原创 Doflamingo https://www.cnblogs.com/doflamingo
原文地址:https://www.cnblogs.com/doflamingo/p/13121183.html