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

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

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

问题

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

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

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

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.victorpiles.escacs</groupId>
  7. <artifactId>server</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <maven.compiler.source>17</maven.compiler.source>
  11. <maven.compiler.target>17</maven.compiler.target>
  12. </properties>
  13. <dependencies>
  14. <!-- Engine -->
  15. <dependency>
  16. <groupId>org.victorpiles.escacs</groupId>
  17. <artifactId>engine</artifactId>
  18. <version>1.0-SNAPSHOT</version>
  19. </dependency>
  20. <!-- Lombok -->
  21. <dependency>
  22. <groupId>org.projectlombok</groupId>
  23. <artifactId>lombok</artifactId>
  24. <version>1.18.26</version>
  25. <scope>compile</scope>
  26. </dependency>
  27. </dependencies>
  28. </project>

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

Engine的pom如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.victorpiles.escacs</groupId>
  7. <artifactId>engine</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <maven.compiler.source>17</maven.compiler.source>
  11. <maven.compiler.target>17</maven.compiler.target>
  12. </properties>
  13. <dependencies>
  14. <!-- Lombok -->
  15. <dependency>
  16. <groupId>org.projectlombok</groupId>
  17. <artifactId>lombok</artifactId>
  18. <version>1.18.26</version>
  19. <scope>compile</scope>
  20. </dependency>
  21. </dependencies>
  22. </project>

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

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] -------------------< org.victorpiles.escacs:server >-------------------
  4. [INFO] Building server 1.0-SNAPSHOT
  5. [INFO] --------------------------------[ jar ]---------------------------------
  6. Warning: The POM for org.victorpiles.escacs:engine:jar:1.0-SNAPSHOT is missing, no dependency information available
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] BUILD FAILURE
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] Total time: 0.282 s
  11. [INFO] Finished at: 2023-03-20T15:12:47Z
  12. [INFO] ------------------------------------------------------------------------
  13. 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]
  14. Error:
  15. Error: To see the full stack trace of the errors, re-run Maven with the -e switch.
  16. Error: Re-run Maven using the -X switch to enable full debug logging.
  17. Error:
  18. Error: For more information about the errors and possible solutions, please read the following articles:
  19. Error: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
  20. 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:

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

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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;groupId&gt;org.victorpiles.escacs&lt;/groupId&gt;
  7. &lt;artifactId&gt;server&lt;/artifactId&gt;
  8. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  9. &lt;properties&gt;
  10. &lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
  11. &lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
  12. &lt;/properties&gt;
  13. &lt;dependencies&gt;
  14. &lt;!-- Engine --&gt;
  15. &lt;dependency&gt;
  16. &lt;groupId&gt;org.victorpiles.escacs&lt;/groupId&gt;
  17. &lt;artifactId&gt;engine&lt;/artifactId&gt;
  18. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  19. &lt;/dependency&gt;
  20. &lt;!-- Lombok --&gt;
  21. &lt;dependency&gt;
  22. &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  23. &lt;artifactId&gt;lombok&lt;/artifactId&gt;
  24. &lt;version&gt;1.18.26&lt;/version&gt;
  25. &lt;scope&gt;compile&lt;/scope&gt;
  26. &lt;/dependency&gt;
  27. &lt;/dependencies&gt;
  28. &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:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;groupId&gt;org.victorpiles.escacs&lt;/groupId&gt;
  7. &lt;artifactId&gt;engine&lt;/artifactId&gt;
  8. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  9. &lt;properties&gt;
  10. &lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
  11. &lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
  12. &lt;/properties&gt;
  13. &lt;dependencies&gt;
  14. &lt;!-- Lombok --&gt;
  15. &lt;dependency&gt;
  16. &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  17. &lt;artifactId&gt;lombok&lt;/artifactId&gt;
  18. &lt;version&gt;1.18.26&lt;/version&gt;
  19. &lt;scope&gt;compile&lt;/scope&gt;
  20. &lt;/dependency&gt;
  21. &lt;/dependencies&gt;
  22. &lt;/project&gt;

The following error is thrown when running the Workflow:

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] -------------------&lt; org.victorpiles.escacs:server &gt;--------------------
  4. [INFO] Building server 1.0-SNAPSHOT
  5. [INFO] --------------------------------[ jar ]---------------------------------
  6. Warning: The POM for org.victorpiles.escacs:engine:jar:1.0-SNAPSHOT is missing, no dependency information available
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] BUILD FAILURE
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] Total time: 0.282 s
  11. [INFO] Finished at: 2023-03-20T15:12:47Z
  12. [INFO] ------------------------------------------------------------------------
  13. 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]
  14. Error:
  15. Error: To see the full stack trace of the errors, re-run Maven with the -e switch.
  16. Error: Re-run Maven using the -X switch to enable full debug logging.
  17. Error:
  18. Error: For more information about the errors and possible solutions, please read the following articles:
  19. Error: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
  20. 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:

确定