Search code examples
javaeclipsegradleeclipse-wtp

Gradle integration for eclipse keeps on changing .classpath file


My current configuration includes a Java WAR project using gradle with the java and eclipse-wtp plugins, and the latest version of Buildship for Eclipse integration (1.0.3 till this morning and 1.0.4 right now) along with Eclipse Luna.

After following this solution https://stackoverflow.com/a/9820317/1544713 (successfully) to avoid deploying the test classes to my local server, I noticed that every time I refreshed the project or closed and opened Eclipse, the .classpath file and .settings/org.eclipse.wst.common.component were changed to their previous state, which is something I that gives me problems (deploying the test classes to the local server implies that they will fail to be loaded due to the lack of some test time dependencies, and of course an undesired behaviour).

The content of .classpath is changed from (the right one):

<classpathentry kind="src" output="target/test-classes" path="src/test/java">
    <attributes>
        <attribute name="FROM_GRADLE_MODEL" value="true"/>
    </attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/resources">
    <attributes>
        <attribute name="FROM_GRADLE_MODEL" value="true"/>
    </attributes>
</classpathentry>

to this undesired state:

<classpathentry kind="src" path="src/test/java">
    <attributes>
        <attribute name="FROM_GRADLE_MODEL" value="true"/>
    </attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/resources">
    <attributes>
        <attribute name="FROM_GRADLE_MODEL" value="true"/>
    </attributes>
</classpathentry>

And in the case of .settings/org.eclipse.wst.common.component, these two lines are added (which again, I don't want them to be present):

    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>

I can't figure out if it is Gradle that is changing the files or Buildship, or even Eclipse. In any case, I guess there is a way to stop this happening. I have tried many alternatives, like the following configs in build.gradle:

eclipse.classpath.file.whenMerged { cp ->
    cp.entries.findAll { it.kind = "src" && it.path.startsWith("src/test/") }*.output = "target/test-classes"
}

eclipse {
    wtp.component {  
        file.withXml { xml ->       
            def node = xml.asNode()                     
            def wbrNodes = node.'**'.findAll { it.name() == 'wb-resource' && it.'@source-path'.startsWith("/src/test/")}
            if (wbrNodes.size() > 0) {
                wbrNodes.each { n -> n.parent().remove(n) }
            }                   
        }
    }
}

But this configuration works erratically (sometimes it seems to work, some other times it doesn't, and actually the first piece of code that starts with eclipse.classpath.file.whenMerged never works).

Thanks in advance.


Solution

  • After some time spent looking for possible solutions, and the cause of the problem, I've found out that the real problem was in fact Buildship, which was not paying any attention the the directives to the eclipse-wtp plugin in build.gradle, and instead it was taking its own approach to generate a .classpath and the related eclipse configuration files. At the same time, and so far (Buildship 1.0.5 released just today) there is no way to configure or manipulate Buildship while it is building its own model (when importing a Gradle project, when opening Eclipse or when refreshing a project e.g. with F5). As Lance_Java said in the Gradle forums (https://discuss.gradle.org/t/gradle-integration-for-eclipse-keeps-on-changing-classpath-file/11813/7?u=fbudassi), it's useless to use the eclipse-wtp plugin together with Buildship, since both take their own approach to generate the Eclipse configuration files.

    So, the solution so far has been to remove Buildship from my Eclipse installation and instead replace it with the Gradle IDE Pack 3.6.x+0.17 plugin right from the Eclipse marketplace, which uses the build.gradle directives to build its own model, avoiding this way any possible conflict.

    If anybody from Buildship gets to read this post, please, give us some hooks to the generation of the files. Again, as Lance_Java suggested, something like this would really help:

    apply plugin: 'buildship' 
    
    buildship.wtp.component.file.withXml { ... }