11
votes

I have a multiproject build with multiple war modules that depends on one jar module.

Both war and jar modules have dependencies over libraries like Spring, Hibernate and so on, those dependencies are defined as providedCompile on the war modules and as compile on the jar.

The problem is that when JetGradle updates the dependencies all artifacts have errors, as the dependencies from the jar module are required on the artifacts.

I would like to use any of this solutions:

  1. Include the libraries on the lib folder of the server and have Intellij treat them as provided.
  2. Include the libraries as project wide libraries somehow, so intellij puts them on all artifacts even after the gradle dependencies are updated.

On the other hand my approach could be completely wrong from the beginning.

The dependencies in the war modules are defined as:

providedCompile 'org.slf4j:slf4j-log4j12:1.7.5'
providedCompile 'org.slf4j:jcl-over-slf4j:1.7.5'
...
compile(project(':jarModule')) {transitive = false}
...

The dependencies in the jar module are defined as:

...
compile 'org.slf4j:slf4j-log4j12:1.7.5'
compile 'org.slf4j:jcl-over-slf4j:1.7.5'
...
3

3 Answers

22
votes

The best solution I found was to set the transitive "compile" dependencies from the jar module as provided using the following code in the Gradle configuration file:

apply plugin: 'idea'

configurations {
    provided
    provided.extendsFrom(compile)
}

idea {
    module {
        scopes.PROVIDED.plus += configurations.provided
    }
}

For Gradle 2.0+ modify the last bit to be like this:

idea {
    module {
        scopes.PROVIDED.plus += [configurations.provided]
    }
}

This solution works using the Intellij Gradle plugin and also the idea task in Gradle

I got this solution working based on the info on this urls: https://github.com/Netflix/RxJava/pull/145 http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html

I hope this helps someone else

0
votes

I tried the above solution but found a problem. In my scenario I had a sub-project that had the above configuration. The problem was that the transitive dependencies of the sub-project were not being exported in the IntelliJ configuration, which caused the base project to stop compiling.

I did some digging around and stumbled upon this little gem which fixed the problem.

https://github.com/gradle/gradle/blob/ccddc438ce09293d84030ebe31668d739c8a228a/gradle/providedConfiguration.gradle

/**
 * Adds a configuration named 'provided'. 'Provided' dependencies
 * are incoming compile dependencies that aren't outgoing
 * dependencies. In other words, they have no effect on transitive
 * dependency management.
 */

configurations {
    provided
    providedPlusCompile.extendsFrom(compile, provided)
    testCompile.extendsFrom(providedPlusCompile)
}

sourceSets.main {
    compileClasspath = configurations.providedPlusCompile
}

plugins.withType(IdeaPlugin) {
    idea.module.scopes.PROVIDED.plus = [ configurations.provided ]
}
0
votes

Adding to the answer from Adrijardi, for Gadle 2.0 + not only did I have to change

scopes.PROVIDED.plus += configurations.provided

to

scopes.PROVIDED.plus += [configurations.provided]

I also had to change

provided project(":module-name") {
    transitive = false
}

to

provided (project(":module-name")) {
    transitive = false
}

Note the extra set of brackets on the second code sample