【问题标题】:spring-boot-maven-plugin doesn't create fat jarspring-boot-maven-plugin 不会创建胖 jar
【发布时间】:2018-09-10 10:35:49
【问题描述】:

我正在使用spring-boot-maven-plugin 来打包我的 REST 服务。我正在使用mvn clean installmvn clean package 构建罐子。在我反编译 jar 之后,我没有找到任何添加的依赖项(我原以为它是一个包含所有依赖项的胖 jar)

 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.9.RELEASE</version>
    <executions>
        <execution>
           <phase>install</phase>
           <goals>
              <goal>repackage</goal>
              <goal>build-info</goal>
           </goals>
        </execution>
    </executions>
    <configuration>
        <executable>true</executable>
        <finalName>myapp</finalName>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

当我使用 java -jar myapp.jar -Drun.jvmArguments="-Dspring.profiles.active=qal" 运行 spring boot 时,我得到了许多类的 ClassNotFoundException。很明显,artifact 没有按预期构建。但是,如果我使用 maven ./mvnw spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=qal" 启动 Spring Boot 应用程序,我猜它会在目标文件夹中找到所有依赖项,因此可以正常工作。如何解决构建问题,以便我可以使用 java -jar 命令启动应用程序。

编辑:这是一个多模块的 maven 项目

【问题讨论】:

  • 从截图来看,重新打包的jar有很多问题。截图出自哪里?另外,像jarunzip 这样的命令行工具会为jar 的内容显示什么?
  • 我使用JD-GUI (jd.benow.ca) 解压jar,截图是使用jd-gui解压后的
  • jar 和 unzip 等命令行工具对 jar 的内容显示什么?
  • Jars 只是带有清单的 zip 文件,因此您可以使用 unzip 来查看内容。即 unzip -l 将显示内容。如果您知道列表中的路径,您也可以仔细阅读内容。即这将打印清单: unzip -p target/scila-adapter-service.jar META-INF/MANIFEST.MF

标签: java maven spring-boot spring-boot-maven-plugin


【解决方案1】:

您似乎使用了错误的命令。 mvn clean package 是 maven 命令,你应该使用命令 'repackage',它用于

重新打包现有的 JAR 和 WAR 档案,以便它们可以执行 从命令行使用 java -jar

正如这里提到的https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html

或者可能是插件配置问题。刚刚检查:它适用于spring-boot-maven-plugin-2.0.0.RELEASE

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                 <classifier>exec</classifier>
            </configuration>
         </execution>
    </executions>
</plugin>

【讨论】:

  • 我使用了“mvn clean package spring-boot:repackage”。我看到所有项目类都已编译并添加到 jar 中,但仍然缺少依赖项,并且我继续收到依赖项的 ClassNotFound 错误
  • 找到它不起作用的原因。我的项目是多模块 Maven 项目。需要按照本文档使用分类器。它也适用于 1.5.9.RELEASE 版本。 docs.spring.io/spring-boot/docs/current/maven-plugin/examples/…
  • 答案中发布的链接已损坏
【解决方案2】:

用这个

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>${start-class}</mainClass>
    <executable>true</executable>
    <fork>true</fork>
    <!-- Enable the line below to have remote debugging of your application on port 5005
         <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
     -->
  </configuration>
</plugin>

【讨论】:

  • 您好,请简要说明此代码为何或如何解决 OPs 问题。
  • 答案没有解释原始问题将得到什么以及如何解决。
猜你喜欢
  • 2018-07-15
  • 2018-03-18
  • 2018-02-23
  • 2019-03-15
  • 2014-04-01
  • 2018-12-14
  • 2023-03-24
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多