You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OKD/OpenShift S2I构建Maven多模块Git子模块项目失败求助

问题:OKD 4.15中S2I构建Maven多模块Git子模块项目失败

在OKD 4.15环境中,使用本地Git仓库作为源,通过S2I构建基于JDK21的Java应用时出现异常:项目为Maven多模块结构,采用Git子模块管理(Backend为根项目),构建过程成功拉取Git根项目,但无法找到Git子模块的POM文件,Maven转而尝试从中央仓库查找依赖的Base模块(如au.com.cs.base:api),最终构建报错。已在BuildConfig中配置Maven命令使用-am参数,预期自动构建依赖项目,但未生效。


环境与配置

BuildConfig配置

kind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
  name: test3
  namespace: test
  uid: 2bae8477-2ec2-4446-9286-314ea33ed54d
  resourceVersion: '5147078'
  generation: 79
  creationTimestamp: '2024-04-15T14:38:54Z'
  labels:
    app: test3
    app.kubernetes.io/component: test3
    app.kubernetes.io/instance: test3
spec:
  nodeSelector: null
  output:
    to:
      kind: ImageStreamTag
      name: 'test2:latest'
  resources: {}
  successfulBuildsHistoryLimit: 5
  failedBuildsHistoryLimit: 5
  strategy:
    type: Source
    sourceStrategy:
      from:
        kind: ImageStreamTag
        namespace: openshift
        name: 'openjdk-21:latest'
      env:
        - name: MAVEN_ARGS_APPEND
          value: '-pl inventory/api -am'
  postCommit: {}
  source:
    type: Git
    git:
      uri: 'ssh://somesuser@somedomain/volume1/repo/project/cybackend/backend'
      ref: master
    contextDir: inventory/api
    sourceSecret:
      name: some-ssh-key
  triggers:
    - type: GitHub
      github:
        secret: <somesecret>
    - type: Generic
      generic:
        secret: <somesecret>
    - type: ConfigChange
    - type: ImageChange
      imageChange: {}
  runPolicy: Serial
status:
  lastVersion: 48
  imageChangeTriggers:
    - lastTriggeredImageID: 'registry.access.redhat.com/ubi9/openjdk-21@sha256:779ca5034381bd4859ea9b51c7121f28210a97aa17a6914c32255237bc578445'
      from:
        namespace: openshift
        name: 'openjdk-21:latest'
      lastTriggerTime: '2024-05-02T03:26:06Z'

项目结构

Backend
|
+--+---base
   |   +---api
   |   +---core
   |   +---data
   |   +---grpc
   |   +---model
   |   +---rsocket
   |   +---service
   +---inventory
       +---api
       +---data
       +---grpc
       +---inv-rsocket
       +---model
       \---service

执行的Maven命令

mvn -e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga -Dfabric8.skip=true -Djkube.skip=true --batch-mode -Djava.net.preferIPv4Stack=true -s /tmp/artifacts/configuration/settings.xml -Dmaven.repo.local=/tmp/artifacts/m2 -pl inventory/api -am package

报错信息

Non-resolvable parent POM for au.com.cs.inventory:api:0.0.1-SNAPSHOT: Could not find artifact au.com.cs.base:api:pom:0.0.1-SNAPSHOT in central (https://repo1.maven.org/maven2) and 'parent.relativePath' points at wrong local POM @ line 6, column 10


相关POM文件

Backend根POM

<project    ...>
...
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>au.com.cs</groupId>
    <artifactId>backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>backend</name>
    <description>Base project for Base</description>

    <modules>
        <module>base</module>
        <module>inventory</module>
....
   </modules>

    <properties>
   ...
    </properties>
   
   ...
   
     <profiles>
        <profile>
            <id>openshift</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.eclipse.jkube</groupId>
                        <artifactId>openshift-maven-plugin</artifactId>
                        <version>${jkube.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>resource</goal>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

Base模块POM

<project ...>
    <parent>
        <groupId>au.com.cs</groupId>
        <artifactId>backend</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>au.com.cs</groupId>
    <artifactId>base</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>base</name>
    <description>Base project for Base</description>

    <modules>
        <module>rsocket</module>
        <module>grpc</module>
        <module>core</module>
        <module>model</module>
        <module>data</module>
        <module>service</module>
        <module>api</module>
    </modules>

Inventory模块POM

<project  ...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>au.com.cs</groupId>
        <artifactId>base</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../base/pom.xml</relativePath>
    </parent>
    <groupId>au.com.cs</groupId>
    <artifactId>inventory</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>inventory</name>
    <description>Inventory project for Container Sales</description>

    <modules>
        <module>api</module>
        <module>inv-rsocket</module>
        <module>model</module>
        <module>data</module>
        <module>service</module>
        <module>grpc</module>
    </modules>
</project>

Inventory/API模块POM

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>au.com.cs.base</groupId>
        <artifactId>api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../../../base/api/pom.xml</relativePath>      <!-- lookup parent from repository -->
    </parent>

    <groupId>au.com.cs.inventory</groupId>
    <artifactId>api</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>api</name>
    <description>API project for Inventory</description>

解决方案

1. 修正BuildConfig的上下文路径

当前contextDir设置为inventory/api,导致S2I仅拉取该子目录,上层的base模块不在构建上下文内,Maven无法解析本地依赖。需修改BuildConfig的source部分:

source:
  type: Git
  git:
    uri: 'ssh://somesuser@somedomain/volume1/repo/project/cybackend/backend'
    ref: master
  # 移除contextDir,使用项目根目录作为构建上下文
  # contextDir: inventory/api
  sourceSecret:
    name: some-ssh-key

保留MAVEN_ARGS_APPEND: '-pl inventory/api -am',Maven会从根目录扫描所有依赖模块。

2. 修复POM中的relativePath错误

  • Backend根POM:移除Spring Boot父POM的错误relativePath(根项目无上层pom):
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <!-- 移除错误的relativePath -->
    </parent>
    
  • 其他模块的relativePath在上下文为根目录时路径正确,无需修改。

3. 确保Git子模块被拉取

若base是Git子模块,需添加环境变量让S2I拉取子模块:

strategy:
  type: Source
  sourceStrategy:
    from:
      kind: ImageStreamTag
      namespace: openshift
      name: 'openjdk-21:latest'
    env:
      - name: MAVEN_ARGS_APPEND
        value: '-pl inventory/api -am'
      - name: GIT_SUBMODULES
        value: "true"

同时确认SSH密钥有权限访问所有子模块的Git仓库。

4. 调整Maven构建目标

将构建目标改为install package,确保依赖模块先安装到本地Maven仓库:

env:
  - name: MAVEN_ARGS_APPEND
    value: '-pl inventory/api -am install package'

内容的提问来源于stack exchange,提问作者Coffee Bean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 16:24:55