Springboot jar主清单属性丢失解决方案

作者:Zs夏至 时间:2022-04-06 05:30:26 

在开发中,用到springboot项目,当打包后部署运行时,出现了这个问题,网上搜了好多,又是加META-INF配置,又是加啥的,感觉springboot这么方便,这种问题怎么可能会搞这么复杂,于是研究了一下:

首先我们项目要依赖springboot的parent或者引入spring-boot-dependencies

Springboot jar主清单属性丢失解决方案

或者

Springboot jar主清单属性丢失解决方案

这样就将springboot的pom文件导入了我们的项目,然后还要再要运行的jar包中写入插件:

Springboot jar主清单属性丢失解决方案

当使用继承spring-boot-starter-parent时,就会出现标志,表示是继承自父类的,然后我们点到spring-boot-starter-parent的pom文件中,查看插件部分:


<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
     <execution>
      <id>repackage</id>
      <goals>
       <goal>repackage</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <mainClass>${start-class}</mainClass>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <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>${start-class}</mainClass>
        </transformer>
       </transformers>
      </configuration>
     </execution>
    </executions>
    <dependencies>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.1.12.RELEASE</version>
     </dependency>
    </dependencies>
    <configuration>
     <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
     <createDependencyReducedPom>true</createDependencyReducedPom>
     <filters>
      <filter>
       <artifact>*:*</artifact>
       <excludes>
        <exclude>META-INF/*.SF</exclude>
        <exclude>META-INF/*.DSA</exclude>
        <exclude>META-INF/*.RSA</exclude>
       </excludes>
      </filter>
     </filters>
    </configuration>
   </plugin>

注意到里面有一个${start-class}变量,这个变量在parent的pom文件中并没有定义,那么我们就在自己要打jar包运行的模块定义这个变量:

Springboot jar主清单属性丢失解决方案

然后再打包,就可以直接通过java -jar *.jar 运行项目了

如果不是继承自parnetxml,而是选择第一种,导入dependencies的方式:

Springboot jar主清单属性丢失解决方案

那么就要改一下前面的spring-boot-maven-plugin插件,

Springboot jar主清单属性丢失解决方案

我们需要指定打包路径的main方法,这样就可以直接打包通过 java -jar *.jar的方式运行了

重要的是一定要定义start-class变量

来源:https://www.cnblogs.com/Zs-book1/p/12457356.html

标签:Spring,boot,jar,主清单,属性
0
投稿

猜你喜欢

  • jsp、struts、spring、mybatis实现前端页面功能模块化拆分的方案

    2023-11-25 07:38:00
  • 详解Java中多进程编程的实现

    2021-12-22 01:46:06
  • Java设计模式之单例和原型

    2023-11-29 04:14:18
  • JAVA基础之一些不为人知的那些秘密

    2023-11-29 07:49:29
  • springboot2.x 接入阿里云市场短信发送的实现

    2023-09-20 23:03:57
  • Java设计模式初识之备忘录模式详解

    2023-08-29 23:27:09
  • Java常用内置注解用法分析

    2023-11-24 04:53:46
  • spring boot如何实现切割分片上传

    2023-06-29 02:49:26
  • 基于Android中获取资源的id和url方法总结

    2023-06-20 06:05:21
  • 使用SSM+Layui+Bootstrap实现汽车维保系统的示例代码

    2023-11-28 18:30:04
  • 详解Java线程同步器CountDownLatch

    2023-08-23 18:42:39
  • 一文精通Java中的volatile关键字

    2023-11-24 04:17:14
  • Opencv EigenFace人脸识别算法详解

    2023-07-21 19:30:17
  • JAVA反射机制实例教程

    2023-11-25 23:33:47
  • 快速了解hibernate配置文件与映射文件

    2023-11-04 23:02:26
  • IntelliJ IDEA 2017 汉化包及图文教程

    2023-11-25 12:17:58
  • 使用Spring Boot进行单元测试详情

    2023-11-10 08:01:53
  • JAVA操作MongoDB数据库实例教程

    2023-11-18 13:22:27
  • 25行Java代码将普通图片转换为字符画图片和文本的实现

    2023-11-24 02:04:26
  • java 引用传递的三种类型小结

    2023-09-03 03:59:47
  • asp之家 软件编程 m.aspxhome.com