1

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?

u6f6o
  • 2,050
  • 3
  • 29
  • 54

2 Answers2

4

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.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • That worked! Nevertheless I decided to go with the traditional approach until this plugin is not incubating anymore. – u6f6o Jul 01 '15 at 14:32
  • 1
    FYI: If your Gradle project has multiple modules, with one depending on the other. You may need to remove the line "{ dep -> dep.name == it.artifactId.text() }" Because this will actually change the artifactId tag in the generated pom.xml – carl_corder Feb 24 '16 at 20:45
0

Nowadays. If you use Gradle version after 7.x , it is recommend to use api keyword instead of compile if you want to expose yours dependencies.

But the api function is provided by Gradle plugin java-library .
So the setting file build.gradle write as follow:

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

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

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
        // Something else...
    }
}
Fulin Xu
  • 1
  • 2