简体   繁体   English

spring-boot Maven:如何使用主类创建可执行 jar?

[英]spring-boot Maven: How to create executable jar with main class?

Recently, I wrote a Spring-Boot project and I wanted that Maven will be able to create a jar file that I'll be able to run by the command "java -jar ".最近,我写了一个 Spring-Boot 项目,我希望 Maven 能够创建一个 jar 文件,我将能够通过命令“java -jar”运行该文件。

This is the pom.xml I wrote:这是我写的 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>SpringBootGame</groupId>
    <artifactId>SpringBootGame</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>    
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.3.RELEASE</version>
                <configuration>
                    <mainClass>com.game.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

In order to build the jar file, I had to run the command: "mvn clean package spring-boot:repackage".为了构建 jar 文件,我必须运行命令:“mvn clean package spring-boot:repackage”。

My questions are:我的问题是:

  1. Why must I add spring-boot-maven-plugin?为什么我必须添加 spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?我已经有了 spring-boot-starter-web 依赖项,它将 spring-boot 添加为依赖项,以及从代码构建 jar 文件的 maven-compiler-plugin?

  2. Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?您能想出一种配置 pom.xml 文件的方法,我将能够使用“本机”命令“mvn clean package”而不是繁琐的“mvn clean package spring-boot:repackage”来获取 jar 文件吗?

Thanks谢谢

Why must I add spring-boot-maven-plugin?为什么我必须添加 spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?我已经有了 spring-boot-starter-web 依赖项,它将 spring-boot 添加为依赖项,以及从代码构建 jar 文件的 maven-compiler-plugin?

Because the plugin is what adds Maven support for Spring Boot因为该插件为 Spring Boot 添加了 Maven 支持

Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?您能想出一种配置 pom.xml 文件的方法,我将能够使用“本机”命令“mvn clean package”而不是繁琐的“mvn clean package spring-boot:repackage”来获取 jar 文件吗?

It looks like you are missing the <packaging>jar</packaging> element in between your <project> </project> element.看起来您在<project> </project>元素之间缺少<packaging>jar</packaging> <project> </project>元素。

The reason you had to run that lengthy command, is because you did not include the <executions></executions> element when including the plugin.您必须运行该冗长命令的原因是,您在包含插件时没有包含<executions></executions>元素。 Please see section 71.1 of the following docs:请参阅以下文档的第 71.1 节:

https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html#build-tool-plugins-include-maven-plugin https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html#build-tool-plugins-include-maven-plugin

71.2 elaborates on the <packaging> element. 71.2 详细说明了<packaging>元素。

In the configuration of the maven plugin you have already in place, you need to add the executable block like :在您已经准备好的 Maven 插件配置中,您需要添加如下可执行块:

<configuration>
   <executable>true</executable>
</configuration>

this will create in the target folder the runnable jar that can be run by using java -jar command.这将在目标文件夹中创建可以使用 java -jar 命令运行的可运行 jar。

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

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