欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-02-25 10:38:58|栏目:JAVA代码|点击:

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

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

或者

这样就将springboot的pom文件导入了我们的项目,然后还要再要运行的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包运行的模块定义这个变量:

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

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

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

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

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

上一篇:httpclient模拟post请求json封装表单数据的实现方法

栏    目:JAVA代码

下一篇:java中匿名内部类解读分析

本文标题:Springboot jar主清单属性丢失解决方案

本文地址:http://www.codeinn.net/misctech/69796.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有