Gradle is adding runtime dependency to compile classpath

I am using gradle 2.2.1. I have declared a runtime dependency in my build.gradle file as following -

runtime ‘org.hibernate:hibernate-entitymanager:4.3.6.Final’

When I run ‘gradle eclipse’, gradle adds this dependency to compile classpath which should not be the case.

Eclipse has no notion of compile vs. runtime classpath. 

In order to support executing the the application (i.e. running the main class) from within the IDE, Gradle flattens the runtime and compile classpaths together when projecting the build configuration to Eclipse.

I can understand that this behavior may be required in some cases as mentioned by you. However this may create problem where one can accidentally include code from a runtime dependency.

Ideally this behavior (runtime dependency is not added to classpath) should be default behavior. If that is not possible then, at a minimum, there should be an option to disable the current behavior for the situations where this behavior is not required.

I disagree that the default should NOT add runtime classpath to Eclipse’s classpath. In order to run test cases, you need the runtime classpath… and all your code has tests… right!!!

I understand your objection to the default, which is likely to occur with any mutually exclusive choice of this nature. This default does seem to be the most appropriate for the majority of users.

You should not consider the IDE build to be authoritative. Both IDEA and Eclipse have drastically simplified models compared to Gradle, so it’s very often going to be a lossy translation. 

If you do want to change the behaviour, you can configure the projected classpath.

I added following code to remove runtime dependencies from eclipse classpath -

eclipse {
 classpath {
  minusConfigurations += [ configurations.runtime ]

However this is removing compile dependencies too from classpath.

Is there any way to fix above issue?


1 Like