---
title: "java打包可运行的jar包"
author: "z19910509"
date: "2015-09-07T11:23:56Z"
tags: []
categories: ["编程语言"]
slug: "java-jar"
draft: false
lastmod: "2015-09-07T11:23:55Z"
id: 2239958
url: "https://notafraid.iteye.com/blog/2239958"
---

整理了下各种打包可执行jar的方法，个人而言，觉得使用**spring-boot插件**更方便，且生成的jar包也更精简，推荐使用

 

**方法一：使用maven-jar-plugin插件**

```
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>2.6</version>
   <configuration>
      <archive>
         <manifest>
            <!--将依赖的jar添加到classpath，具体效果查看生成jar里的META-INF/MANIFEST.MF的Class-Path-->
            <addClasspath>true</addClasspath>
            <!--classpath路径前缀，如lib/log4j.jar-->
            <classpathPrefix>lib/</classpathPrefix>
<!--程序入口类-->
            <mainClass>cn.qlt.service.UserService</mainClass>
         </manifest>
      </archive>
   </configuration>
</plugin>
<!--该插件用于将依赖jar复制到指定文件夹-->
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
      <execution>
         <id>copy</id>
         <!--将该操作绑定在compile周期中-->
         <phase>compile</phase>
         <goals>
           <goal>copy-dependencies</goal>
         </goals>
         <configuration>
            <outputDirectory>
            <!--复制的ar包存放的目录,以下路径即target/lib-->
             ${project.build.directory}/lib
            </outputDirectory>
         </configuration>
      </execution>
   </executions>
</plugin>
```

 

运行:mvn clean package

会在target目录下生成一个可执行的jar包与存放依赖的lib文件夹，将生成的jar包与lib文件夹放入同一目录下，执行 java -jar XXX.jar即可

说明：

1、依赖的jar包与程序不能打入一个jar包，这样最终部署的时候还得程序jar与依赖的jar包同时部署，比较麻烦

2、通过设置outputDirectory:${project.build.directory}/classes/lib也做到了将依赖与程序打入一个jar，但最终运行总是找不到依赖的jar。尝试了许多次均宣告失败。若有知道用该插件将依赖与程序打为一个jar的相关配置，烦劳告知，谢谢

 

**方法二：使用maven-assembly-plugin插件**

 

```
<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.5</version>
				<configuration>
					<archive>
						<manifest>
							<mainClass>cn.qlt.service.UserService</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<!--引用id为jar-with-dependencies的一个descriptor文件，该文件为该插件预先写好的文件。在.m2\repository\org\apache\maven\plugins\maven-assembly-plugin\2.5.5文件夹，解压maven-assembly-plugin-2.5.5.jar，在assemblies文件夹里可查看该文件-->
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
```

 

 

   
 说明：通过上述方法即可生成可执行的jar包，但其将依赖的jar包都解压了，即我们看到的都是一个个文件夹与class文件，这样从管理等方面都不是很友好。

 

上面说过jar-with-dependencies其实是一个id为jar-with-dependencies的xml文件，下面就是我们自定义这种xml文件的方法。

```
 <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.5</version>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
						<configuration>
						<!--若为true，则在生成的jar名称后面追加assembly.xml文件中我们定义的id-->
						<appendAssemblyId>false</appendAssemblyId>
						<descriptors>
							<!--自定义的xml文件-->
							<descriptor>assembly.xml</descriptor>
						</descriptors>
						<archive>
							<manifest>
								<mainClass>cn.qlt.service.UserService</mainClass>
							</manifest>
						</archive>
						</configuration>
					</execution>
				</executions>
			</plugin>  
```

 assembly.xml：

```
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>distribution</id>
	<!-- 如果为true，会将文件打入以项目ArtifactId为名的文件夹内-->
	<includeBaseDirectory>false</includeBaseDirectory>
	<formats>
		<!--打包类型-->
		<format>jar</format>
	</formats>
	<fileSets>
		<fileSet>
                        <!--将指定目录中文件打包到下面定义目录，这里为classes目录-->
			<directory>${project.build.outputDirectory}</directory>
			<outputDirectory>/</outputDirectory>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
                       <!--若为true，则将依赖解压，即我们看到的是文件夹和class文件 -->
			<unpack>false</unpack>
			<!--是否将当前项目的非执行jar包打入依赖--> 
                        <useProjectArtifact>false</useProjectArtifact>
                         <!--依赖的输出地址-->
		         <outputDirectory>/lib</outputDirectory>
		</dependencySet>
	</dependencySets>
</assembly>
```

 通过这种方式，我们将依赖与我们的程序都打入一个jar包了，但java -jar运行程序的时候，依赖的类都是notfound，是不是因为没有添加到classpath，不知道怎么解决。在MANIFEST.MF中配置Class-Path只能配置当前jar包外面的路径，因依赖jar已打入jar包，不知如何配置

 

**方法三：使用maven-shade-plugin插件**

 

```
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>2.4.1</version>
	<configuration>
		<transformers>
			<transformer        
				implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!--设置入口类-->				<mainClass>cn.qlt.service.UserService</mainClass>
			</transformer>
		</transformers>
	</configuration>
	<executions>
		<execution>
                        <!--绑定在maven的package生命周期上-->
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
		</execution>
	</executions>
</plugin> 
```

 

运行:mvn clean package，会在target目录里生成xxx-shaded.jar,运行该jar即可

 

说明：

1、和上面maven-assembly-plugin插件一样**，**通过该方法生成的可运行jar文件，将我们依赖的jar包都解压了，即我们看到的是一个个的文件夹与class，这对于我们的管理十分的不方便

2、如果想去掉生成的jar文件名的后缀shaded或者改一个名字，在&lt;configuration&gt;&lt;/configuration&gt;中增加&lt;shadedArtifactAttached&gt;false&lt;/shadedArtifactAttached&gt;或&lt;shadedClassifierName&gt;自定义的ClassifierName&lt;/shadedClassifierName&gt;即可

 

**方法四：使用spring-boot插件（建议）**

maven：

```
<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<version>1.2.5.RELEASE</version>
		<executions>
			<execution>
				<goals>
					<goal>repackage</goal>
				</goals>
				<configuration>
					<!--非必填项，即在生成的jar包名称后面追加该分类名称-->
					<classifier>boot</classifier>
					<mainClass>cn.qlt.service.UserService</mainClass>
				</configuration>
			</execution>
		</executions>
</plugin> 
```

 

 

运行mvn clean package即可。该方法将依赖与我们自己的程序打入同意jar包。且依赖jar没有解压放入lib文件夹下。通过jar -jar运行没有出现依赖找不到的情况。

 

说明：

若出现以下异常：\[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:repackage (default) on project maven-service: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:repackage failed: For artifact {cn.qlt:maven-service:0.0.1-SNAPSHOT:jar}: An attached artifact must have a different ID than its corresponding main artifact

 

请检查是否在spring-boot-maven-plugin插件中配置了finalname，若配置了该属性，请同时配置classifier。因为若不配置classifier且又设置了finalname导致了同一group和artifact中出现了两个jar包(我们生成的可执行的jar和普通的jar)，这样maven就不知道用哪个了

 

gradle：

```
buildscript {
    //引入spring-boot-gradle插件
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC1")
    }
}
//使用spring-boot插件
apply plugin: 'spring-boot'

springBoot {
        //配置入口类
    	mainClass = "main.Zone"
}
```

 

 

以上就是整理的打包可执行程序方法。若哪位兄弟知道其他更好的方法或者有好的建议，劳烦提出，谢谢