[Gradle] Compile and test dependencies with conflicts

I have a simple Gradle module with the following dependencies:

dependencies {
    compile 'commons-logging:commons-logging:1.1'

    testCompile group: 'junit', name: 'junit', version: '4.+'
    testCompile "javax.servlet:servlet-api:2.4"
}

The Gradle dependencies command reports the following:

------------------------------------------------------------
Project :moduleA
------------------------------------------------------------

compile - Compile classpath for source set 'main'.
\--- commons-logging:commons-logging:1.1
     +--- log4j:log4j:1.2.12
     +--- logkit:logkit:1.0.1
     +--- avalon-framework:avalon-framework:4.1.3
     \--- javax.servlet:servlet-api:2.3


testCompile - Compile classpath for source set 'test'.
+--- commons-logging:commons-logging:1.1
|    +--- log4j:log4j:1.2.12
|    +--- logkit:logkit:1.0.1
|    +--- avalon-framework:avalon-framework:4.1.3
|    \--- javax.servlet:servlet-api:2.3 -> 2.4
+--- junit:junit:4.+ -> 4.12
|    \--- org.hamcrest:hamcrest-core:1.3
\--- javax.servlet:servlet-api:2.4


Notice that the compile version of servlet-api is 2.3, meanwhile the test dependency is 2.4.
So the following Java code:

package moduleA;

import org.junit.Test;

public class TestClass {

 @Test
 public void testA()
 {
 HttpServletRequest req = null;

  int port = req.getLocalPort();
 }
}


Should compile in IntellJ Idea, but actually if I do a Make a get a compile error saying that "getLocalPort()" cannot be found, this method was introduced in 2.4. Therefore IntellJ Idea is using the 2.3 version of the library.

Is this a IntellJ Idea problem or a Gradle one?

Thanks!

5 comments
Comment actions Permalink

What Gradle version do you use?

0
Comment actions Permalink

My Gradle version is 2.6

0
Comment actions Permalink

IDEA uses gradle tooling api to get the project information. There was a fix for ide classpath generation in 2.5 see https://docs.gradle.org/2.5/release-notes#changes-in-ide-classpath-generation
But it's not your case. The problem is how to merge compile and test dependency of different versions in the same IDEA module using right classpath order.
Try the following:
1. Open 'Project Structure' dialog => modules => 'moduleA' => Dependencies tab
2. Move "javax.servlet:servlet-api:2.4" entry with scope 'Test' upper than "javax.servlet:servlet-api:2.3" with scope 'Compile'

0
Comment actions Permalink

I could give a try to the latest version of Gradle, 2.7 or 2.8, but as you pointed out I haven't seen anything related to this classpath issue.
Definitely, changing the order using the Dependencies tab would solve this issue, but I see this like a hacky solution.
Do you know if this is a registered bug somewhere?

Thanks!

0

Please sign in to leave a comment.