问题在Spring Boot Maven项目中创建一个薄Jar。

huangapple go评论94阅读模式
英文:

Problem creating a thin jar in Spring Boot Maven project

问题

我有一个使用Maven构建的Spring Boot微服务Java项目,它被组织成一个父模块和6个子模块。在父模块的pom.xml文件中,我已经在build/plugins部分包含了Maven Spring Boot插件:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.1.5.RELEASE</version>
</plugin>

我的6个子模块中有5个是微服务,上面的插件确保在运行 mvn clean install 时,它们会被构建成可执行的Spring Boot可执行jar包,包括所有依赖项。

然而,另一个子模块只是一个标准的Java实用项目,没有Spring Boot上下文。当我尝试构建时,我看到以下错误:

Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to find main class -> [Help 1]

这是预期的,因为这个子模块不是一个Spring Boot应用程序,也没有主类。我尝试通过在该子模块的pom文件中覆盖Spring Boot Maven插件来修复这个问题,以便将其视为标准的 'thin' jar 包:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <dependencies>
        <!-- 以下启用了 "thin jar" 部署选项。 -->
        <!-- 这将创建一个普通的jar文件,而不是可执行的Spring Boot jar文件。 -->
        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-thin-layout</artifactId>
            <version>1.0.11.RELEASE</version>
        </dependency>
    </dependencies>
</plugin>

然而,我在我的Maven构建中仍然看到完全相同的错误。对于这个问题,我将非常感激任何提示或指导。

英文:

I have a Maven Spring Boot Micro Services Java project arranged into a parent module and 6 sub-modules. In the parent pom.xml, I have included the Maven Spring Boot Plugin in the build/plugins section: -

    &lt;plugin&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.1.5.RELEASE&lt;/version&gt;
    &lt;/plugin&gt;

5 of my 6 sub-modules are Micro Services, and the above plugin ensures that these are built into executable Spring Boot jars, including all dependencies, when I run mvn clean install

However, the other sub-module is just a standard Java utility project and does not have a Spring Boot context. When I try to build, I see the following error: -

 Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to find main class -&gt; [Help 1]

This is expected as this sub-module is not a Spring Boot application and does not have a main class. I tried to fix this by overriding the Spring Boot Maven plugin in the pom file of that sub-module so that it is treated as a standard 'thin' jar: -

        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;dependencies&gt;
                &lt;!-- The following enables the &quot;thin jar&quot; deployment option. --&gt;
                &lt;!-- This creates a normal jar, not an executable springboot jar --&gt;
                &lt;dependency&gt;
                    &lt;groupId&gt;org.springframework.boot.experimental&lt;/groupId&gt;
                    &lt;artifactId&gt;spring-boot-thin-layout&lt;/artifactId&gt;
                    &lt;version&gt;1.0.11.RELEASE&lt;/version&gt;
                &lt;/dependency&gt;
            &lt;/dependencies&gt;
        &lt;/plugin&gt;

However, I am still seeing exactly the same error in my Maven build. I would be most grateful for any hints or pointers on this one.

答案1

得分: 1

你可以通过在该模块中覆盖配置来跳过实用模块的重新打包插件的执行。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>
英文:

You can skip the execution of the repackage plugin for your utility module by overriding the configuration in that module.

&lt;plugin&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;skip&gt;true&lt;/skip&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

huangapple
  • 本文由 发表于 2020年7月30日 19:23:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定