maven生成war包的两种格式
http://touchfu.iteye.com/blog/545708http://blog.sina.com.cn/s/blog_67e0655401016ipm.html war包即对WEB应用程序进行打包,用于应用容器的部署。如在jboss中只要把war包丢入deploy目录下即可发布自己的应用了。打包方式有很多中,很多工具本身就支持此功能。下面主要介绍通过maven来
方法一:
- <build>
-
<plugins> -
...... -
<plugin> -
<groupId>org.apache.maven.plugins</groupId> -
<artifactId>maven-war-plugin</artifactId> -
<version>2.0.2</version> -
<configuration> -
<warSourceExcludes>WEB-INF/lib/log4j-${log4j.version}.jar</warSourceExcludes> -
</configuration> -
</plugin> -
</plugins> - </build>
当然你可以指定war包的名称,输出的目录,webapp目录等等。
可以在需要打包的工程中直接设定:<pom>war</pom>
这样在install的时候就会对此工程生成相应配置的war包,原来此web工程依赖的其他工程全部会以jar包的形式出现在lib目录下,所以不需要再去关心war包中集成的jar包是否完整,maven已经帮你做完了。
顺便提一句,在jboss中发布war包,如果包中包含log4j.jar,会报错,所以在本例中将此jar包排除在外。
方法二:
不用war插件,而是用到另外三个插件:maven-dependency-plugin、maven-antrun-plugin
下面分别说明下着三个插件在此过程扮演的角色:
maven-dependency-plugin :相当于数据收集器,该依赖那些jar包,该加载那些resource信息。
maven-antrun-plugin:完成收集完文件夹的压缩和包的重命名。
build-helper-maven-plugin:让压缩包拥有男人本色,oh sorry 是war本色。
在写pom.xml配置前,先说明下目录结构
root
|
|---template
|
|---wardir
其中这两个目录都作为maven的独立工程,下面要写配置在wardir下的pom.xml中
- <build>
-
<plugins> -
<plugin> -
<groupId>org.apache.maven.plugins</groupId> -
<artifactId>maven-dependency-plugin</artifactId> -
<version>2.0</version> -
<executions> -
<execution> -
<id>create-structure</id> -
<phase>process-resources</phase> -
<goals> -
<goal>unpack</goal> -
</goals> -
<configuration> -
<artifactItems> -
<artifactItem> -
<groupId>GROUPID</groupId> -
<artifactId>template</artifactId> -
</artifactItem> -
</artifactItems> -
<outputDirectory> -
${project.build.directory}/${assembly.name}.war -
</outputDirectory> -
</configuration> -
</execution> -
<execution> -
<id>copy-libs</id> -
<phase>process-resources</phase> -
<goals> -
<goal>copy</goal> -
</goals> -
<configuration> -
<artifactItems> -
<artifactItem> -
<groupId>jdom</groupId> -
<artifactId>jdom</artifactId> -
</artifactItem> -
...... -
</artifactItems> -
-
<outputDirectory>${project.build.directory}/${assembly.name}.war/WEB-INF/lib</outputDirectory> -
</configuration> -
</execution> -
</executions> -
</plugin> -
<plugin> -
<groupId>org.apache.maven.plugins</groupId> -
<artifactId>maven-antrun-plugin</artifactId> -
<version>1.3</version> -
<executions> -
<execution> -
<id>copy-config</id> -
<phase>process-resources</phase> -
<goals> -
<goal>run</goal> -
</goals> -
<configuration> -
<tasks> -
<copy todir="${project.build.directory}/${assembly.name}.war"> -
<fileset dir="src/main/resources" /> -
</copy> -
<zip destfile="${project.build.directory}/${assembly.name}-${project.version}-war.zip"> -
<zipfileset dir="${project.build.directory}/${assembly.name}.war" includes="**\*.*" excludes="*.zip" /> -
</zip> -
<copy file="${project.build.directory}/${assembly.name}-${project.version}-war.zip" -
tofile="${project.build.directory}/${assembly.name}-${project.version}.war" /> -
</tasks> -
</configuration> -
</execution> -
</executions> -
</plugin> -
<plugin> -
<groupId>org.codehaus.mojo</groupId> -
<artifactId>build-helper-maven-plugin</artifactId> -
<version>1.4</version> -
<executions> -
<execution> -
<id>attach-artifacts</id> -
<phase>package</phase> -
<goals> -
<goal>attach-artifact</goal> -
</goals> -
<configuration> -
<artifacts> -
<artifact> -
<file>${project.build.directory}/${assembly.name}-${project.version}.war</file> -
<type>war</type> -
</artifact> -
</artifacts> -
</configuration> -
</execution> -
</executions> -
</plugin> -
</plugins> - </build>
更多推荐
所有评论(0)