How to change artifactory runtime scope to compile scope?

时间:2015-06-26 09:31:28

标签: gradle artifactory

I am working on a project that uses gradle and the jfrog plugin to publish to artifactory. The important code fragments are these:

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
        }
    }
}

When I do a gradle artifactoryPublish everything seems to work fine. The artefacts are published and a pom file is generated as well.

Regretfully the dependencies in the pom file all have a runtime scope:

<?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>

Instead of this runtime scope they should have a compile scope. What am I doing wrong?

1 个答案:

答案 0 :(得分:4)

从未在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中找到了解决方案。