【问题标题】:Script generated via appassembler-maven-plugin is not able to find main class in Spring Boot application通过 appassembler-maven-plugin 生成的脚本无法在 Spring Boot 应用程序中找到主类
【发布时间】:2016-10-08 11:31:38
【问题描述】:

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

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

还有我的 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 时,我得到了

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

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

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

【问题讨论】:

    标签: java spring maven spring-boot appassembler


    【解决方案1】:

    由于 Spring Boot 重新打包 JAR 以使其成为可执行 JAR 的方式,您遇到了这个问题。来自the documentation

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

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

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

    没有 Appassembler

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

    <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 的唯一变化是添加了指向主类的&lt;mainClass&gt; 参数,并删除了appassembler-maven-plugin。启动 mvn clean package 时,您将能够直接启动生成的可执行 JAR:

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

    如果可执行文件名称中的 0.0.1-SNAPSHOT 版本存在问题,您只需在 POM 中设置 &lt;finalName&gt;

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

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

    使用 Appassembler

    如前所述,由于 Spring Boot repackage 目标将类和 JAR 放在 BOOT-INF 文件夹中,我们需要摆脱它。所以删除spring-boot-maven-plugin插件声明,并在你的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 绑定到 package 阶段,因此无需调用 appassembler:assemble 即可启动执行。然后,在运行mvn clean package 时,您将能够使用 Appassembler 生成的脚本启动您的应用程序:

    ./target/appassembler/bin/app
    

    【讨论】:

    • 太棒了!有用。我决定摆脱 spring-boot-maven-plugin。谢谢
    猜你喜欢
    • 2019-05-06
    • 2017-04-05
    • 2017-08-13
    • 2020-03-26
    • 1970-01-01
    • 2016-11-08
    • 2021-03-22
    • 2023-03-27
    • 2019-04-28
    相关资源
    最近更新 更多