【问题标题】:How to use spring-boot-maven-plugin to create a startup jar file in a multi-module project如何在多模块项目中使用spring-boot-maven-plugin创建启动jar文件
【发布时间】:2021-11-04 09:45:39
【问题描述】:

我需要使用 spring-boot-maven-plugin 为我的项目构建一个 jar 文件。当我构建一个没有模块的项目时,一切都解决了。我决定尝试同样的事情,但已经在一个多模块项目中。结果,我收到了这个错误: 找不到主类

这是我的父 Pom.xml:

<project>
    <groupId>org.example</groupId>
    <artifactId>Project Name</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>Controller</module>
        <module>Model</module>
        <module>View</module>
        <module>Execute</module>
    </modules>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.4.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                            <configuration>
                                <classifier>spring-boot</classifier>
                                <mainClass>
                                    ru.kwork.Main
                                </mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        ...
    </dependencies>
</project>

这里是子 Pom.xml:

<project>
    <parent>
        <artifactId>Project Name</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>Model</artifactId>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>
</project>

<project>
    <parent>
        <artifactId>Project Name</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>Controller</artifactId>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Model</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

<project>
    <parent>
        <artifactId>Project Name</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>View</artifactId>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Controller</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

<project>
    <parent>
        <artifactId>Project Name</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>Execute</artifactId>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>View</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

主类位于 Execute 模块中。

我在类似的问题中看到他们添加到子问题 Pom.xml 这里是这个插件:

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

但这对我没有帮助/或我做错了什么。 我也在网上看到了一些解决方案,但我不明白。我希望你能帮助我解决这个问题并找出解决方案。谢谢。

【问题讨论】:

  • 您是否尝试过在spring-boot-maven-plugin 上不使用&lt;executions&gt; (...) &lt;/executions&gt; 配置?
  • @JoãoDias,没有。然后在哪里指定主类?
  • 在 Spring Boot 中,您使用 @SpringBootApplication 标记主类。此类必须具有通常的public static void main(String[] args) 方法。在这里查看一个示例 --> baeldung.com/spring-boot-annotations#spring-boot-application.
  • @JoãoDias,我在主类上方有这个注释。
  • @JoãoDias,但它仍然没有帮助。

标签: java spring-boot maven jar multi-module


【解决方案1】:

从您的父 pom-xml 中删除 spring-boot-maven-plugin 并将以下内容删除到包含 @SpringBootApplication 的模块中:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.0</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

它会搜索main方法标记为可运行类,所以你不需要显式配置它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-01
    • 2018-02-23
    • 2018-02-02
    • 2018-04-25
    • 2018-09-17
    • 2015-10-10
    • 2018-09-10
    • 2017-02-14
    相关资源
    最近更新 更多