Home > Archives > 如何编译出你想要的jar包?

如何编译出你想要的jar包?

Publish:

maven-jar-plugin – 主要目的是生成 Jar包中的 MANIFEST.MF,和对应的jar包

<plugin>
	<artifactId>maven-jar-plugin</artifactId>
	<configuration>
		<excludes>
			<exclude>spring/*</exclude>
			<exclude>*.xml</exclude>
			<exclude>*.properties</exclude>
		</excludes>
		<includes>
			<include>**/</include>
		</includes>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib/</classpathPrefix>  <!-- 这个是引用到的包 -->
				<mainClass>com.baoguoding.ServerNodeRun</mainClass>
			</manifest>
			<manifestEntries>
				<Class-Path>./conf/</Class-Path>  <!-- 这个和下面的配置文件想对应 -->
			</manifestEntries>
		</archive>
	</configuration>
</plugin>

maven-resources-plugin – 主要是管理resource相关内容

<plugin>
	<artifactId>maven-resources-plugin</artifactId>
	<version>2.6</version>
	<executions>
		<execution>
			<id>copy-resources</id>
			<phase>package</phase>
			<goals>
				<goal>copy-resources</goal>
			</goals>
			<configuration>
				<outputDirectory>${basedir}/target/conf</outputDirectory>
				<resources>
					<resource>
						<directory>src/main/resources</directory>
					</resource>
				</resources>
			</configuration>
		</execution>
	</executions>
</plugin>

maven-compiler-plugin – 这个是编译器相关配置

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>2.3.2</version>
	<configuration>
		<target>1.7</target>
		<source>1.7</source>
		<encoding>UTF-8</encoding>
	</configuration>
</plugin>

maven-javadoc-plugin – 这个是javadoc相关配置

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-javadoc-plugin</artifactId>
	<version>2.9</version>
	<configuration>
		<aggregate>true</aggregate>
		<charset>UTF-8</charset>
		<encoding>UTF-8</encoding>
		<docencoding>UTF-8</docencoding>
	</configuration>
	<executions>
		<execution>
			<phase>deploy</phase>
			<goals>
				<goal>jar</goal>
			</goals>
			<inherited>false</inherited>
		</execution>
	</executions>
</plugin>

maven-dependency-plugin – 这个是依赖相关管理

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
		<execution>
			<id>copy-dependencies</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<excludeScope>provided</excludeScope>
				<outputDirectory>${project.build.directory}/lib</outputDirectory>
				<overWriteReleases>false</overWriteReleases>
				<overWriteSnapshots>false</overWriteSnapshots>
				<overWriteIfNewer>true</overWriteIfNewer>
			</configuration>
		</execution>
	</executions>
</plugin>

maven-surefire-plugin – Resolve: 执行maven test命令时console出现中文乱码

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.7.2</version>
	<configuration>
		<forkMode>once</forkMode>
		<argLine>-Dfile.encoding=UTF-8</argLine>
		<skipTests>true</skipTests>
	</configuration>
</plugin>

maven-assembly-plugin – 按照自己的要求来打zip包

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<!-- not append assembly id in release file name -->
		<appendAssemblyId>false</appendAssemblyId>
		<descriptors>
			<descriptor>src/main/build/package.xml</descriptor>
		</descriptors>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

package.xml – zip包相关的配置

<?xml version="1.0" encoding="UTF-8"?>
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
	<id>package</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>true</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>bin</directory>
			<outputDirectory>/</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>src/main/resources</directory>			
			<outputDirectory>/</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${project.build.directory}</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
			<scope>runtime</scope>
			<excludes>
				<exclude>${groupId}:${artifactId}</exclude>
			</excludes>
		</dependencySet>
	</dependencySets>
</assembly>  

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: Ding Bao Guo