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包,可以根据需要选择一种合适的方法。

方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包

在pom.xml中配置:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-jar-plugin</artifactId>
  6. <version>2.6</version>
  7. <configuration>
  8. <archive>
  9. <manifest>
  10. <addClasspath>true</addClasspath>
  11. <classpathPrefix>lib/</classpathPrefix>
  12. <mainClass>com.xxg.Main</mainClass>
  13. </manifest>
  14. </archive>
  15. </configuration>
  16. </plugin>
  17. <plugin>
  18. <groupId>org.apache.maven.plugins</groupId>
  19. <artifactId>maven-dependency-plugin</artifactId>
  20. <version>2.10</version>
  21. <executions>
  22. <execution>
  23. <id>copy-dependencies</id>
  24. <phase>package</phase>
  25. <goals>
  26. <goal>copy-dependencies</goal>
  27. </goals>
  28. <configuration>
  29. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  30. </configuration>
  31. </execution>
  32. </executions>
  33. </plugin>
  34. </plugins>
  35. </build>

maven-jar-plugin用于生成META-INF/MANIFEST.MF文件的部分内容,<mainClass>com.xxg.Main</mainClass>指定MANIFEST.MF中的Main-Class,<addClasspath>true</addClasspath>会在MANIFEST.MF加上Class-Path项并配置依赖包,<classpathPrefix>lib/</classpathPrefix>指定依赖包所在目录。

例如下面是一个通过maven-jar-plugin插件生成的MANIFEST.MF文件片段:

  1. Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar
  2. Main-Class: com.xxg.Main

只是生成MANIFEST.MF文件还不够,maven-dependency-plugin插件用于将依赖包拷贝到<outputDirectory>${project.build.directory}/lib</outputDirectory>指定的位置,即lib目录下。

配置完成后,通过mvn package指令打包,会在target目录下生成jar包,并将依赖包拷贝到target/lib目录下,目录结构如下:

指定了Main-Class,有了依赖包,那么就可以直接通过java -jar xxx.jar运行jar包。

这种方式生成jar包有个缺点,就是生成的jar包太多不便于管理,下面两种方式只生成一个jar文件,包含项目本身的代码、资源以及所有的依赖包。

方法二:使用maven-assembly-plugin插件打包

在pom.xml中配置:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-assembly-plugin</artifactId>
  6. <version>2.5.5</version>
  7. <configuration>
  8. <archive>
  9. <manifest>
  10. <mainClass>com.xxg.Main</mainClass>
  11. </manifest>
  12. </archive>
  13. <descriptorRefs>
  14. <descriptorRef>jar-with-dependencies</descriptorRef>
  15. </descriptorRefs>
  16. </configuration>
  17. </plugin>
  18. </plugins>
  19. </build>

打包方式:

  1. mvn package assembly:single

打包后会在target目录下生成一个xxx-jar-with-dependencies.jar文件,这个文件不但包含了自己项目中的代码和资源,还包含了所有依赖包的内容。所以可以直接通过java -jar来运行。

此外还可以直接通过mvn package来打包,无需assembly:single,不过需要加上一些配置:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-assembly-plugin</artifactId>
  6. <version>2.5.5</version>
  7. <configuration>
  8. <archive>
  9. <manifest>
  10. <mainClass>com.xxg.Main</mainClass>
  11. </manifest>
  12. </archive>
  13. <descriptorRefs>
  14. <descriptorRef>jar-with-dependencies</descriptorRef>
  15. </descriptorRefs>
  16. </configuration>
  17. <executions>
  18. <execution>
  19. <id>make-assembly</id>
  20. <phase>package</phase>
  21. <goals>
  22. <goal>single</goal>
  23. </goals>
  24. </execution>
  25. </executions>
  26. </plugin>
  27. </plugins>
  28. </build>

其中<phase>package</phase>、<goal>single</goal>即表示在执行package打包时,执行assembly:single,所以可以直接使用mvn package打包。

