Gradle + Eclipse plugin: Add AspectJ nature into projects

I have found nice script for adding AspectJ nature into Eclipse project using Gradle. See https://github.com/breskeby/gradleplugins/blob/0.9-upgrade/aspectjPlugin/aspectJ.gradle#L29 . Unfortunately each run of ‘gradle eclipse’ adds new nodes into project XML. Here is my fix of that issue:

eclipse.project.file.withXml { xmlProvider-> 
    def projectDescription = xmlProvider.asNode() 
    def xmlparser = new XmlParser() 
    def builders = projectDescription.buildSpec[0] 
    
    if (!builders.buildCommand.any {it.name[0].value()=='org.eclipse.ajdt.core.ajbuilder'}) {
        def ajbuilder = xmlparser.createNode(builders, 'buildCommand', [:])
        
        xmlparser
            .createNode(ajbuilder, 'name', [:])
            .setValue('org.eclipse.ajdt.core.ajbuilder') 

        xmlparser.createNode(ajbuilder, 'arguments', [:]);
    } 
        
    def natures = projectDescription.natures[0] 
    
    if (!natures.nature.any {it.value()=='org.eclipse.ajdt.ui.ajnature'}) { 
        def ajnature = xmlparser.createNode(null, 'nature', [:]) 
        ajnature.setValue('org.eclipse.ajdt.ui.ajnature'); 
        natures.children().add(0, ajnature) 
    } 
} 

Then all nodes are created only once.

Tags:  Gradle  Java  Eclipse 
Last blog articles:
Why I am not afraid of nowadays technologies
DIY pokus: Destilace z kvasu
Hello Ruby: Dobrodružné programování
Edison EdBlocks: Using remote control
Edison 2.0 received!

Tags:
Java Gradle Ubuntu Software-architecture children React Eclipse DisplayLink DIY AI česky personal Spring JavaScript AspectJ neuron SSH JavaFX Immutables FatJar Edison technologies procrastination pokusy lambda kvas ellipsis destilace chemie alkohol VisualVM Upstart Tomcat Selenium REST PrimeFaces Log4j Jooq Jackson JSF GIT Debug DI CSS

Last tweets: