首页 > 解决方案 > 使用 API 模块构建的 Maven 多项目构建似乎不包含构建映像中的项目级依赖项

问题描述

您好,我有一个构建多个图像的 springboot 多项目 maven 构建。结构类似于:

- project-parent
   - common
   - project-b-parent
      - project-b-api
      - project-b-gateway
      - project-b-launcher
   - project-c-parent
      - project-c-api
      - project-c-gateway
      - project-c-launcher

*launcher 模块是我的 springboot uber jar,网关是 spring mvc 控制器和支持类,而 apis 是启动器模块中的项目级依赖项。例如 project-c 依赖于 project-b-api

我的项目父 pom 有:

   ...
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
   </parent>
   <version>0.0.1-SNAPSHOT</version>
   <groupId>my.group</groupId>
   <artifactId>project-parent</artifactId>
   <packaging>pom</packaging>
   ...
   <modules>
        <module>project-b-parent</module>
        <module>project-c-parent</module>
    </modules>
    ...
    <properties>
        <jib.skip>true</jib.skip>
    </properties>
    ...
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.google.cloud.tools</groupId>
                        <artifactId>jib-maven-plugin</artifactId>
                        <version>1.0.0</version>
                        <configuration>
                            <from>
                                <image>my.base.image:latest</image>
                            </from>
                            <to>
                                <image>my.image.registry/${project-name}</image>
                                <tags>
                                    <tag>${project.version}</tag>
                                    <tag>latest</tag>
                                </tags>
                            </to>
                            <allowInsecureRegistries>true</allowInsecureRegistries>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

注意我已经添加<jib.skip>true</jib.skip>到我的父 pom.xml 中。我已经进入<jib.skip>false</jib.skip>了启动器,这创建了仅从 *launcher 模块创建图像的预期行为。

所有模块的版本都为 0.0.1-SNAPSHOT。

当我使用潜水检查我的图像时,我没有 SNAPSHOT DEPENDENCIES 层,并且我的项目级依赖项似乎不在图像中。

当我运行我的应用程序时,它们似乎启动了 SpringBoot 并在端口上侦听,但控制器的端点没有加载。另一个项目在尝试从项目级依赖项加载类时抛出 NoClassDefFoundError 异常。

我已经尝试在没有 SNAPSHOT 的情况下将所有内容版本化为 0.0.1,并且我的项目级依赖项不包含在我的依赖项层中。

我也尝试过运行mvn -X -DjibSerialize=true clean compile jib:build > logs.txt和我的类级依赖项

...
[INFO] --- jib-maven-plugin:1.0.0:build (default-cli) @ project-b-launcher ---
[DEBUG] Configuring mojo com.google.cloud.tools:jib-maven-plugin:1.0.0:build from plugin realm ClassRealm[plugin>com.google.cloud.tools:jib-maven-plugin:1.0.0, parent: sun.misc.Launcher$AppClassLoader@5c647e05]
[DEBUG] Configuring mojo 'com.google.cloud.tools:jib-maven-plugin:1.0.0:build' with basic configurator -->
[DEBUG]   (f) allowInsecureRegistries = true
[DEBUG]   (f) mainClass = project-b.stuff.ApplicationKt
[DEBUG]   (f) container = com.google.cloud.tools.jib.maven.JibPluginConfiguration$ContainerParameters@77662d13
[DEBUG]   (f) image = my.registry/distroless-java:latest
[DEBUG]   (f) from = com.google.cloud.tools.jib.maven.JibPluginConfiguration$FromConfiguration@6a0328d7
[DEBUG]   (f) project = MavenProject: my.company:project-a-launcher:0.0.1 @ C:\my-programs\project-parent\project-b-parent\project-b-launcher\pom.xml
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@51ec2df1
[DEBUG]   (f) skip = false
[DEBUG]   (f) image = my-new-image
[DEBUG]   (f) tags = [0.0.1, latest]
[DEBUG]   (f) to = com.google.cloud.tools.jib.maven.JibPluginConfiguration$ToConfiguration@6370bf52
[DEBUG] -- end configuration --
[INFO] 
[INFO] Containerizing application to my.registry\project-parent\project-b-parent\project-b-launcher, my.registry\project-parent\project-b-parent\project-b-launcher\customer-launcher\project-b-launcher:0.0.1-SNAPSHOT, my.registry\project-parent\project-b-parent\project-b-launcher\customer-launcher\project-b-launcher...
[DEBUG] Containerizing application with the following files:
[DEBUG]     Dependencies:
[DEBUG]         C:\my-programs\project-parent\project-c-parent\project-c-gateway\target\classes
[DEBUG]         C:\my-programs\project-parent\project-c-parent\project-c-api\target\classes
[DEBUG]         C:\Users\me\.m2\repository\org\springframework\spring-web\5.1.4.RELEASE\spring-web-5.1.4.RELEASE.jar
...

请注意,我在此 DEBUG 日志输出中重命名了许多敏感目录等。我不确定第二条和第三条底线(项目级依赖项)是否应该指向 target\classes\ - 他们应该引用 .jars 吗?我想如果我只做一个 mvn compile jib:build 他们不能

我希望我已将其发布在正确的位置。

标签: javamavenkotlinjib

解决方案


事实证明,OP 的项目设置存在问题。(https://github.com/GoogleContainerTools/jib/issues/1539中的详细信息。)基本上,两者spring-boot-maven-pluginjib-maven-plugin都应用于 root pom.xml。这导致

  • spring-boot-maven-plugin尝试为每个子模块创建一个可运行的 JAR,其中一些是没有主类的库。
  • jib-maven-plugin尝试为每个子模块以及顶级项目构建映像。

推荐阅读