不过,如果项目中用到spring Framework,用这种方式打出来的包运行时会出错,使用下面的方法三可以处理。

方法三:使用maven-shade-plugin插件打包

在pom.xml中配置:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-shade-plugin</artifactId>
  6. <version>2.4.1</version>
  7. <executions>
  8. <execution>
  9. <phase>package</phase>
  10. <goals>
  11. <goal>shade</goal>
  12. </goals>
  13. <configuration>
  14. <transformers>
  15. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  16. <mainClass>com.xxg.Main</mainClass>
  17. </transformer>
  18. </transformers>
  19. </configuration>
  20. </execution>
  21. </executions>
  22. </plugin>
  23. </plugins>
  24. </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来对文件内容追加合并:

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.plugins</groupId>
    5. <artifactId>maven-shade-plugin</artifactId>
    6. <version>2.4.1</version>
    7. <executions>
    8. <execution>
    9. <phase>package</phase>
    10. <goals>
    11. <goal>shade</goal>
    12. </goals>
    13. <configuration>
    14. <transformers>
    15. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    16. <mainClass>com.xxg.Main</mainClass>
    17. </transformer>
    18. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    19. <resource>META-INF/spring.handlers</resource>
    20. </transformer>
    21. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    22. <resource>META-INF/spring.schemas</resource>
    23. </transformer>
    24. </transformers>
    25. </configuration>
    26. </execution>
    27. </executions>
    28. </plugin>
    29. </plugins>
    30. </build>

自己代码中的shade+assembly:

pom.xml:

         <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.17.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<createDependencyReducedPom>false</createDependencyReducedPom>
<finalName>demo-web-server-shade</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.my.demo.httpserver.WebServerApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assemble</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>

assembly.xml:

 <?xml version="1.0" encoding="utf-8"?>

<assembly>
<id>assembly</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<!-- <fileSet>
<directory>src/main/resources/bin</directory>
<includes>
<include>start.sh</include>
<include>env.sh</include>
<include>stop.sh</include>
</includes>
<fileMode>0755</fileMode>
<outputDirectory>/</outputDirectory>
</fileSet> -->
<fileSet>
<directory>src/main/resources/configsec</directory>
<includes>
<include>important.properties</include>
</includes>
<outputDirectory>configsec/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources/lib</directory>
<includes>
<include>quasar-core-0.7.9-jdk8.jar</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<files>
<file>
<source>${project.build.directory}/demo-web-server-shade.jar</source>
<outputDirectory>/</outputDirectory>
</file>
</files>
</assembly>

转自:https://blog.csdn.net/daiyutage/article/details/53739452

参考资料:

https://yq.aliyun.com/articles/308777

maven-shade-plugin:https://my.oschina.net/u/2377110/blog/1584205

maven-assembly-plugin:https://www.jianshu.com/p/14bcb17b99e0 https://blog.csdn.net/liupeifeng3514/article/details/79777976

