Maven将代码及依赖打成一个Jar包的方式详解(最新推荐)
作者:章三丰 发布时间:2022-03-31 06:52:47
Maven可以使用mvn package指令对项目进行打包,如果使用java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in xxx.jar"(没有设置Main-Class)、ClassNotFoundException(找不到依赖包)等错误。
要想jar包能直接通过java -jar xxx.jar运行,需要满足:
1、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,这样才能确定程序的入口在哪里;
2、要能加载到依赖包。
使用Maven有以下几种方法可以生成能直接运行的jar包并且是打成一个jar包,可以根据需要选择一种合适的方法。
方法一:使用maven-assembly-plugin插件打包
在pom.xml中配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
打包方式: mvn package assembly:single(在eclipse中执行maven build…… ,然后在Goals中输入 assembly:single)
打包后会在target目录下生成一个xxx-jar-with-dependencies.jar文件,这个文件不但包含了自己项目中的代码和资源,还包含了所有依赖包的内容。所以可以直接通过java -jar来运行。
此外还可以直接通过mvn package来打包(在eclipse中直接执行maven install),无需assembly:single,不过需要加上一些配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<!-- 这个jar-with-dependencies是assembly预先写好的一个,组装描述引用 -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!--工程名-->
<finalName>${project.name}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
其中<phase>package</phase>、<goal>single</goal>即表示在执行package打包时,执行assembly:single,所以可以直接使用mvn package打包。
不过,如果项目中用到Spring Framework,用这种方式打出来的包运行时会出错,使用下面的方法二可以处理。
方法二:使用maven-shade-plugin插件打包
在pom.xml中配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.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.xxg.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
配置完成后,执行mvn package即可打包。在target目录下会生成两个jar包,注意不是original-xxx.jar文件,而是另外一个。和maven-assembly-plugin一样,生成的jar文件包含了所有依赖,所以可以直接运行。
如果项目中用到了Spring Framework,将依赖打到一个jar包中,运行时会出现读取XML schema文件出错。原因是Spring Framework的多个jar包中包含相同的文件spring.handlers和spring.schemas,如果生成一个jar包会互相覆盖。为了避免互相影响,
可以使用AppendingTransformer来对文件内容追加合并:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.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.xxg.Main</mainClass>
</transformer>
<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>
</plugins>
</build>
方法三:使用maven-jar-plugin和maven-dependency-plugin插件打包
原理是通过修改maven 打jar包的maven-jar-plugin插件的配置信息来生成我们需要的指定依赖的可执行jar包。
在项目的pom.xml文件中修改默认的jar插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<!--运行jar包时运行的主类,要求类全名-->
<mainClass>com.hafiz.Runner</mainClass>
<!-- 是否指定项目classpath下的依赖 -->
<addClasspath>true</addClasspath>
<!-- 指定依赖的时候声明前缀 -->
<classpathPrefix>./lib/</classpathPrefix>
<!--依赖是否使用带有时间戳的唯一版本号,如:xxx-1.3.0-20121225.012733.jar-->
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<!--接着我们还要配置maven的maven-dependency-plugin插件把当前项目的所有依赖放到target目录下的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>
其中,${project.build.directory}表示默认的target文件夹。
我们通过上文的修改便完成了适用maven生成指定依赖的可执行jar包。
我们发现生成的manifest文件中已经设置好了Main-Class以及Class-Path,如下:
如果<addClasspath>设置为false,则生成的manifest文件中不会声明依赖(即不会有Class-Path声明)
程序主类如下:
package com.hafiz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Desc:主类
* Created by hafiz.zhang on 2018/4/07.
*/
public class Runner {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("context:" + context.getClass());
System.out.println("The Main Class Is Running....");
}
}
pom.xml文件如下:
<project 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/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hafiz</groupId>
<artifactId>assembly-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>assembly-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<!--运行jar包时运行的主类,要求类全名-->
<mainClass>com.hafiz.Runner</mainClass>
<!-- 是否指定项目classpath下的依赖 -->
<addClasspath>true</addClasspath>
<!-- 指定依赖的时候声明前缀 -->
<classpathPrefix>./lib/</classpathPrefix>
<!--依赖是否使用带有时间戳的唯一版本号,如:xxx-1.3.0-20121225.012733.jar-->
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
<!--把当前项目所有的依赖打包到target目录下的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>
<!--已存在的Release版本不重复copy-->
<overWriteReleases>false</overWriteReleases>
<!--已存在的SnapShot版本不重复copy-->
<overWriteSnapshots>false</overWriteSnapshots>
<!--不存在或者有更新版本的依赖才copy-->
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我们现在进入生成的jar包所在的文件夹下,使用 java -jar xxx.jar来执行生成的jar包
到此我们就完成了如何使用maven的jar包生成插件来进行生成指定依赖的可执行jar包。
代码Github地址:https://github.com/hafizzhang/assembly-demo.git
参考网址:
https://blog.csdn.net/xiao__gui/article/details/47341385
http://www.cnblogs.com/hafiz/p/6538107.html
https://www.cnblogs.com/hafiz/p/6538332.html
来源:https://www.cnblogs.com/zhangwuji/p/10040834.html


猜你喜欢
- 基础知识介绍: @RequestBody主要用来接收前端传递给后端的json字符串中的
- 在项目中,需要使用XStream将xml string转成相应的对象,却报出了java.lang.ClassCastException: c
- 累加数累加数 是一个字符串,组成它的数字可以形成累加序列。一个有效的 累加序列 必须 至少 包含 3 个数。除了最开始的两个数以外,序列中的
- 一. struts2读取进度原理分析(作为草稿存了好久,刚刚发布出来......)1.在strut2中控制文件上传信息的类是实现MultiP
- 前言:IntelliJ IDEA 如果说IntelliJ IDEA是一款现代化智能开发工具的话,Eclipse则称得上是石器时代的东西了。其
- 结论:HashMap对象的key、value值均可为null。HahTable对象的key、value值均不可为null。且两者的的key值
- 什么是字符串字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。它是编程
- 本文实例为大家分享了android studio实现简单计算器的具体代码,供大家参考,具体内容如下布局:<?xml version=&
- 在JAVA克隆对象不能简单的使用clone方法,clone方法只是进行浅克隆。请看下方:深度克隆类:Java代码 import java.i
- 本文实例讲述了C#获取网页源代码的方法。分享给大家供大家参考。具体如下:public string GetPageHTML(string u
- 玩安卓的人都知道adb,玩adb的人都知道install和uninstall,但是为什么adb shell pm install packa
- 1.简单介绍一下NDK和JNINDK:NDK是Native Development Kit的缩写,是Google提供的一套工具集
- 本文实例讲述了Android实现便于批量操作可多选的图片ListView。分享给大家供大家参考,具体如下:之前项目需要实现一个可多选的图片列
- 本文实例讲述了Java数组的定义、初始化、及二维数组用法。分享给大家供大家参考,具体如下:数组的定义1.数组是有序数据的集合,数组中的每个元
- 最近由于项目开发使用到了动态布局,因为打包sdk ,sdk 这块activity 需要一些layout 文件 。而做过sdk 开发的小伙伴应
- @ModelAttribute与@RequestBody的区别最近在写代码的过程中,发现之前项目都是使用的@ModelAttribute注解
- FormClosing事件在窗体关闭时,FormClosing事件发生。此事件会得到处理。从而释放与窗体相关的所有资源。如果取消此事件,则窗
- 一、实现方式@ConfigurationProperties 注解(最好加上前缀prefix=“person”,标明是和配置文件中哪个开头的
- 布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去
- 简介:本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件思路:主要重写Recyclerview