一、变量-自定义变量及内置变量
1.自定义变量
<properties>
<project.build.name>tools</project.build.name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
2.内置变量
${basedir} 项目根目录
${project.build.directory} 构建目录,缺省为target
${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
${project.packaging} 打包类型,缺省为jar
${project.xxx} 当前pom文件的任意节点的内容
二、常见插件配置
1.编译插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
source: 源代码编译版本;
target: 目标平台编译版本;
encoding: 字符集编码
2.设置资源文件的编码方式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
xml、properties文件都是资源文件,编码的时候遇到中文总要进行转码!用什么编码?UTF-8,那就记得强制 <encoding>${project.build.sourceEncoding}</encoding>
3.自动拷贝jar包到target目录
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- ${project.build.directory}为Maven内置变量,缺省为target -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<!-- 表示是否不包含间接依赖的包 -->
<excludeTransitive>false</excludeTransitive>
<!-- 表示复制的jar文件去掉版本信息 -->
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
关于maven-dependency-plugin:用得最多的几个操作:copy, copy-dependencies和它们对应的unpack, unpack-dependencies
描述:copy 和 unpack操作是由要拷某个包,这个包需要具体指定要拷哪个包,与当前工程的依赖没有关系。这两者区别-是否解压
copy-dependencies和unpack-dependencies,但是它是用来拷当前工程的依赖包的。这两者区别-是否解压
参考链接:http://liugang594.iteye.com/blog/2093082
4.生成源代码jar包
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<!-- <finalName>${project.build.name}</finalName> -->
<attach>true</attach>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
5.将项目打成jar包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<!-- 告知 maven-jar-plugin添加一个 Class-Path元素到 MANIFEST.MF文件,以及在Class-Path元素中包括所有依赖项 -->
<addClasspath>true</addClasspath>
<!-- 所有的依赖项应该位于 lib文件夹 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- 当用户使用 lib命令执行JAR文件时,使用该元素定义将要执行的类名 -->
<mainClass>com.zhengtian.tools.service.phone.MobilePhoneTool</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
在将项目打成jar包时,有时会需要将项目打成可以直接运行的jar包,因此就需要将项目依赖的jar包也打入jar包中,此时需要在Eclipse上安装例外一个插件,用来打可执行jar包
更多maven配置详见:https://my.oschina.net/zh119893/blog/276090
6.关于maven-assembly-plugin的使用
“assembly”是把一组文件、目录、依赖元素组装成一个归档文件
参考链接:http://blog.csdn.net/WANGYAN9110/article/details/38646677
pom中配置
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>make-zip</id>
<!-- 绑定到package生命周期阶段上 -->
<phase>package</phase>
<goals>
<!-- 绑定到package生命周期阶段上 -->
<goal>single</goal>
</goals>
<configuration>
<descriptors> <!--描述文件路径-->
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
assembly.xml配置
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}\src\main\resources</directory>
<outputDirectory>\</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}\src\bin</directory>
<outputDirectory>\bin</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<!-- 将scope为runtime的依赖包打包到lib目录下。 -->
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
转载自:http://blog.csdn.net/amorym/article/details/52619402