如何从Gradle Maven Publishing插件构建的POM中排除依赖项?

bnl4lu3b  于 2023-10-19  发布在  Maven
关注(0)|答案(3)|浏览(136)

我在build.gradle中有以下依赖项:

dependencies {
    compile 'org.antlr:antlr4-runtime:4.5.1'
    compile 'org.slf4j:slf4j-api:1.7.12'
    antlr "org.antlr:antlr4:4.5.1"
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'org.codehaus.groovy:groovy-all:2.4.4'
    testCompile 'cglib:cglib-nodep:3.1'
    testCompile 'org.objenesis:objenesis:2.1'
}

当我使用Maven Publishing插件发布我的库时,它在生成的POM中包含了ANTLR运行时和编译时JAR作为依赖项:

<dependencies>
  <dependency>                    <!-- runtime artifact -->
    <groupId>org.antlr</groupId>
    <artifactId>antlr4-runtime</artifactId>
    <version>4.5.1</version>
    <scope>runtime</scope>
  </dependency>
  <dependency>                    <!-- compile time artifact, should not be included -->
    <groupId>org.antlr</groupId>
    <artifactId>antlr4</artifactId>
    <version>4.5.1</version>
    <scope>runtime</scope>
  </dependency>
</dependencies>

我只希望运行时库包含在这个POM中。
罪魁祸首是antlr依赖:如果删除这一行,生成的POM就没有编译时依赖项。然而,构建失败了。

pcww981p

pcww981p1#

从@RaGe建议使用pom.withXml,我能够使用这个黑客来删除额外的依赖。

pom.withXml {
  Node pomNode = asNode()
  pomNode.dependencies.'*'.findAll() {
    it.artifactId.text() == 'antlr4'
  }.each() {
    it.parent().remove(it)
  }
}

使用前:

<dependencies>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-runtime</artifactId>
      <version>4.5.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4</artifactId>
      <version>4.5.1</version>
      <scope>runtime</scope>
    </dependency>
</dependencies>

之后:

<dependencies>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-runtime</artifactId>
      <version>4.5.1</version>
      <scope>runtime</scope>
    </dependency>
</dependencies>

更多链接来解释这个问题:

nbysray5

nbysray52#

给予grad福瑞一个机会。它肯定会处理排除,我很确定只有已知的配置包含在生成的pom中。它也有一些代码来确保没有重复的条目与冲突的范围(这是一个痛苦的皇家找出解决方案)
https://github.com/gradle-fury/gradle-fury
免责声明,我在努力

zyfwsgd6

zyfwsgd63#

在某些情况下,您可能需要强制特定依赖项在gradle maven publish插件构建的pom中具有排除项。据我在写这篇文章时所知,没有一种直接的方法可以不使用pom.withXml来完成这一任务
我将把这段代码留在这里,以防对其他人有帮助。特别是,Spring依赖管理插件的用户可能会发现这很有用

// in buildSrc/main/src/main/groovy/Exclusion.groovy 
// gradle needs groovy classes to be located here
// If they are, they automatically become accessibl in the build.gradle
class Exclusion {
  Node dependencies

  Exclusion(Node dependencies) {
    this.dependencies = dependencies
  }

  static void create(Node dependencies, Closure closure) {
    closure.delegate = new Exclusion(dependencies)
    closure()
  }

  void fromDependency(Map args, Closure closure) {
    closure.delegate = new ExcludedDependency(args)
    closure.resolveStrategy = Closure.DELEGATE_FIRST
    closure()
  }

  class ExcludedDependency {
    String groupId
    String artifactId
    String version

    ExcludedDependency(Map args) {
      this.groupId = args.groupId
      this.artifactId = args.artifactId
      this.version = args.version
    }

    void exclude(Map exclusionArgs) {
      Node dependency = dependencies.depthFirst().find({ Node it ->
        it.name() == 'dependency'
          && it.groupId.text() == groupId
          && it.artifactId.text() == artifactId
          && it.version.text() == version
      })

      if (null != dependency) {
        Node exclusionNode = (dependency.exclusions[0] as Node
          ?: dependency.appendNode('exclusions').appendNode('exclusion'))
        exclusionNode.appendNode('groupId', exclusionArgs.groupId)
        exclusionNode.appendNode('artifactId', exclusionArgs.artifactId)
      }
    }

  }
}
// Usage in build.gradle to force the pom to have our exclusions
publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java
      pom.withXml { ->
        Node pomDependencies = asNode() as Node
        Exclusion.create(pomDependencies) {
          fromDependency([groupId: 'com.enterprise', artifactId: 'xyz', version: "${xyzVersion}"]) {
            exclude([groupId: 'com.foo', artifactId: 'bar'])
          }
        }
      }
    }
  }
}

这将导致

<dependencies>
    <dependency>
        <groupId>org.enterprise</groupId>
        <artifactId>xyz</artifactId>
        <version>1.2.3</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>org.foo</groupId>
                <artifactId>bar</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

而在以前,如果没有修改,

<dependencies>
    <dependency>
        <groupId>org.enterprise</groupId>
        <artifactId>xyz</artifactId>
        <version>1.2.3</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

相关问题