体验maven插件功能,同时打包jar和war
maven 是一个java编写的用于构建系统的自动化工具,正在逐渐取代Ant,成为主流的工具.
官方主页:http://maven.apache.org/
maven打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par。但是maven同时只能生成一种类型的包,在实际项目中,如果
同时生成几种类型的包,如生成jar,同时生成war,并将新生成的jar也打到war中。这就需要使用maven的插件功能,下面列举一些常用的插件:
1 同时生成jar和war, war在主文件中,用plugin生成jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>target/classes</classesDirectory>
<finalName>fooWebApp</finalName>
<outputDirectory>target/fooWebApp/WEB-INF/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
2 类似于ant的文件复制
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
src/main/webapp/WEB-INF/lib
</outputDirectory>
<resources>
<resource>
<directory>
target/jar
</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
3 指定jdk版本
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
4 指定编码格式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
注意: maven打包和构建是异步执行的,同时执行多个任务时,可能会发生IO错误,如要拷贝的文件夹正在生成。可以考虑将该任务分成多个阶段。
完整的示例:
<project xmlns="http://maven.apache.org/POM/4.0.0 "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
<modelVersion>4.0.0</modelVersion>
<groupId>net.zhuozhe</groupId>
<artifactId>fooWebApp</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>fooWebApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<finalName>fooWebApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>target/classes</classesDirectory>
<finalName>fooWebApp</finalName>
<outputDirectory>target/fooWebApp/WEB-INF/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
src/main/webapp/WEB-INF/lib
</outputDirectory>
<resources>
<resource>
<directory>
target/jar
</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>