maven关于打包的那些插件

在工作中使用maven创建java工程,管理jar包依赖,方便快捷。根据需要,需要把工程打包成各种需要的形式,这些打包插件就用到了。
现将各种打包用到的插件总结到这里,你可以参考官方文档,修改或者增加适合你的参数。

第一个:打源码包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<plugin>  
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-source-plugin</artifactId>  
	<version>2.1</version>  
	<configuration>  
		<attach>true</attach>  
	</configuration>  
	<executions>  
		<execution>  
			<phase>compile</phase>  
			<goals>  
				<goal>jar</goal>  
			</goals>  
		</execution>  
	</executions>  
</plugin>

第二个:打全量包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>1.2.1</version>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
			<configuration>
				<transformers>
					<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
						<mainClass>com.zhaoyanblog.Launcher</mainClass>
					</transformer>
					<!--防止spring多个包同名配置文件覆盖-->
					<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
						<resource>META-INF/spring.handlers</resource>
					</transformer>
					<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
					   <resource>META-INF/spring.schemas</resource>
					</transformer
				</transformers>
			</configuration>
		</execution>
	</executions>
</plugin>

第三个:copy依赖jar包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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>
				<includeScope>runtime</includeScope>
				<outputDirectory>target/lib/</outputDirectory>
			</configuration>
		</execution>
	</executions>
</plugin>

第四个:指定main方法,修改manifest文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.3.2</version>
	<configuration>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib</classpathPrefix>
				<mainClass>com.zhaoyanblog.Launcher</mainClass>
			</manifest>
			<manifestEntries>
				<Class-Path>conf/</Class-Path>
			</manifestEntries>
		</archive>
		<finalName>tools-${project.version}</finalName>
	</configuration>
</plugin>

第五个:打压缩包。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.4</version>
	<executions>
		<execution>
			<id>make-tar.gz</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
			<configuration>
				<finalName>${project.build.finalName}</finalName>
				<skipAssembly>false</skipAssembly>
				<descriptors>
					<descriptor>tar.gz.xml</descriptor>
				</descriptors>
			</configuration>
		</execution>
	</executions>
</plugin>

同时要增加一个tar.gz.xml文件,描述打包内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
 
<assembly 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/xsd/assembly-1.0.0.xsd">
	<id>package</id>
	<formats>
		<format>tar.gz</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>bin</directory>
			<outputDirectory>/</outputDirectory>
			<fileMode>0755</fileMode>
			<directoryMode>0755</directoryMode>
		</fileSet>
		<fileSet>
			<directory>${project.build.directory}/conf</directory>
			<outputDirectory>/conf</outputDirectory>
			<includes>
				<include>*.xml</include>
				<include>*.properties</include>
			</includes>
		</fileSet>
	</fileSets>
	<files>
		<file>
			<source>${project.build.directory}/${project.build.finalName}.jar</source>
			<destName>/${project.build.finalName}.jar</destName>
		</file>
	</files>
	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
</assembly>

留言

提示:你的email不会被公布,欢迎留言^_^

*

验证码 *