Add Java CI with Maven workflow to Maven project with my own dependencies.

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

Add Java CI with Maven workflow to Maven project with my own dependencies

问题

我有一个GitHub仓库,里面包含三个Maven项目:

  • 客户端 (Client)
  • 服务器 (Server)
  • 引擎 (Engine)

我想要应用使用Maven的Java CI工作流程,配置如下:

# 该工作流将使用Maven构建Java项目,并缓存/还原依赖项以提高工作流执行时间
# 有关更多信息,请参见:https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# 该工作流使用未经GitHub认证的操作。
# 这些操作由第三方提供,并受独立的服务条款、隐私政策和支持文档约束。
name: Java CI with Maven
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: maven
      - name: Build with Maven -> Client
        run: mvn -B package --file client/pom.xml
      - name: Build with Maven -> Engine
        run: mvn -B package --file engine/pom.xml
      - name: Build with Maven -> Server
        run: mvn -B package --file server/pom.xml
      # 可选:将完整的依赖关系图上传到GitHub,以改善Dependabot警报的质量,该存储库可以接收这些警报
      - name: Update dependency graph
        uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

问题出在Server的pom文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.victorpiles.escacs</groupId>
    <artifactId>server</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <!-- Engine -->
        <dependency>
            <groupId>org.victorpiles.escacs</groupId>
            <artifactId>engine</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

因此,Server将Engine作为依赖项,而GitHub无法解析此依赖项。

Engine的pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.victorpiles.escacs</groupId>
    <artifactId>engine</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

运行工作流程时出现以下错误:

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< org.victorpiles.escacs:server >-------------------
[INFO] Building server 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Warning:  The POM for org.victorpiles.escacs:engine:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.282 s
[INFO] Finished at: 2023-03-20T15:12:47Z
[INFO] ------------------------------------------------------------------------
Error:  Failed to execute goal on project server: Could not resolve dependencies for project org.victorpiles.escacs:server:jar:1.0-SNAPSHOT: Could not find artifact org.victorpiles.escacs:engine:jar:1.0-SNAPSHOT -> [Help 1]
Error:  
Error:  To see the full stack trace of the errors, re-run Maven with the -e switch.
Error:  Re-run Maven using the -X switch to enable full debug logging.
Error:  
Error:  For more information about the errors and possible solutions, please read the following articles:
Error:  [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
Error: Process completed with exit code 1.
英文:

I have a GitHub repo with this three Maven projects inside:

  • Client
  • Server
  • Engine

I want to apply the Java CI with Maven workflow, the configuration is this one:

# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Java CI with Maven
on:
  push:
    branches: [ &quot;main&quot; ]
  pull_request:
    branches: [ &quot;main&quot; ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: &#39;17&#39;
          distribution: &#39;temurin&#39;
          cache: maven
      - name: Build with Maven -&gt; Client
        run: mvn -B package --file client/pom.xml
      - name: Build with Maven -&gt; Engine
        run: mvn -B package --file engine/pom.xml
      - name: Build with Maven -&gt; Server
        run: mvn -B package --file server/pom.xml
      # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
      - name: Update dependency graph
        uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

The problem comes with the Server pom, looks like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
		 xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
		 xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

	&lt;groupId&gt;org.victorpiles.escacs&lt;/groupId&gt;
	&lt;artifactId&gt;server&lt;/artifactId&gt;
	&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

	&lt;properties&gt;
		&lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
		&lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;!-- Engine --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.victorpiles.escacs&lt;/groupId&gt;
			&lt;artifactId&gt;engine&lt;/artifactId&gt;
			&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;!-- Lombok --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
			&lt;version&gt;1.18.26&lt;/version&gt;
			&lt;scope&gt;compile&lt;/scope&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;
&lt;/project&gt;

So the Server has the Engine as a dependency and GitHub can't resolve this dependency.

The pom of the Engine is the following:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
		 xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
		 xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

	&lt;groupId&gt;org.victorpiles.escacs&lt;/groupId&gt;
	&lt;artifactId&gt;engine&lt;/artifactId&gt;
	&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

	&lt;properties&gt;
		&lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
		&lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;!-- Lombok --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
			&lt;version&gt;1.18.26&lt;/version&gt;
			&lt;scope&gt;compile&lt;/scope&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;
&lt;/project&gt;

The following error is thrown when running the Workflow:

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------&lt; org.victorpiles.escacs:server &gt;--------------------
[INFO] Building server 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Warning:  The POM for org.victorpiles.escacs:engine:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.282 s
[INFO] Finished at: 2023-03-20T15:12:47Z
[INFO] ------------------------------------------------------------------------
Error:  Failed to execute goal on project server: Could not resolve dependencies for project org.victorpiles.escacs:server:jar:1.0-SNAPSHOT: Could not find artifact org.victorpiles.escacs:engine:jar:1.0-SNAPSHOT -&gt; [Help 1]
Error:  
Error:  To see the full stack trace of the errors, re-run Maven with the -e switch.
Error:  Re-run Maven using the -X switch to enable full debug logging.
Error:  
Error:  For more information about the errors and possible solutions, please read the following articles:
Error:  [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
Error: Process completed with exit code 1.

答案1

得分: 2

你的项目应该是一个多模块项目。

将一个 pom.xml 放入项目的根目录,列出其他目录作为 &lt;modules&gt;。然后构建根 POM,一切都将自动构建,包括已解决的依赖项。

英文:

Your project should have been a multi-module project.

Put a pom.xml into the root directory of the project which lists the other directories as &lt;modules&gt;. Then you build the root POM and everything will be build automatically, with resolved dependencies.

huangapple
  • 本文由 发表于 2023年3月21日 01:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793299.html
匿名

发表评论

匿名网友

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

确定