前言

在晚期没有应用maven之前,咱们援用一些私有jar或者api jar,咱们可能会采纳这样的形式,通过手动导入这些jar到我的项目的classpath门路进行援用。

有了maven后,咱们公司外部可能就会搭建maven私仓比方nexus,而后把这些私有jar或者api jar上传到nexus私仓,在pom.xml配置一下这些jar的坐标就能够援用。

明天咱们的话题就是来聊聊我的项目打包公布到maven私仓常见的几种形式

公布到maven私仓的步骤

1.在maven的settings.xml中< servers >节点配置用户名和明码,形如下:

<servers>    <server>      <id>nexus-releases</id>      <username>admin</username>      <password>admin123</password>    </server>    <server>      <id>nexus-snapshots</id>      <username>admin</username>      <password>admin123</password>    </server>    </servers>

注: 其中id可先看做是一个标识。username和password为nexus私仓的用户名和明码

2、指定公布到nexus私仓的url并进行公布

形式一:pom.xml文件增加distributionManagement节点

形如下:

 <distributionManagement>         <!--正式版本-->        <repository>            <!-- 在settings.xml中<server>的id-->            <id>nexus-releases</id>            <url>http://192.168.0.11:8081/nexus/content/repositories/releases/</url>        </repository>         <!--快照版本-->        <snapshotRepository>             <id>nexus-snapshots</id>             <url>http://192.168.0.11:8081/nexus/content/repositories/snapshots/</url>        </snapshotRepository>    </distributionManagement>

注:

  • 如果存在parent,只需在parent中的pom.xml中配置,没有则在本我的项目的pom.xml配置即可
  • < repository >节点下的< id >对应maven的配置文件settings.xml文件中的server的id,两者必须保持一致
  • 上传到私仓的是正式版本还是快照版本,取决于pom.xml文件version中是SNAPSHOT还是RELEASE。比方你我的项目中配置如下
<groupId>com.example</groupId>    <artifactId>demo</artifactId>    <version>0.0.1-SNAPSHOT</version>

则上传到私仓的就是快照版本

最初执行maven的deploy命令进行公布

形式二:在maven的settings.xml中< profiles >节点配置< properties >,并在< properties >指定 < altSnapshotDeploymentRepository > 和< altReleaseDeploymentRepository >

形如下:

<profiles>     <profile>     <id>nexus</id>     <properties>         <altSnapshotDeploymentRepository>             nexus-snapshots::default::http://192.168.0.11:8081/repository/maven-snapshots/         </altSnapshotDeploymentRepository>         <altReleaseDeploymentRepository>            nexus-releases::default::http://192.168.0.11:8081/repository/maven-releases/         </altReleaseDeploymentRepository>     </properties>    </profile>  </profiles>  <activeProfiles>    <activeProfile>nexus</activeProfile>  </activeProfiles>

注:

  • nexus-snapshots和 nexus-releases要和maven的配置文件settings.xml文件中的server的id,两者必须保持一致
  • 属性altSnapshotDeploymentRepository和altReleaseDeploymentRepository是随maven-release-plugin 2.8版一起引入的。低于2.8版本,执行mvn deploy时,则会报如下谬误
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter

解决方案就是在公布的我的项目中指定一下2.8版本以上的插件,形如下

<build>        <plugins>            <plugin>                <artifactId>maven-deploy-plugin</artifactId>                <version>2.8.2</version>            </plugin>        </plugins>    </build>

最初再执行maven的deploy命令进行公布

形式三:通过mvn deploy指定参数
  • 办法一:通过-D参数指定altSnapshotDeploymentRepository和altReleaseDeploymentRepository

形如下

mvn deploy -DskipTests -DaltSnapshotDeploymentRepository=nexus-snapshots::default::https://YOUR_NEXUS_URL/snapshots-DaltReleaseDeploymentRepository=nexus-releases::default::https://YOUR_NEXUS_URL/releases

同理上述命令要执行胜利,得确保deploy插件是基于2.8版本以上

  • 办法二:通过-D指定要公布的jar的相干信息以及私仓地址,私仓id,私仓id要和settings.xml文件中的server的id保持一致

形如下

mvn deploy:deploy-file -DskipTests -Dfile=jar包文件地址,绝对路径  -DgroupId=组名 -DartifactId=项目名称 -Dversion=版本号 -Dpackaging=jar -DrepositoryId=私库id(和setting.xml文件中的server的id保持一致) -Durl=私仓地址
形式四:通过nexus的可视化界面进行上传jar公布

如下图

这几种公布形式的抉择

形式一,通过distributionManagement这种形式公布,可能是大多数人的抉择。但如果要公布的我的项目很多,咱们就能够思考应用形式二,通过在全局的settings文件配置altSnapshotDeploymentRepository 和altReleaseDeploymentRepository进行公布,只需配置一次,所有我的项目就都能够公布,无需在多个我的项目pom指定

形式一和形式二比拟适宜公司本人外部开发我的项目,对于一些第三方提供的jar,举荐应用mvn deploy -DrepositoryId=私库id(和settings.xml文件中的server的id保持一致) -Durl=私仓地址的形式或者间接应用nexus可视化界面上传的形式