欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven常用配置

程序员文章站 2024-01-19 10:27:34
...
1.设置Compile插件,编译时使用jdk1.6,文件编码格式为UTF-8
<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<source>1.6</source>
		<target>1.6</target>
		<encoding>UTF-8</encoding>
	</configuration>
</plugin>


2.设置resource插件,指定资源文件的编码格式为UTF-8
<plugin>				<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>2.5</version>
	<configuration>
		<encoding>UTF-8</encoding>
	</configuration>
</plugin>


3.设置打包时跳过测试
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
		<skip>true</skip>
		<testFailureIgnore>true</testFailureIgnore>
	</configuration>
</plugin>


4.设置在编译时同时生成源文件的source-jar包
<plugin>
	<artifactId>maven-source-plugin</artifactId>
	<version>2.1</version>
	<configuration>
		<attach>true</attach>
		<encoding>UTF-8</encoding>
	</configuration>
	<executions>
		<execution>
			<phase>compile</phase>
			<goals>
				<goal>jar</goal>
			</goals>
		</execution>
	</executions>
</plugin>


5.拷贝依赖的jar包,到指定目录,如lib目录
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
		<execution>
			<id>copy</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<outputDirectory>
					${project.build.directory}/lib
				</outputDirectory>
			</configuration>
		</execution>
	</executions>
</plugin>


6.打包成jar包时,设置运行主程序类及把lib的jar包添加到classpath
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<configuration>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib/</classpathPrefix>
				<mainClass>com.ecss.monitor.MonitorMain</mainClass>
			</manifest>
		</archive>
	</configuration>
</plugin>


7.提取依赖jar包的class文件,与自己的项目代码一起,打包到同一个jar文件中
<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<archive>
			<manifest>
				<mainClass>com.iflytek.ecss.Main</mainClass>
			</manifest>
		</archive>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
	</configuration>
	<executions>
		<execution>
			<id>make-my-jar-with-dependencies</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>


8.设置资源文件拷贝的源目录和目标目录
这个配置是在build节点下。
<resources>
	<!-- 控制资源文件的拷贝 -->
	<resource>
		<directory>src/main/resources</directory>
		<targetPath>${project.build.directory}</targetPath>
	</resource>
	<resource>
		<directory>src/main/resources</directory>
	</resource>
</resources>


9.设置Maven编译时引入本地jar包,添加dependency,设置scope为system即可。示例如下
<dependency>
	<groupId>json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.0</version>
	<scope>system</scope>
	<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/json-lib-2.0-jdk15.jar</systemPath>
</dependency>



其它关于maven的工作机制及设置,参考http://ajita.iteye.com/blog/1709192