【问题标题】:Unable to find main class when running mvn sping-boot:build-image运行 mvn spring-boot:build-image 时找不到主类
【发布时间】:2021-12-04 13:14:03
【问题描述】:

我有一个 Maven 多模块项目

Project
|--- executable
|       |--- src
|       |--- pom.xml
|--- core
|       |--- src
|       |--- pom.xml
|--- pom.xml

根 pom.xml 看起来像这样

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.5</version>
    <relativePath/> 
</parent>

<groupId>com.playgrounds</groupId>
<artifactId>playgrounds-api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
    <java.version>11</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>8.0.1</version>
    </dependency>

</dependencies>

<modules>
    <module>executable</module>
    <module>core</module>
</modules>

可执行模块有一个主类,pom.xml看起来像这样

<parent>
    <groupId>com.playgrounds</groupId>
    <artifactId>playgrounds-api</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../</relativePath>
</parent>

<artifactId>executable</artifactId>
<name>executable</name>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

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

<dependencies>
    <dependency>
        <groupId>com.playgrounds</groupId>
        <artifactId>core</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.0-alpha5</version>
    </dependency>
</dependencies>

核心模块没有主类,长这样。

<parent>
    <groupId>com.playgrounds</groupId>
    <artifactId>playgrounds-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>core</artifactId>
<name>core</name>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

命令 mvn clean package 生成 SUCCESS 输出,但是当我运行 mvn spring-boot:build-image 命令时,我在核心模块上收到错误:尚未找到主类...有什么想法吗?我在 spring-boot 文档中进行了搜索,但找不到我所缺少的。

【问题讨论】:

    标签: java spring-boot docker maven


    【解决方案1】:

    当您从多模块项目的根目录调用像 spring-boot:build-image 这样的 Maven 目标时,Maven 将尝试在每个模块上运行到目标。在这种情况下,您只想从 executable 模块构建映像,而不是从 core 模块构建映像。处理此类情况的最佳方法是修改可执行模块的配置,将spring-boot:build-image 目标附加到另一个目标(如package),以便仅对该模块调用它。

    您可以通过修改executable 模块pom.xml 来做到这一点,如Spring Boot documentation 所示:

    <build>
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>build-image</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    

    然后运行mvn package

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 2022-06-17
      • 2023-01-28
      • 2020-09-03
      • 1970-01-01
      • 2022-10-17
      • 2021-01-04
      • 2020-10-01
      • 2021-10-29
      相关资源
      最近更新 更多