本部分内容介绍了Maven打包Plugin。
本文内容全部翻译自Apache Software Foundation,如有转载,请声明!
Jar插件
Jar插件用于创建jar文件,并对jar文件进行签名。Jar插件包含了四个目标,如下所述:
jar:jar用于创建工程源代码的jar文件。
必要参数:
classesDirectory:保存类文件的目录
finalName:jar文件的名称,默认值为${project.build.finalName}
outputDirectory:生成的jar文件的保存位置。
可选参数:
archive:使用的档案文件配置。
classifier:添加到生成的产品中的classifier。
excludes:排除的文件,使用fileset模式来指定。
foreCreation:是否强制生成文件,默认为false。
includes:包含的文件。
useDefaultManifestFile:是否使用默认的Manifest文件,默认为false。
jar:test-jar用于创建测试类的jar文件。
Maven2.0环境,需要在pom中指定范围为test,自动在package构建阶段执行。
可选参数及必要参数与jar:jar类似。
jar:sign对jar文件进行签名。
http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/jarsigner.html#Options必要参数:
alias:别名,参考j2se中的jarsigner命令
finalName:生成jar文件的名字。
workingDirectory:工作目录,运行jarsigner程序的目录。默认为${basedir}。
可选参数:
classifier:
jarPath:jar文件的路径,当指定的时候,忽略finalName选项。
keypass、keystore、sigfile、signedjar、storepass、type、verbose、verify等参数请参考jarsigner命令的javadoc文档。
jar:sign-verify用于验证一个签名的jar文件。
Maven2.0环境,需要在pom中指定范围为test,自动在package构建阶段执行。
必须参数:
finalName:要验证的jar文件的名字。
workingDirecotry:运行jarsigner的目录。
可选参数:
checkCerts、verbose参考jarsigner命令的文档。
如果Project的packaging类型为jar,那么当运行package阶段的时候,就会运行如下的命令:
mvn package
,这时会在工程的target目录中生成jar文件。
如果需要对jar文件进行签名,那么就需要在pom.xml文件中对要签名的jar文件进行适当的配置。注意,可以自动的在签名之后验证jar文件。
<project>
...
<packaging>jar</packaging>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<keystore>/path/to/your/keystore</keystore>
<alias>youralias</alias>
<storepass>yourstorepassword</storepass>
<signedjar>${project.build.directory}/signed/${project.build.finalName}.jar</signedjar>
<verify>true</verify>
</configuration>
</plugin>
也可以通过在命令行上执行如下的命令来设置签名的参数:
mvn jar:sign -Dkeystore=/path/to/your/keystore \
-Dstorepass=yourstorepassword -Dalias=youralias
禁用签名的方式为:
mvn ... –Dmaven.jar.skip.sign=true
在打包时,有些时候可能需要包含特定的文件或者排除特定的文件,设置如下:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>**/service/*</include>
</includes>
</configuration>
</plugin>
通过指定fileset模式来包含或者排除文件,同时添加classifier元素在pom.xml文件中:
注意,jar插件必须在新的执行过程中进行定义,否则会替换默认jar插件,而不是添加一个新的产品。
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>client</classifier>
<includes>
<include>**/service/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
WAR插件
war插件用于汇集web应用程序的所有的产品依赖、类以及资源,然后将它们打包到一个web应用程序文件(.war)中。
war插件包含3个goals。
war:war目标是在运行package类型为war的工程的package构建阶段时默认的goal。
需要Maven2.0项目环境,依赖处理的范围为runtime,在package阶段自动运行。
必要参数:
cacheFile:包含webapp结构的缓存文件。
outputDirectory:生成的WAR文件的目录。
warName:WAR文件的名字。
warSourceDirctory:单一目录,保存war文件中要包含的文件。
webappDirectory:构建webapp的目录。
workDirectory:用于保存解压缩依赖的WAR文件的目录。
可选参数:
http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#Optional Parameters
war:exploded目标一般用来在开发过程中加快测试,通过在指定目录创建一个exploded war。
与war:war的说明基本一致。
war:inplace是war:explode的另一种变化形式,war被生成在web源目录,而不是默认的src/main/webapp。
共有四种方式来使用war插件:
使用war作为packaging的值。
调用war:war目标。
调用war:exploded目标。
调用war:inplace目标。
当使用war目标时,假设已经完成了编译,即war插件不对java源文件进行编译或者拷贝资源文件。
<project>
[...]
<groupId>com.example.projects</groupId>
<artifactId>documentedproject</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Documented Project</name>
<url>http://example.com</url>
[...]
</project>
这种类型的工程的目录结构为:
.
|-- pom.xml
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- projects
| `-- SampleAction.java
|-- resources
| |-- images
| | `-- sampleimage.jpg
| `-- sampleresource
`-- webapp
|-- WEB-INF
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp
然后可以调用
mvn package或者mvn compile war:war
就可以生成target/documentedproject-1.0-SNAPSHOT.war文件了。该文件的结构如下:
documentedproject-1.0-SNAPSHOT.war
|-- META-INF
| |-- MANIFEST.MF
| `-- maven
| `-- com.example.projects
| `-- documentedproject
| |-- pom.properties
| `-- pom.xml
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | `-- SampleAction.class
| | `-- images
| | `-- sampleimage.jpg
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp
可以通过mvn compile war:exploded命令来生成一个exploded版本的war。目录为:target/documentedproject-1.0-SNAPSHOT。目录结构如下:
documentedproject-1.0-SNAPSHOT
|-- META-INF
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | `-- SampleAction.class
| | `-- images
| | `-- sampleimage.jpg
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp
默认的目录名为target/<finalName>,finalName的名字一般为<artifact>-<version>。可以通过制定webappDirectory插件参数来覆盖默认的值。
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
</configuration>
</plugin>
可以通过在命令行上调用mvn compile war:inplace命令,生成如下的目录结构:
.
|-- pom.xml
|-- src
| `-- main
| |-- java
| | `-- com
| | `-- ideal
| | `-- projects
| | `-- SampleAction.java
| |-- resources
| | |-- images
| | | `-- sampleimage.jpg
| | `-- sampleresource
| `-- webapp
| |-- META-INF
| |-- WEB-INF
| | |-- classes
| | | |-- com
| | | | `-- ideal
| | | | `-- projects
| | | | `-- SampleAction.class
| | | `-- images
| | | `-- sampleimage.jpg
| | `-- web.xml
| |-- index.jsp
| `-- jsp
| `-- websource.jsp
`-- target
`-- classes
|-- com
| `-- ideal
| `-- projects
| `-- SampleAction.class
`-- images
`-- sampleimage.jpg
Overlays
Overlays意味着多个web应用程序之间共享共同的资源。一个war工程所有的依赖都被汇集到WEB-INF/lib目录下,除了那些被指定为overlayed的资源。
在以前的war插件版本中,不需要进行任何的配置,这只是当默认配置符合需求的时候,否则,可以进行自定义的配置。
假设如下的目录结构:
.
|-- pom.xml
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- projects
| `-- SampleAction.java
|-- resources
| |-- images
| | `-- sampleimage.jpg
| `-- sampleresource
`-- webapp
|-- WEB-INF
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp
上面的工程依赖于documentedprojectdependency-1.0-SNAPSHOT.war
documentedprojectdependency-1.0-SNAPSHOT.war
|-- META-INF
| |-- MANIFEST.MF
| `-- maven
| `-- com.example.projects
| `-- documentedprojectdependency
| |-- pom.properties
| `-- pom.xml
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | `-- SampleActionDependency.class
| | |-- images
| | | `-- sampleimage-dependency.jpg
| `-- web.xml
|-- index-dependency.jsp
假设已经将war作为依赖在pom.xml中进行了设置。
[...]
<dependencies>
<dependency>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
[...]
</dependencies>
[...]
那么最后的war的结构如下:
|-- META-INF
| |-- MANIFEST.MF
| `-- maven
| `-- com.example.projects
| |-- documentedproject
| | |-- pom.properties
| | `-- pom.xml
| `-- documentedprojectdependency
| |-- pom.properties
| `-- pom.xml
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | |-- SampleAction.class
| | | `-- SampleActionDependency.class
| | |-- images
| | | |-- sampleimage-dependency.jpg
| | | `-- sampleimage.jpg
| `-- web.xml
|-- index-dependency.jsp
|-- index.jsp
|-- jsp
| `-- websource.jsp
war插件可以处理war和zip产品,然后,为了向后兼容,zip overlays只有明确定义才可以使用。
下面是配置overlays所需要的参数:
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<excludes>
<exclude>images/sampleimage-dependency.jpg</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
[...]
参数说明如下:
# id - sets the id of the overlay. If none is provided, the war plugin will generate one.
# groupId - sets the groupId of the overlay artifact you want to configure.
# artifactId - sets the artifactId of the overlay artifact you want to configure.
# classifier - sets the classifier of the overlay artifact you want to configure if multiple artifacts matches the groupId/artifact.
# includes - sets the files to include. By default, all files are included.
# excludes - sets the files to exclude. By default, the META-INF directory is excluded.
# targetPath - sets the target relative path in the webapp structure, which is only available for war overlays. By default, the content of the overlay is added in the root structure of the webapp.
# skip - set to true to skip this overlay. Default is false.
此外,还可以添加或者过滤外部Web资源文件,自定义WAR文件的Manifest、使用Jetty6(jetty为maven提供了插件)进行快速的测试,创建SkinnyWAR文件。更多内容可以参考:
还有一些其他的打包工具,例如ear、ejb、rar和shade,笔者就不一一介绍,更多内容可以参考:
发表于 @ 2008年05月27日 16:56:53|编辑