Java项⽬打包发布到maven私仓常见的⼏种⽅式
⽬录
前⾔
发布到maven私仓的步骤
1.在maven的l中< servers >节点配置⽤户名和密码,形如下:
2、指定发布到nexus私仓的url并进⾏发布
⽅式⼀:l⽂件添加distributionManagement节点
⽅式⼆:在maven的l中< profiles >节点配置< properties >,并在< properties >指定<altSnapshotDeploymentRepository > 和<
altReleaseDeploymentRepository >
⽅式三:通过mvn deploy指定参数
⽅式四:通过nexus的可视化界⾯进⾏上传jar发布
这⼏种发布⽅式的选择
前⾔
在早期没有使⽤maven之前,我们引⽤⼀些公有jar或者api jar,我们可能会采⽤这样的⽅式,通过⼿动导⼊这些jar到项⽬的classpath路径进⾏引⽤。
有了maven后,我们公司内部可能就会搭建maven私仓⽐如nexus,然后把这些公有jar或者api jar上传到nexus私仓,在l配置⼀下这些jar的坐标就可以引⽤。
今天我们的话题就是来聊聊项⽬打包发布到maven私仓常见的⼏种⽅式
发布到maven私仓的步骤
1.在maven的l中< servers >节点配置⽤户名和密码,形如下:
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
java开发可视化界面
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
注:其中id可先看做是⼀个标识。username和password为nexus私仓的⽤户名和密码
2、指定发布到nexus私仓的url并进⾏发布
⽅式⼀:l⽂件添加distributionManagement节点
形如下:
<distributionManagement>
<!--正式版本-->
<repository>
<!-- 在l中<server>的id-->
<id>nexus-releases</id>
<url>192.168.0.11:8081/nexus/content/repositories/releases/</url>
</repository>
<!--快照版本-->
<snapshotRepository>
<id>nexus-snapshots</id>
<url>192.168.0.11:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注:
如果存在parent,只需在parent中的l中配置,没有则在本项⽬的l配置即可
< repository >节点下的< id >对应maven的配置⽂件l⽂件中的server的id,两者必须保持⼀致
上传到私仓的是正式版本还是快照版本,取决于l⽂件version中是SNAPSHOT还是RELEASE。⽐如你项⽬中配置如下
<groupId&le</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
则上传到私仓的就是快照版本
最后执⾏maven的deploy命令进⾏发布
⽅式⼆:在maven的l中< profiles >节点配置< properties >,并在< properties >指定<altSnapshotDeploymentRepository > 和< altReleaseDeploymentRepository >
形如下:
<profiles>
<profile>
<id>nexus</id>
<properties>
<altSnapshotDeploymentRepository>
nexus-snapshots::default::192.168.0.11:8081/repository/maven-snapshots/
</altSnapshotDeploymentRepository>
<altReleaseDeploymentRepository>
nexus-releases::default::192.168.0.11:8081/repository/maven-releases/
</altReleaseDeploymentRepository>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
注:
1. nexus-snapshots和 nexus-releases要和maven的配置⽂件l⽂件中的server的id,两者必须保持⼀致
2. 属性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::YOUR_NEXUS_URL/snapshots
-DaltReleaseDeploymentRepository=nexus-releases::default::YOUR_NEXUS_URL/releases
同理上述命令要执⾏成功,得确保deploy插件是基于2.8版本以上
⽅法⼆:通过-D指定要发布的jar的相关信息以及私仓地址,私仓id,私仓id要和l⽂件中的server的id保持⼀致
形如下
mvn deploy:deploy-file -DskipTests -Dfile=jar包⽂件地址,绝对路径 -DgroupId=组名 -DartifactId=项⽬名称 -Dversion=版本号 -Dpackaging=jar -DrepositoryId=私库id(和l⽂件中的server的id保持⼀致) -Durl=私仓地址⽅式四:通过nexus的可视化界⾯进⾏上传jar发布
这⼏种发布⽅式的选择
⽅式⼀,通过distributionManagement这种⽅式发布,可能是⼤多数⼈的选择。但如果要发布的项⽬很多,我们就可以考虑使⽤⽅式⼆,通过在全局的settings⽂件配置altSnapshotDeploymentRepository 和altReleaseDeploymentRepository进⾏发布,只需配置⼀次,所有项⽬就都可以发布,⽆需在多个项⽬pom指定
⽅式⼀和⽅式⼆⽐较适合公司⾃⼰内部开发项⽬,对于⼀些第三⽅提供的jar,推荐使⽤mvn deploy -DrepositoryId=私库id(和l⽂件中的server的id保持⼀致) -Durl=私仓地址的⽅式或者直接使⽤nexus可视化界⾯上传的⽅式
以上就是项⽬打包发布到maven私仓常见的⼏种⽅式的详细内容,更多关于项⽬打包发布到maven的资料请关注其它相关⽂章!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。