Search code examples
gradleartifactory

How to change artifactory runtime scope to compile scope?


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?


Solution

  • Never tried this on practive, but you can try to use publication.pom.withXml configuration block to introduce changes in generated 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'
                    }
                }
            }
        }
    }
    

    Also, this is a known limitation of new publishing plugin and I found the solution in this thread.