maven打包部署到私服

时间:2023-03-09 15:36:45
maven打包部署到私服

转载地址:http://blog.****.net/stormragewang/article/details/43407471

心得

apache的开源maven插件对我们使用maven进行打包,发布的流程简化了不少。开源插件帮我们减少了了解详细流程的工作量,那么同时肯定会带来熟悉各个插件默认配置的麻烦,以下是最近使用maven插件的一些心得。
  • 我们在创建工程是的命名最好遵从maven工程的习惯,尤其是artifactId。虽然你后面可以修改配置文件来修改打包后的jar或war包名,但是使用maven-delpoy-plugin插件时,默认上传的就是artifactId开头的,目前我还没有找到好的解决办法。
  • 网上有介绍使用maven-war-plugin的overlay属性来合并多个web工程,但是overlay只是合并资源文件,并不能合并web.xml,参考http://java.dzone.com/articles/mavens-war-overlay-what-are。我后来找到了cargo-maven2-plugin插件,其介绍在http://cargo.codehaus.org/Merging+WAR+files
  • 一般常用的插件有:发布jar到私有库,会用到maven-deploy插件;同时发布源码会用到maven-source-plugin插件;合并多个web工程会用到cargo-maven-plugin插件。

maven-deploy-plugin插件

1、首先在nexus私有库中创建一个可以上传jar包的用户。我先创建了一个角色,再创建一个上传的用户,这样其他项目组的成员可以重用这个角色的配置。
maven打包部署到私服
创建用户
maven打包部署到私服
2、在个人maven配置文件中添加该用户的信息
maven打包部署到私服
setting.xml
maven打包部署到私服
3、在maven工程的pom.xml配置部署的仓库,注意pom.xml和setting.xml中的id属性要一致
 <distributionManagement>
<repository>
<id>packaging-releases</id>
<name>Packaging Release Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/packaging-release/</url>
</repository>
<snapshotRepository>
<id>packaging-snapshots</id>
<name>Packaging Snapshot Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/packaging-snapshot/</url>
</snapshotRepository>
</distributionManagement>

这样就可以将自己的jar包部署到私有库中了,将maven工程的SNAPSHO后缀删除后自动发布到Release属性的仓库,之前默认的是Snapshot属性的仓库。
maven打包部署到私服

maven-source-plugin插件

要想在部署jar包到私有库的同时上传源码包,修改一下maven工程的pom.xml文件就可以,添加以下内容

     <build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
再部署时,就会自动上传源码包了
maven打包部署到私服

cargo-maven2-plugin插件

当我们需要和并多个war包时,使用cargo-maven2-plugin可以自动帮我们合并web.xml
1、添加cargo-maven2-plugin插件,其中descriptor指向配置文件
         <plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.1.3</version>
<extensions>true</extensions>
<configuration>
<descriptor>src/assemble/merge.xml</descriptor>
</configuration>
</plugin>
</plugins>
2、配置文件src/assemble/merge.xml配置如下
 <?xml version="1.0" encoding="UTF-8"?>
<uberwar>
<wars>
<war>com.hust.wnlo.dsis.ipackaging:user.web</war>
<war>com.hust.wnlo.dsis.ipackaging:commodity.web</war>
</wars>
<webXml>
<contextParams>
<strategy name="ChooseByName">
<default>
<strategy name="Preserve" />
</default>
<choice name="contextConfigLocation">
<strategy name="NodeMerge">
<context-param>
<param-name>$left:param-name</param-name>
<param-value>$left:param-value $right:param-value</param-value>
</context-param>
</strategy>
</choice>
</strategy>
</contextParams>
</webXml>
</uberwar>


3、使用cargo:uberwar合并这2个war包

maven打包部署到私服
输出日志如下