食谱:如何将Lombok与Eclipse,Gradle和Javadocs一起使用?

时间:2014-09-15 16:30:01

标签: java eclipse gradle lombok

Lombok使Java变得更好,gradle是一个非常棒的,灵活的构建工具,Eclipse大大简化了开发,而Javadocs让世界变得圆满。

因此,当我开始一个新项目时,我想弄清楚如何合并所有这些魔法。附件是我为此目的而编写的编译器,与项目的其他问题隔离。将其保存在可访问的地方并使用apply from: "path/to/lombok.gradle"

包含它
/**
 * Project Lombok, Eclipse, Javadocs and Gradle
 */
// Doing this twice (once here, once in your main project) has no effect on Gradle, but 
// this script depends on the Java and Eclipse plugins
apply plugin: 'java'
apply plugin: 'eclipse'

// Create a configuration to hold the lombok jar as a dependency
configurations {
    lombok
}
// Add the lombok jar to the configuration
dependencies { 
    lombok 'org.projectlombok:lombok:+' 
}
// Add the lombok configuration to all of the compile classpaths
sourceSets.each{ sourceSet -> 
    sourceSet.compileClasspath += configurations.lombok
    sourceSet.ext.delombok = new File(buildDir, "generated-src/delombok/" + sourceSet.name);
}
// This task will download lombok and install it in your eclipse instance
task installLombok() {
    dependsOn configurations.lombok
} << {
    File jarFile = null;
    configurations.lombok.resolvedConfiguration.resolvedArtifacts.find {
        if ("lombok".equals(it.name)) {
            jarFile = it.file;
        }
    }
    javaexec {
        main="-jar"
        args = [
            jarFile,
            "install",
            "auto"
        ]
    }
}
// Install lombok into eclipse when you set up the project (optional line)
eclipseProject.dependsOn installLombok

// Javadoc doesn't handle lombok'd code, so we have to "delombok" it - that is, expand the 
// neat annotations so that Javadoc can do something with them.
task delombok() {
    dependsOn configurations.compile
    dependsOn configurations.lombok
} << {
    File jarFile = null;
    configurations.lombok.resolvedConfiguration.resolvedArtifacts.find {
        if ("lombok".equals(it.name)) {
            jarFile = it.file;
        }
    }
    sourceSets.each{ sourceSet ->
        def classPath = sourceSet.compileClasspath.files.join(";")
        def delombokPath = sourceSet.ext.delombok
        delombokPath.mkdirs();
        javaexec {
            main = "-jar"
            args jarFile, "delombok"
            if (!classPath.empty) {
                args "-c", classPath
            }
            args "-d", delombokPath
            args sourceSet.allJava.srcDirs
        }
    }
}

javadoc {
    dependsOn delombok
    // At your discretion; I actually use ext.apiDelombok in my case
    source = sourceSets.main.ext.delombok
}

0 个答案:

没有答案