Search code examples
gradleaspectjsts-springsourcetoolsuitegradle-eclipsespring-aspects

How to set .classpath in STS (eclipse) by gradle for AspectJ and spring-aspects


I have a project using compile time weaving for @Configurable from spring-aspects into my classes using the @Configurable. I use the Spring Tool Suite 3.7.0 and got everything running if I use gradle tasks to build and start my application. (Thanks to the plugin: https://github.com/eveoh/gradle-aspectj).

Now I want to use also the AspectJ Eclipse nature. Manually I got this running by turning the project into AspectJ AND adding spring-aspects.jar as AspectJ inpath. I want to do this by gradle as well. To turn the project into AspectJ nature was possible by:

eclipse {
    project {
        buildCommand('org.eclipse.ajdt.core.ajbuilder')
        natures += 'org.eclipse.ajdt.ui.ajnature'
}

How to to configure gradle that it also do the step "add spring-aspects.jar as my inpath"?

When I compare the .classpath file the difference is this one:

<classpathentry exported="true" kind="con" path="org.eclipse.jst.j2ee.internal.web.container">
    <attributes>
        <attribute name="org.eclipse.ajdt.inpath.restriction" value="spring-aspects-4.1.7.RELEASE.jar"/>
        <attribute name="org.eclipse.ajdt.inpath" value="org.eclipse.ajdt.inpath"/>
    </attributes>
</classpathentry>

<classpathentry kind="con" path="org.eclipse.ajdt.core.ASPECTJRT_CONTAINER"/>

(the classpathentry org.eclipse.jst.j2ee.internal.web.container was already there but attributes are missing)

So how can I add this snipped to the classpath? I have seen examples which are modifying the classpath like this:

eclipseClasspath {
    withXml { xmlProvider ->
      def classpath = xmlProvider.asNode()
      def parser = new XmlParser() 

...but I always get an error here: could not find method whenConfigured() for arguments [build_52wic5gr82z6rcs33lo3ix1lk$_run_closure7_closure12_closure13@73914b82] on org.gradle.plugins.ide.eclipse.model.EclipseClasspath_Decorated@6ca18169. How to fix this error? Is this the right way to configure the AspectJ inpath to adapt the .classpath manually?


Solution

  • Finally I found a solution, maybe helpful for others. To create the named .classpath snippet just add the following in the build.gradle

    eclipse {
      classpath {
        file {
            withXml {
    
                def xmlparser = new XmlParser()
    
                def node = it.asNode()             
                node.findAll{it['@path'] == 'org.eclipse.jst.j2ee.internal.web.container'}.each {
                    println it;
                    def attributes = xmlparser.createNode(it, 'attributes', [:])
                    xmlparser.createNode(attributes, 'attribute', [name: 'org.eclipse.ajdt.inpath.restriction', value: 'spring-aspects-4.1.7.RELEASE.jar']);
                    xmlparser.createNode(attributes, 'attribute', [name: 'org.eclipse.ajdt.inpath', value: 'org.eclipse.ajdt.inpath']);
    ...