szi*_*kow 5 java spring maven appassembler spring-boot
我使用 appassembler-maven-plugin 生成的启动脚本有问题。我有一个基本的 spring-boot 应用程序,只有一个类:
@SpringBootApplication
public class ScriptDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ScriptDemoApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
和我的 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>
Run Code Online (Sandbox Code Playgroud)
我用以下方式运行 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
Run Code Online (Sandbox Code Playgroud)
您之所以遇到这个问题,是因为 Spring Boot 重新打包 JAR 以使其成为可执行 JAR 的方式。从文档:
可执行存档不能用作依赖项,因为可执行 jar 格式将应用程序类打包在
BOOT-INF/classes. 这意味着当可执行 jar 用作依赖项时无法找到它们。
本质上,Spring Boot Maven 插件重新打包您的 JAR 并将您的类放入 中BOOT-INF/classes,将所有 JAR 依赖项放入 中BOOT-INF/lib,并将其JarLauncher类作为主类启动,它将在这些位置搜索类和 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>
Run Code Online (Sandbox Code Playgroud)
POM 的唯一变化是添加了<mainClass>指向主类的参数,并删除了appassembler-maven-plugin. 启动时mvn clean package,您将能够直接使用以下命令启动生成的可执行 JAR:
<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>
Run Code Online (Sandbox Code Playgroud)
如果可执行文件名称中的 0.0.1-SNAPSHOT 版本有问题,您只需<finalName>在 POM 中设置一个:
<build>
<finalName>${project.artifactId}</finalName>
<!-- ... -->
</build>
Run Code Online (Sandbox Code Playgroud)
然后可执行 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>
Run Code Online (Sandbox Code Playgroud)
与您当前的 POM 的不同之处在于 Appassembler 绑定到package阶段,因此无需调用appassembler:assemble. 然后,在运行时mvn clean package,您将能够使用 Appassembler 生成的脚本启动您的应用程序:
java -jar target/script-demo-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2624 次 |
| 最近记录: |