好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java项目打包发布到maven私仓常见的几种方式

前言

在早期没有使用maven之前,我们引用一些公有jar或者api jar,我们可能会采用这样的方式,通过手动导入这些jar到项目的classpath路径进行引用。

有了maven后,我们公司内部可能就会搭建maven私仓比如nexus,然后把这些公有jar或者api jar上传到nexus私仓,在pom.xml配置一下这些jar的坐标就可以引用。

今天我们的话题就是来聊聊项目打包发布到maven私仓常见的几种方式

发布到maven私仓的步骤

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

?

1

2

3

4

5

6

7

8

9

10

11

12

< 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节点

形如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

< 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。比如你项目中配置如下

?

1

2

3

< groupId >com.example</ groupId >

  < artifactId >demo</ artifactId >

  < version >0.0.1-SNAPSHOT</ version >

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

最后执行maven的deploy命令进行发布

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

形如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

< 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时,则会报如下错误

?

1

Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter

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

?

1

2

3

4

5

6

7

8

< build >

   < plugins >

    < plugin >

     < artifactId >maven-deploy-plugin</ artifactId >

     < version >2.8.2</ version >

    </ plugin >

   </ plugins >

  </ build >

最后再执行maven的deploy命令进行发布

方式三:通过mvn deploy指定参数

方法一:通过-D参数指定altSnapshotDeploymentRepository和altReleaseDeploymentRepository
形如下

?

1

2

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保持一致
形如下

?

1

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可视化界面上传的方式

以上就是项目打包发布到maven私仓常见的几种方式的详细内容,更多关于项目打包发布到maven的资料请关注其它相关文章!

原文链接:https://segmentfault测试数据/a/1190000039383875

查看更多关于Java项目打包发布到maven私仓常见的几种方式的详细内容...

  阅读:45次