maven 打包可執行jar的方法

在這以前,我本身找了點maven的東西添加進來 java

Maven內置變量說明:
	${basedir} 項目根目錄
	${project.build.directory} 構建目錄,缺省爲target
	${project.build.outputDirectory} 構建過程輸出目錄,缺省爲target/classes
	${project.build.finalName} 產出物名稱,缺省爲${project.artifactId}-${project.version}
	${project.packaging} 打包類型,缺省爲jar
	${project.xxx} 當前pom文件的任意節點的內容



方式1 apache

修改pom.xml增長以下內容 app

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.4</version>
	<configuration>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib/</classpathPrefix>
				<mainClass>com.sysware.HelloWorld</mainClass>
			</manifest>
		</archive>
	</configuration>
</plugin>

運行mvn clean package便可 maven

批註: 工具

通過我本人自測以後發現,這種方式雖然能夠打包,可是不會將依賴包也添加進去。因此我認爲這種方式的打包適合寫一個工具,而後打成JAR包,導入其餘工程使用,若是是一個單獨運行的JAR包,則不適用。 ui

方式2 編碼

在pom.xml增長以下內容 spa

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.3</version>
	<configuration>
		<appendAssemblyId>false</appendAssemblyId>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
		<archive>
			<manifest>
				<mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>
			</manifest>
		</archive>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>assembly</goal>
			</goals>
		</execution>
	</executions>
</plugin>
運行mvn assembly:assembly ( 我本身使用mvn clean package也是能夠的

批註: code

這個方式和方式1不一樣,這種打包方式,將項目及所依賴的全部jar包打包成一個jar,讓打出來的JAR包成爲一個可獨立運行的JAR包 orm

方式3

批註:

    我本身在博主的基礎上,添加了一些配置和註釋

    方式3和方式4都是比較複雜的打包方式,可是能夠本身定義細節,好比須要哪些文件被打包,哪些文件不打包進去,哪些文件放到哪裏等等。

<build>
	<finalName>test-${project.version}</finalName>
	<sourceDirectory>src/main/java</sourceDirectory>
	<resources>
		<!-- 控制資源文件的拷貝 -->
		<resource>
			<directory>src/main/resources</directory>
			<targetPath>${project.build.directory}</targetPath>
			<!-- excludes和includes二選一使用便可 -->
			<!-- 不包含的文件,支持通配符 -->
			<excludes>
				<exclude>*.txt</exclude>
			</excludes>
			<!-- 包含的文件,支持通配符 -->
			<includes>
				<include>*.properties</include>
			</includes>
		</resource>
	</resources>
	<plugins>
		<!-- 設置源文件編碼方式 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<defaultLibBundleDir>lib</defaultLibBundleDir>
				<source>1.7</source>
				<target>1.7</target>
				<encoding>UTF-8</encoding>
			</configuration>
		</plugin>
		<!-- 打包jar文件時,配置manifest文件,加入lib包的jar依賴 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
						<classpathPrefix>lib/</classpathPrefix>
						<mainClass>com.eya.main.Hello</mainClass>
					</manifest>
				</archive>
			</configuration>
		</plugin>
		<!-- 拷貝依賴的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>
		<!-- 解決資源文件的編碼問題 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>2.4</version>
			<configuration>
				<encoding>UTF-8</encoding>
			</configuration>
		</plugin>
		<!-- 打包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>
	</plugins>
</build>

方式4

<build>
		<resources>
			<resource>
				<targetPath>${project.build.directory}/classes</targetPath>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.0</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass>com.test.testguava.app.App</mainClass>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>applicationContext.xml</resource>
								</transformer>
							</transformers>
							<shadedArtifactAttached>true</shadedArtifactAttached>
							<shadedClassifierName>executable</shadedClassifierName>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
相關文章
相關標籤/搜索