当前位置:首页 > 开发 > 编程语言 > 持续集成 > 正文

maven打包成可执行的jar包(package a runnable jar)

发表于: 2015-04-23   作者:conkeyn   来源:转载   浏览:
摘要: 第一步:利用maven-jar-plugin生成当前工程的jar,该jar包包含manifest的main-class和class-path参数。 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId&

第一步:利用maven-jar-plugin生成当前工程的jar,该jar包包含manifest的main-class和class-path参数。

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.6</version>
	<configuration>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib/</classpathPrefix>
				<mainClass>com.zkteco.timecube.zkcloud.phoenix.core.App</mainClass>
			</manifest>
			<!-- 添加当前目录和子目录lib -->
			<manifestEntries>
				<Class-Path>. ./lib</Class-Path>
			</manifestEntries>
		</archive>
	</configuration>
</plugin>

 第二步:添加组装描述文件,assembly.xml

<?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>zip</format>
	</formats>
	<includeBaseDirectory>true</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>${project.build.directory}</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
			<excludes>
				<exclude>*sources.jar</exclude>
			</excludes>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
</assembly>

 第三步:添加maven-assembly-plugin插件,不可以再配置maven-assembly-plugin的<manifest>标签,不然会覆盖maven-jar-plugin生成的manifes文件。

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<appendAssemblyId>false</appendAssemblyId>
		<descriptors>
			<descriptor>src/assemble/package.xml</descriptor>
		</descriptors>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

 生成的manifest.mf文件的class-path结果:

Class-Path: . ./lib  ./lib/slf4j-api-1.7.10.jar ./lib/slf4j-log4j12-1.7.10.jar ./lib/log4 j-1.2.17.jar ./lib/commons-lang3-3.3.2.jar

 由此可以看出<manifestEntries>标签先于<manifest>执行

 

maven打包成可执行的jar包(package a runnable jar)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号