Maven 打包工具从 maven-jar-plugin 改成 maven-dependency-plugin 后 Wrapper 无法启动的问题

Posted by 佳佳 on 2018-08-22 33 IT Java Maven Wrapper

修改前使用 maven-jar-plugin 插件打包,maven-dependency-plugin 插件将依赖拷贝到 /lib 目录下。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.mycompany.soa.api.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>

            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive>
                <stripVersion>false</stripVersion>
            </configuration>

            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                        <includeScope>compile</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

修改后使用 spring-boot-maven-plugin 插件打包,只生成一个 jar 包,所有的依赖都会打包在该 jar 包中。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

修改使用 java 命令可以成功启动站点 Spring Boot ,但是在测试环境中有使用 wrapper 打包成服务后始终无法成功启动。

wrapper.log 中的日志如下:

ERROR  | wrapper  | 2018/08/21 15:39:27 | JVM exited while loading the application.
ERROR  | wrapper  | 2018/08/21 15:39:33 | JVM exited while loading the application.
ERROR  | wrapper  | 2018/08/21 15:39:40 | JVM exited while loading the application.
ERROR  | wrapper  | 2018/08/21 15:39:47 | JVM exited while loading the application.
ERROR  | wrapper  | 2018/08/21 15:39:53 | JVM exited while loading the application.
FATAL  | wrapper  | 2018/08/21 15:39:54 | There were 5 failed launches in a row, each lasting less than 300 seconds.  Giving up.
FATAL  | wrapper  | 2018/08/21 15:39:54 |   There may be a configuration problem: please check the logs.

由于 jar 包单独可以执行,再根据日志,应该是配置文件 wrapper.conf 中的设置有误导致的。

执行 wrapper 目录的 App.bat 控制台显示如下信息:

wrapper  | Launching a JVM...
jvm 2    | WrapperManager: Initializing...
jvm 2    | WrapperSimpleApp Error: Unable to locate the class com.mycompany.soa.api.Application : java.lang.ClassNotFoundException: com.mycompany.soa.api.Application
jvm 2    |
jvm 2    | WrapperSimpleApp Usage:
jvm 2    |   java org.tanukisoftware.wrapper.WrapperSimpleApp {app_class{/app_method}} [app_arguments]
jvm 2    |
jvm 2    | Where:
jvm 2    |   app_class:      The fully qualified class name of the application to run.
jvm 2    |   app_arguments:  The arguments that would normally be passed to the
jvm 2    |                   application.
jvm 2    | Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
wrapper  | JVM exited while loading the application.

日志中显示找不到 com.mycompany.soa.api.Application 类,但这个类肯定是打包进去了的。
这个类名是在配置文件中的 wrapper.app.parameter 中配置的,用来设置 jar 包的 main class

# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=com.octopus.soa.api.Application

WinRAR 打开 jar 包查看,发现结构变化了好多,MANIFEST.MF 文件中的内容也变了很多。

使用 spring-boot-maven-plugin 插件打包的 jar 文件的结构 和 MANIFEST.MF 文件内容如下。

example.jar
 |
 +-META-INF
 |  +-MANIFEST.MF
 +-org
 |  +-springframework
 |     +-boot
 |        +-loader
 |           +-<spring boot loader classes>
 +-BOOT-INF
    +-classes
    |  +-mycompany
    |     +-project
    |        +-YourClasses.class
    +-lib
       +-dependency1.jar
       +-dependency2.jar

MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: Api
Implementation-Version: 1.0-SNAPSHOT
Built-By: liujiajia
Implementation-Vendor-Id: com.mycompany
Spring-Boot-Version: 2.0.4.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.mycompany.soa.api.Application
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.0.5
Build-Jdk: 1.8.0_101
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-boot-starter-parent/mycompany/Api

根据 MANIFEST.MF 新打的 jar 包的 Main-Class 已经变成了 org.springframework.boot.loader.JarLauncher。将 wrapper.app.parameter.1 的值改了之后就可以正常启动了。

# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=org.springframework.boot.loader.JarLauncher

再贴下使用 maven-jar-plugin 插件打包的 jar 文件的结构 和 MANIFEST.MF 文件内容,做下对比。

example.jar
 |
 +-application.properties
 +-META-INF
 |  +-MANIFEST.MF
 |  +-maven
 |     +-mycompany
 |        +-project
 |           +-pom.properties
 |           +-pom.xml
 +-mycompany
    +-project
    |  +-YourClasses.class

MANIFEST.MF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: liujiajia
Build-Jdk: 1.8.0_101
Main-Class: com.mycompany.soa.api.Application
Class-Path: lib/spring-boot-starter-web-1.4.0.RELEASE.jar lib/spring-boot-starter-1.4.0.RELEASE.jar
 ···
 lib/jboss-logging-3.2.1.Final.jar

参考

  1. MANIFEST.MF: difference between Main-Class and Start-Class
  2. Appendix E. The Executable Jar Format
版权声明:本文为博主原创文章,未经博主允许不得转载。http://www.liujiajia.me/blog/details/88a143ac-d378-4c4f-807d-1735f54d1b18

Buy me a coffeeBuy me a coffee