【问题标题】:Gradle + Eclipse: How to not export JARs that are dependencies of a dependency?Gradle + Eclipse:如何不导出作为依赖项的依赖项的 JAR?
【发布时间】:2014-02-25 17:09:29
【问题描述】:

在我的build.gradle 文件中,我有以下内容:

...
dependencies {
  ...
  testCompile (group: 'org.uncommons', name: 'reportng', version: '1.1.2') { exclude group: 'org.testng', module: 'testng' }
  ...
}
...

reportng 需要velocity-1.4.jar 和velocity-dep-1.4.jar,实际上上面的testCompile 依赖导致这两个JAR 被提取并放入Eclipse 的.classpath 文件中,作为“导出”(即,他们在 Eclipse 的“Java Build Path”对话框的“Order and Export”选项卡中的复选框被选中)。

这 2 个 JAR 被设置为导出的事实是一个问题。我需要它们仍然被提取但不被导出。

从Gradle doc 我了解到这是通过使用noExportConfigurations 按照他们的示例完成的:

apply plugin: 'java'
apply plugin: 'eclipse'

configurations {
  provided
  someBoringConfig
}

eclipse {

  classpath {

    //if you don't want some classpath entries 'exported' in Eclipse
    noExportConfigurations += configurations.provided
  }
}

我的问题是我没有configurations {} 部分,虽然我当然可以添加一个,但我不知道要在其中放入什么以便从导出中排除而不是整个 reportng 而只是它附带的两个 JAR。

【问题讨论】:

    标签: eclipse gradle build gradle-eclipse


    【解决方案1】:

    你可能想要这样的东西:

    configurations {
        noExport
    }
    dependencies {
        // replace with correct values
        noExport "foo:velocity:1.4" 
        noExport "foo:velocity-dep:1.4"
    }
    eclipse {
        classpath {
            noExportConfigurations += configurations.noExport
        }
    }
    

    PS:请不要在这里和http://forums.gradle.org上重复发帖。

    【讨论】:

    • 我完全不知道在这里和 Gradle 论坛上发布同样的问题是不好的。我真诚地道歉。感谢您的回答,我会尽快尝试。
    • 正在跟进on Gradle forum
    【解决方案2】:

    显然,在 Peter's answer 之后的一年半时间里,noExportConfigurations 已被弃用,并计划在 Gradle 3.0 中删除。更重要的是,the linked Gradle forum thread 中的任何解决方案都不允许我删除从文件夹导入的依赖项,例如 war/WEB-INF/lib。

    所以经过大量研究,我在build.gradle file in GitHub 的末尾偶然发现了一个示例,它重新排序了类路径中的条目:

    withXml { xml ->
        def node = xml.asNode()
        node.remove( node.find { it.@path == 'org.eclipse.jst.j2ee.internal.web.container' } )
        node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.jst.j2ee.internal.web.container', exported: 'true'])
    }
    

    我修改了这个例子,让它使用 Groovy 节点的正则表达式功能简单地删除一个 JAR 文件。请注意,xml -> 部分不是必需的,并且该条目是文件关闭的子项:

    withXml {
        def node = it.asNode()
        node.remove( node.find { it.@path ==~ /.*velocity-1\.4\.jar/ } )
        node.remove( node.find { it.@path ==~ /.*velocity-dep-1\.4\.jar/ } )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 2017-06-09
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多