通过appassembler-maven-plugin生成的脚本无法在Spring Boot应用程序中找到主类 [英] Script generated via appassembler-maven-plugin is not able to find main class in Spring Boot application

查看:377
本文介绍了通过appassembler-maven-plugin生成的脚本无法在Spring Boot应用程序中找到主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用appassembler-maven-plugin生成的启动脚本有问题。
我有一个只有一个类的基本spring-boot应用程序:

I have a problem with the start script that I generate with appassembler-maven-plugin. I have a basic spring-boot application with only one class:

@SpringBootApplication
public class ScriptDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScriptDemoApplication.class, args);
    }
}

和我的pom.xml

and my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.home.sziolkow</groupId>
    <artifactId>script-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>script-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>
                                repackage
                            </goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.10</version>
                <configuration>
                    <goal>
                        package
                    </goal>
                    <showConsoleWindow>
                        true
                    </showConsoleWindow>
                    <platforms>
                        <platform>unix</platform>
                    </platforms>
                    <programs>
                        <program>
                            <mainClass>org.home.sziolkow.ScriptDemoApplication</mainClass>
                            <id>app</id>
                        </program>
                    </programs>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

我运行maven: mvn package appassembler:assemble

生成包和脚本但是当我尝试运行 ./ target / appassembler / bin / app ,我得到

Package and scripts are generated but when I try to run ./target/appassembler/bin/app, I get


错误:无法找到或加载主类org.home.sziolkow.ScriptDemoApplication

Error: Could not find or load main class org.home.sziolkow.ScriptDemoApplication

我测试了生成的包,我可以毫无问题地启动应用程序:

I tested generated packages and I can start the application without problems with:

java -jar ./target/appassembler/repo/org/home/sziolkow/script-demo/0.0.1-SNAPSHOT/script-demo-0.0.1-SNAPSHOT.jar 


推荐答案

由于Spring Boot重新打包JAR的方式,你遇到了麻烦它是一个可执行的JAR。来自文档

You're having this trouble because of the way Spring Boot is repackaging your JAR, in order to make it an executable JAR. From the documentation:


可执行存档不能用作依赖项,因为可执行的jar格式包 BOOT-INF / classes 中的应用程序类。这意味着当可执行jar用作依赖项时无法找到它们。

The executable archive cannot be used as a dependency as the exectuable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.

基本上,Spring Boot Maven插件重新打包你的JAR并将您的类放入 BOOT-INF / classes BOOT-INF / lib 中的所有JAR依赖项,并启动其 JarLauncher 类作为主类,它将在这些位置搜索类和JAR。

Essentially, the Spring Boot Maven Plugin repackages your JAR and puts your classes inside BOOT-INF/classes, all the JAR dependencies inside BOOT-INF/lib, and launches its JarLauncher class as main class, which will search for classes and JARs at those locations.

所以你有2解决方案:不要使用Spring Boot将JAR重新打包成可执行的JAR,或者根本不使用Appassembler插件。

So you have 2 solutions: do not use Spring Boot to repackage your JAR into an executable JAR, or do not use the Appassembler plugin at all.

由于Spring Boot为您创建了一个可执行的JAR,因此不需要使用Appassembler插件生成脚本。删除 appassembler-maven-plugin 的插件声明,并拥有:

Since Spring Boot creates an executable JAR for you, generating scripts with the Appassembler plugin is not necessary. Remove the plugin declaration for appassembler-maven-plugin and have:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <mainClass>org.home.sziolkow.ScriptDemoApplication</mainClass>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

POM的唯一变化是添加< mainClass> 指向主类的参数,以及 appassembler-maven-plugin 的删除。启动 mvn clean package 时,您将能够直接启动生成的可执行JAR:

The only change with your POM is the addition of the <mainClass> parameter pointing to your main class, and the removal of the appassembler-maven-plugin. When launching mvn clean package, you'll be able to launch the generated executable JAR directly with:

java -jar target/script-demo-0.0.1-SNAPSHOT.jar

如果可执行文件名称中的版本0.0.1-SNAPSHOT存在问题,您只需设置< finalName> 即可你的POM:

If the version 0.0.1-SNAPSHOT in the name of the executable is an issue, you can simply set a <finalName> in your POM:

<build>
  <finalName>${project.artifactId}</finalName>
  <!-- ... -->
</build>

然后使用 java -jar target / script启动可执行JAR -demo.jar

如前所述,自从Spring Boot repackage 目标将类和JAR放在 BOOT-INF 文件夹中,我们需要摆脱它。所以删除 spring-boot-maven-plugin 插件声明,并进入你的POM:

As said before, since Spring Boot repackage goal put the classes and JARs inside a BOOT-INF folder, we need to get rid of that. So remove the spring-boot-maven-plugin plugin declaration, and have in your POM:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>appassembler-maven-plugin</artifactId>
  <version>1.10</version>
  <executions>
    <execution>
      <id>assemble</id>
      <goals>
        <goal>assemble</goal>
      </goals>
      <phase>package</phase>
      <configuration>
        <showConsoleWindow>true</showConsoleWindow>
        <platforms>
          <platform>unix</platform>
        </platforms>
        <programs>
          <program>
            <mainClass>org.home.sziolkow.ScriptDemoApplication</mainClass>
            <id>app</id>
          </program>
        </programs>
      </configuration>
    </execution>
  </executions>
</plugin>

与当前POM的区别在于Appassembler绑定到阶段,以便在不调用 appassembler:assemble 的情况下启动执行。然后,当运行 mvn clean package 时,您将能够使用Appassembler生成的脚本启动您的应用程序:

The difference with your current POM is that the Appassembler is bound to the package phase, so that the execution is launched without invoking appassembler:assemble. Then, when running mvn clean package, you'll be able to start your application using the scripts generated by the Appassembler:

./target/appassembler/bin/app

这篇关于通过appassembler-maven-plugin生成的脚本无法在Spring Boot应用程序中找到主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