通过 appassembler-maven-plugin 生成的脚本无法在 Spring Boot 应用程序中找到主类

2024-05-23

我使用 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, I get

错误:无法找到或加载主类 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 

您之所以遇到这个麻烦,是因为 Spring Boot 重新打包您的 JAR 的方式,以使其成为可执行的 JAR。从文档 http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/html/howto-build.html#howto-create-an-additional-executable-jar:

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

本质上,Spring Boot Maven 插件会重新打包您的 JAR 并将您的类放入其中BOOT-INF/classes,里面所有的JAR依赖BOOT-INF/lib,并推出其JarLauncher http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/api/org/springframework/boot/loader/JarLauncher.html类作为主类,它将在这些位置搜索类和 JAR。

所以你有 2 个解决方案:不要使用 Spring Boot 将你的 JAR 重新打包成可执行 JAR,或者根本不使用 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 的唯一变化是添加了<mainClass>指向您的主类的参数,并删除appassembler-maven-plugin。启动时mvn clean package,您将能够直接使用以下命令启动生成的可执行 JAR:

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

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

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

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

使用应用程序组装器

正如之前所说,自从 Spring Bootrepackage目标将类和 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 appassembler-maven-plugin 生成的脚本无法在 Spring Boot 应用程序中找到主类 的相关文章

随机推荐