本文介绍了Delombok使用Gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我们构建过程的一部分,我们使用SonarQube分析了我们的源代码。



这个问题的一个问题是我们使用了Lombok注释,SonarQube处理得不好 - 我们的代码需要'delombok'。

删除的源代码删除了注释,并用源代码替换了编译器使用的最终代码。



这可以在gradle中完成(参见)。

好了,部分。通常可以使用Ant任务来删除源。下面的代码示例: -

 任务delombok {
// delombok任务可能依赖于已编译的其他项目
dependsOn configurations.compile.getTaskDependencyFromProjectDependency(true,compileJava)

//设置增量构建,必须在配置阶段进行(不是doLast)
inputs.files文件( srcJava)
outputs.dir文件(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)
}
}



另一种方法是使用Maven lombok:delombok插件(参见)。但我不知道如何做到这一点,如果这也需要一个预配置的环境。



我不确定哪个是最好的方法。一种不需要预先配置的构建系统并且可以完全从gradle / gradlew工作的方法将是可取的。

最终目标是创建一个'delombok'项目任务本质上执行以下操作:

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

编辑

已经很多了这个工作大致这个片段: - $ / b>

 依赖关系{
compile'org.projectlombok:lombok :1.14.2'
}

任务delombok {
描述'Delomboks整个源代码树'
def srcDelomboked ='build / src-delomboked'
def srcJava ='src'

inputs.files文件(srcJava)
outputs.dir文件(srcDelomboked)

doLast {
def collection = files(configurations.compile + configurations.testcompile)
def sumTree = collection + fi leTree(dir:'bin')

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


//替换当前的src目录包含删除源
复制{
从srcDelomboked
到srcJava
}
}
}




  • 使用delombok ant任务,该第一个位确保lombok jar可用于
    的gradle。然后,我们将源文件配置为
    use。

  • 接下来,我们设置gradle以使用ant任务。

  • 最后,复制任务将整个源代码树与代码的删除版本替换。很明显,这可以被删除,以满足您的需求。
  • 使用Ant任务很好。 不需要Ant预配置。或者,你可以使用 JavaExec 任务来调用delombok,就像你最后的代码片段一样。 ( JavaExec 目前不支持 -jar 选项,所以你必须命名主类。)使用Gradle中的Maven插件是不可能的(除了用 Exec 任务执行Maven)。


    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 forusing the delombok ant task.
    • Then we configure the source files touse.
    • 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.

    解决方案

    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).

    这篇关于Delombok使用Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:31