我如何从 spring maven 项目创建可执行文件?
How do i create executable from spring maven project?
我使用 Spring Tool Suite 4。我们有一个 spring-boot API,我们想创建一个可执行文件(jar 或 war,不知道,我需要哪个)。我们有 pom.xml 中所述的依赖项。默认情况下,如果您制作一个干净的 spring-boot 应用程序,这就是我们正在构建的内容:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
所以我的问题是我必须添加什么才能真正为我的可执行文件执行 maven 安装?截至目前,如果我 运行 maven install(这对我来说是创建可执行文件的唯一方法),它会给我一个 AssertionError。我试图在网上查找指南,但其中 none 有效。
现在,如果我什至能够设法执行 maven 安装,我会很高兴,但我的计划包括在可执行文件之外使用配置文件,我还想在 jar 文件旁边记录 (log4j2),所以我也需要帮助。如何在 pom.xml 中指定这些以便我可以从外部使用配置文件并能够在外部登录?
您需要一个可执行的 jar。将此添加到您的 pom 文件中:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
specify.your.main.Class
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
所以我得到了 AssertionError,因为出于某种原因我的一些测试对 Maven 不起作用(即使我执行它们它们也会通过)。
jar 或 war 实际上很重要。在我的情况下,它必须是 war 因为我正在制作 API
此外,这里是您必须在 pom.xml 中更改才能创建可执行文件的最低限度:(这在 pom.xml 内的依赖项之前)
<packaging>war</packaging>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</properties>
下面的部分在<build></build>
之间。
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.class</mainClass>
</manifest>
</configuration>
</plugin>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>org.apache:apache-jar-resource-bundle:1.0</resourceBundle>
</resourceBundles>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
总而言之,我仍然不知道其中大部分是做什么的,但这很有效。
我使用 Spring Tool Suite 4。我们有一个 spring-boot API,我们想创建一个可执行文件(jar 或 war,不知道,我需要哪个)。我们有 pom.xml 中所述的依赖项。默认情况下,如果您制作一个干净的 spring-boot 应用程序,这就是我们正在构建的内容:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
所以我的问题是我必须添加什么才能真正为我的可执行文件执行 maven 安装?截至目前,如果我 运行 maven install(这对我来说是创建可执行文件的唯一方法),它会给我一个 AssertionError。我试图在网上查找指南,但其中 none 有效。
现在,如果我什至能够设法执行 maven 安装,我会很高兴,但我的计划包括在可执行文件之外使用配置文件,我还想在 jar 文件旁边记录 (log4j2),所以我也需要帮助。如何在 pom.xml 中指定这些以便我可以从外部使用配置文件并能够在外部登录?
您需要一个可执行的 jar。将此添加到您的 pom 文件中:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
specify.your.main.Class
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
所以我得到了 AssertionError,因为出于某种原因我的一些测试对 Maven 不起作用(即使我执行它们它们也会通过)。 jar 或 war 实际上很重要。在我的情况下,它必须是 war 因为我正在制作 API 此外,这里是您必须在 pom.xml 中更改才能创建可执行文件的最低限度:(这在 pom.xml 内的依赖项之前)
<packaging>war</packaging>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</properties>
下面的部分在<build></build>
之间。
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.class</mainClass>
</manifest>
</configuration>
</plugin>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>org.apache:apache-jar-resource-bundle:1.0</resourceBundle>
</resourceBundles>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
总而言之,我仍然不知道其中大部分是做什么的,但这很有效。