Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delombok using Gradle

Tags:

gradle

lombok

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.
like image 637
VeasMKII Avatar asked Jul 28 '14 14:07

VeasMKII


People also ask

Can I use a Gradle plugin in Maven?

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories. The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

What is Delombok?

Delombok's standard mode of operation is that it copies an entire directory into another directory, recursively, skipping class files, and applying lombok transformations to any java source files it encounters. Delombok's output format can be configured with command line options (use --format-help for a complete list).

How do you use Lombok in spring boot gradle?

For Lombok to work, we need to turn on annotations processing. To do that, go to File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors and mark “Enable annotation processing”.


1 Answers

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')
}
like image 148
Andreas M. Oberheim Avatar answered Sep 19 '22 01:09

Andreas M. Oberheim