如何将人工运行时范围更改为编译范围?

How to change artifactory runtime scope to compile scope?

我正在开发一个使用 gradle 和 jfrog 插件发布到 artifactory 的项目。重要的代码片段是:

plugins {
   id "java"
   id "idea"
   id "groovy"
   id "pmd"
   id "findbugs"
   id "maven-publish"
   id "com.jfrog.artifactory" version "3.1.1"
}

dependencies {
   compile 'com.google.guava:guava:18.0'
   compile 'com.mashape.unirest:unirest-java:1.4.5'
   compile 'log4j:log4j:1.2.14'
}

artifactory {
   contextUrl = "https://SOME_SERVER/artifactory"
   publish {
      repository {
           repoKey = 'libs-snapshot-local'
           username = artifactory_username
           password = artifactory_password
           maven = true
      }
      defaults {
           publications ('mavenJava')
      }
   }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

当我执行 gradle artifactoryPublish 时,一切似乎都运行良好。工件已发布,并且还会生成一个 pom 文件。

遗憾的是,pom文件中的依赖项都有一个运行时范围:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.whatsoever</groupId>
  <artifactId>some-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>18.0</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.mashape.unirest</groupId>
      <artifactId>unirest-java</artifactId>
      <version>1.4.5</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

他们应该有一个编译范围,而不是这个运行时范围。我究竟做错了什么?

从未在 practive 上尝试过,但您可以尝试使用 publication.pom.withXml 配置块在生成的 pom 中引入更改:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            pom.withXml {
                asNode().dependencies.'*'.findAll() {
                    it.scope.text() == 'runtime' && project.configurations.compile.allDependencies.find { dep ->
                        dep.name == it.artifactId.text()
                    }
                }.each() {
                    it.scope*.value = 'compile'
                }
            }
        }
    }
}

此外,这是新发布插件的已知限制,我在 this thread 中找到了解决方案。