【问题标题】:maven - skip dependency during buildmaven - 在构建期间跳过依赖项
【发布时间】:2023-03-05 03:56:02
【问题描述】:

我想在构建期间跳过 demo-api(这是另一个模块)。设置可选的 true 不起作用。关于如何跳过它但不从 pom.xml 中删除依赖项的任何建议?

Failed to execute goal on project [36mdemo-web[m: [1;31mCould not resolve dependencies for project demo-web:demo-web:war:1.0-SNAPSHOT: Failed to collect dependencies at demo-api:demo-api:jar:1.0-SNAPSHOT[m: Failed to read artifact descriptor for demo-api:demo-api:jar:1.0-SNAPSHOT: Could not find artifact demo-spring-boot:demo-spring-boot:pom:1.0-SNAPSHOT

    <dependency>
        <artifactId>demo-api</artifactId>
        <groupId>demo-api</groupId>
    <version>1.0-SNAPSHOT</version>
    <optional>true</optional>
    </dependency>

【问题讨论】:

  • 为什么不评论呢?你想解决什么问题?
  • 我不想评论它。是否有可以在构建过程中跳过的标签?我不希望开发人员每次都评论和取消评论
  • 我无法真正关注你。如果demo-api 是您项目的依赖项,您肯定会在代码的某个地方使用它吗?那么省略它会导致编译错误?
  • 这个依赖是否只在运行时使用?如果是这样,您可以定义 &lt;scope&gt;runetime&lt;scope&gt; 运行时(请参阅详细信息:maven.apache.org/pom.html

标签: java maven pom.xml


【解决方案1】:

您可以将该依赖项放在配置文件中:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.foo</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <organization>
        <name>Example Company</name>
        <url>http://www.example.com/</url>
    </organization>

    <name>foo</name>
    <profiles>
        <profile>
            <id>includeBadDependency</id>
            <dependencies>
                <dependency>
                    <artifactId>demo-api</artifactId>
                    <groupId>demo-api</groupId>
                    <version>1.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

<!--    Normal dependencies go here-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.5.RELEASE</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

当你构建这个项目时:

$ mvn verify # this will succeed

$ mvn verify -PincludeBadDependency
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< org.foo:foo >-----------------------------
[INFO] Building foo 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from maven-atlassian-com: https://packages.atlassian.com/maven/repository/internal/demo-api/demo-api/1.0-SNAPSHOT/maven-metadata.xml
Downloading from maven-atlassian-com: https://packages.atlassian.com/maven/repository/internal/demo-api/demo-api/1.0-SNAPSHOT/demo-api-1.0-SNAPSHOT.pom
[WARNING] The POM for demo-api:demo-api:jar:1.0-SNAPSHOT is missing, no dependency information available
Downloading from maven-atlassian-com: https://packages.atlassian.com/maven/repository/internal/demo-api/demo-api/1.0-SNAPSHOT/demo-api-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.434 s
[INFO] Finished at: 2021-09-21T18:24:24+10:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project foo: Could not resolve dependencies for project org.foo:foo:jar:1.0.0-SNAPSHOT: Could not find artifact demo-api:demo-api:jar:1.0-SNAPSHOT in maven-atlassian-com (https://packages.atlassian.com/maven/repository/internal) -> [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

【讨论】:

    【解决方案2】:

    https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

    也许这可以给你一两个提示。

    <scope>runtime</scope>
    

    我认为这可以解决问题

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多