Maven打包的三种方式的更多相关文章

  1. Maven打包的三种方式(转)

    Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...

  2. idea执行maven命令的三种方式

    前言: java开发的IDE工具idea默认会提供maven生命周期的图形化执行,但是如果我们需要定制化的执行命令的时候,就需要使用手动执行maven命令的方式,今天就和大家讲一下idea手动执行ma ...

  3. Maven打jar包的三种方式

    Maven打jar包的三种方式 不包含依赖jar包 该方法打包的jar,不包含依赖的jar包,也没有指定入口类. <build> <plugins> <plugin> ...

  4. (转)maven怎么 引入(或引用/使用) 自定义(或本地/第三方) jar的三种方式 图文教程 方法二最简单

    转:https://blog.csdn.net/wabiaozia/article/details/52798194 准备工作: 假如我有一个自定义jar是:123456.jar,下载地址http:/ ...

  5. Maven中解决jar包冲突的三种方式

    首先我们在idea中创建一个maven工程,我们只关注pom.xml以及External Libraries中导入的jar包 导入spring-beans.jar <dependency> ...

  6. dubbo服务运行的三种方式

    dubbo服务运行,也就是让生产服务的进程一直启动.如果生产者进程挂掉,也就不存在生产者,消费者不能进行消费. Dubbo服务运行的三种方式如下:1.使用Servlet容器运行(Tomcat.Jett ...

  7. Hadoop Hive概念学习系列之hive三种方式区别和搭建、HiveServer2环境搭建、HWI环境搭建和beeline环境搭建(五)

     说在前面的话 以下三种情况,最好是在3台集群里做,比如,master.slave1.slave2的master和slave1都安装了hive,将master作为服务端,将slave1作为服务端. 以 ...

  8. 三种方式上传文件-Java

    前言:负责,因为该项目他(jetty嵌入式开始SpringMvc)实现文件上传的必要性,并拥有java文件上传这一块还没有被曝光.并 Http 更多晦涩协议.因此,这种渐进的方式来学习和实践上载文件的 ...

  9. 在Linux安装配置Tomcat 并部署web应用 ( 三种方式 )

    系统版本:centos6.5版本 java版本:1.7 一.准备工作 1.java -version 检查是否有java环境,没有则需要去安装并配置到环境变量中. 2.下载tomcat包,下载地址:h ...

随机推荐

  1. chrome-解决该扩展程序未列在 Chrome 网上应用店中

    1.win10添加策略组 复制以下内容到.bat文件中,右键-以管理员身份运行,即可添加策略组 pushd "%~dp0" dir /b C:\Windows\servicing\ ...

  2. Oralce 学习笔记

    1. Oracle 数据库文件后缀是什么格式? 数据文件是以oracle自定义的格式存储的,没有固定的后缀名,一般通用的为.dbf和.ora而默认是dbf的 2.Database Configurat ...

  3. 【树形DP】【P3177】[HAOI2015] 树上染色

    Description 给定一棵 \(n\) 个点的带权树,要求选 \(k\) 个点染成黑色,剩下染成白色,最大化两两同色点之间的距离和. Limitations \(0 \leq k \leq n ...

  4. SqlServer 快速查看表结构

    --快速查看表结构(比较全面的) THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 , col.name AS 列名 , ISNULL(ep.[v ...

  5. MybatisPlus使用代码生成器遇到的小问题

    MyBatisPlus 在3.0.3版本之前使用代码生成器因为存在默认依赖,所以不需要其他的依赖,项目中使用的是3.0.1的版本,所以不用添加其他依赖,添加之后反倒是会报错,实际上MP官网上已经说明了 ...

  6. 2018-2019-2 20165313 《网络对抗技术》 Exp 9 Web安全基础

    一.实验要求 本实践的目标理解常用网络攻击技术的基本原理,做不少于7个题目,共3.5分.包括(SQL,XSS,CSRF).Webgoat实践下相关实验. 二.实验问题回答 (1)SQL注入攻击原理,如 ...

  7. 【Gamma】“北航社团帮”发布说明——小程序v3.0

    目录 Gamma版本新功能 小程序v3.0新功能 新功能列表 新功能展示 这一版修复的缺陷 Gamma版本的已知问题和限制 小程序端 网页端 运行.安装与发布 运行环境的要求 安装与发布 小程序 网页 ...

  8. pip: failed to create process.解决方法

    昨天在使用pip过程,pip提示:failed to create process. 解决方法:python -m pip install xxx 就可以了 如以matplotlib为例即:pytho ...

  9. Idea 设置单击打开文件或者双击打开文件、自动定位文件所在的位置

    以下定位,框架下,打开文件的点击方式,以及点击标签导航页上的已打开文件定位到展开路径,如下图:

  10. 浅析PHP框架Laravel最新SQL注入漏洞

    PHP知名开发框架Laravel,之前在官方博客通报了一个高危SQL注入漏洞,这里简单分析下. 首先,这个漏洞属于网站coding写法不规范,官方给了提示: 但官方还是做了修补,升级最新版本V5.8. ...