Delombok using Gradle

2019-04-19 07:51发布

问题:

As part of our build process we analyse our source code with SonarQube.

One problem with this is that we use Lombok annotations and SonarQube is not handling this very well -- our code needs to be 'delombok'ed.

Delomboked source removed the annotations and replaces the source file with the final code used by the compiler.

This can be done in gradle (see here).

Well, in part. Typically an Ant task can be used to delombok source. Code sample below:-

task delombok {
        // delombok task may depend on other projects already being compiled
        dependsOn configurations.compile.getTaskDependencyFromProjectDependency(true, "compileJava")

        // Set up incremental build, must be made in the configuration phase (not doLast)
        inputs.files file(srcJava)
        outputs.dir file(srcDelomboked)

        doLast {
            FileCollection collection = files(configurations.compile)
            FileCollection sumTree = collection + fileTree(dir: 'bin')

            ant.taskdef(name: 'delombok', classname: 'lombok.delombok.ant.DelombokTask', classpath: configurations.compile.asPath)
            ant.delombok(from:srcJava, to:srcDelomboked, classpath: sumTree.asPath)
        }
    }

The problem I have with this is that I believe I would need a pre-configured ant system (I've yet to get this working).

Another approach would be to use a Maven lombok:delombok plugin (see here). However I don't know how to do this and if this would also require a pre-configured environment.

I'm not sure which is the best approach. An approach that does not require a pre-configured build system and can work fully from gradle/gradlew would be preferrable.

The ultimate aim would to have a 'delombok' project task which would essentially perform the following:

java -jar lombok.jar delombok src -d src-delomboked

edit

So i've pretty much got this to work with roughly this snippet:-

dependencies {
    compile 'org.projectlombok:lombok:1.14.2'
}

task delombok {
    description 'Delomboks the entire source code tree'
    def srcDelomboked = 'build/src-delomboked'
    def srcJava = 'src'

    inputs.files file( srcJava )
    outputs.dir file( srcDelomboked )

    doLast {
        def collection = files( configurations.compile + configurations.testCompile )
        def sumTree = collection + fileTree( dir: 'bin' )

        ant.taskdef( name: 'delombok', classname: 'lombok.delombok.ant.DelombokTask', 
            classpath: configurations.compile.asPath +
                       configurations.testCompile.asPath )
        ant.delombok( from:srcJava, to:srcDelomboked, classpath: sumTree.asPath )


        // Replace current src directory with delomboked source
        copy {
            from srcDelomboked
            into srcJava
        }
    }
}
  • This first bit ensures that the lombok jar is available to gradle for using the delombok ant task.
  • Then we configure the source files to use.
  • Next we setup gradle to use the ant task.
  • Finally the copy task replaces the entire source tree with the delomboked version of the code. Obviously this could be removed to suit your needs.

回答1:

Using an Ant task is fine. No "Ant preconfiguration" should be necessary. Alternatively, you could use a JavaExec task to call delombok as in your last snippet. (JavaExec doesn't currently support the -jar option, so you'd have to name the main class.) Using a Maven plugin from Gradle isn't possible (except for executing Maven with an Exec task).



回答2:

I think the easiest way to delombok sources with gradle is:

task delombok {
    description 'Delomboks the source code'
    ant.taskdef(classname: 'lombok.delombok.ant.Tasks$Delombok', classpath: configurations.compile.asPath,  name: 'delombok')
    ant.mkdir(dir: 'build/src-delomboked') 
    ant.delombok(verbose: 'true', encoding: 'UTF-8', to: 'build/src-delomboked', from: 'src/main/java')
}


回答3:

https://github.com/franzbecker/gradle-lombok

buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/' }
    }
    dependencies {
        classpath 'io.franzbecker:gradle-lombok:1.6'
    }
}

apply plugin: 'java'
apply plugin: 'io.franzbecker.gradle-lombok'

UPD: at the moment I quite enough IntelliJ IDEA 2016.3 + Lombok plugin and the contents build.gradle:

dependencies {
    compileOnly 'org.projectlombok:lombok:+'
}


标签: gradle lombok