简体   繁体   English

如何从spring maven项目创建可执行文件?

[英]How do i create executable from spring maven project?

I use Spring Tool Suite 4. We have a spring-boot API and we would like to create an executable (jar or war, no clue, which one i need). 我使用Spring Tool Suite 4.我们有一个spring-boot API,我们想创建一个可执行文件(jar或war,没有线索,我需要哪一个)。 We have dependencies stated in pom.xml. 我们在pom.xml中声明了依赖项。 By default this is what we have under build if you make a clean spring-boot application: 默认情况下,如果您创建一个干净的spring-boot应用程序,这就是我们正在构建的内容:

<plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>

So my question what do i have to add so i can actually execute maven install for my executable? 所以我的问题是什么,我必须添加,所以我可以实际执行maven install我的可执行文件? As of now if i run maven install (which for me the only way i know to create executable), it gives me an AssertionError. 截至目前,如果我运行maven install(这对我来说是我知道创建可执行文件的唯一方法),它会给我一个AssertionError。 I tried to find guides online, but none of them worked. 我试图在网上找到指南,但没有一个能奏效。

For now i would be happy if i could even manage to execute maven install, but my plan includes the usage of config file outside of the executable also i would like to log (log4j2) next to the jar file, so i need help with those too. 现在我很高兴,如果我甚至可以设法执行maven安装,但我的计划包括在可执行文件之外的配置文件的使用我也想在jar文件旁边记录(log4j2),所以我需要帮助那些太。 How to specify these in pom.xml so i can use config file from outside and to be able to log outside? 如何在pom.xml中指定这些,以便我可以从外部使用配置文件并能够在外面登录?

You'll want an executable jar. 你会想要一个可执行的jar。 Add this to your pom file: 将其添加到您的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>

So I got AssertionError, because for some reason some of my tests dont work for maven (even though if i execute them they pass). 所以我得到了AssertionError,因为出于某种原因,我的一些测试不适用于maven(即使我执行它们也会通过)。 It actually matters if jar or war. 如果罐子或战争真的很重要。 In my case it has to be a war because im making an API Also here are the minimum you have to change in pom.xml to have a chance to create executable: (this goes before dependencies inside the pom.xml) 在我的情况下,它必须是一场战争,因为我正在创建一个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>

The following part goes in between <build></build> . 以下部分介于<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>

To sum up I still have no idea what most of these do, but this works. 总而言之,我仍然不知道大多数这样做,但这是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM