Groovy 中关于 eclipse plugin的一些理解

EclipseJdt

官方API文档

Eclipse plugin的详细内容进行优化调节

  1. 什么是Eclipse plugin
  2. Eclipse plugin的detail包括哪些内容
  3. 如何优化调节

首先看官方文档中例子

apply plugin: 'java'      //要用eclipse插件,当然需要申明引入java插件
apply plugin: 'eclipse'   //申明引入eclipse插件

//groovy语言的特点,都是用{}括起来的closure,针对eclipse这个closure进行配置
//引入了eclipse插件,自然就有了eclipse这个closure
eclipse {
  //eclipse里的jdt,下面有3个属性可以设置
  //1.file;2.sourceCompatibility;3.targetCompatibility
  jdt {
    //if you want to alter the java versions (by default they are configured with gradle java plugin settings):
    sourceCompatibility = 1.6
    targetCompatibility = 1.5

    file {
      //whenMerged closure is the highest voodoo
      //and probably should be used only to solve tricky edge cases.
      //the type passed to the closure is Jdt

      //closure executed after jdt file content is loaded from existing file
      //and after gradle build information is merged
      whenMerged { jdt
        //you can tinker with the Jdt here
      }
      
      //withProperties allows addition of properties not currently
      //modeled by Gradle
      withProperties { properties ->
          //you can tinker with the Properties here
      }
    }
  }
}
属性的详细
file

这是一个PropertiesFileContentMerger类型(只读)
具体参照,EclipseJdt这个closure的file的closure内容 什么是只读

sourceCompatibility

定义了java源文件的java版本,缺省是project.sourceCompatibility

targetCompatibility

定义了生成java字节码文件的jvm版本,缺省是project.targetCompatibility

Script blocks file:Enables advanced configuration like affecting the way existing jdt file content is merged with gradle build information 可以进一步配置

file{} Enables advanced configuration like affecting the way existing jdt file content is merged with gradle build information
The object passed to whenMerged{} and beforeMerged{} closures is of type Jdt
The object passed to withProperties{} closures is of type Properties
For example see docs for EclipseJdt

Groovy的Class

EclipseJdt
PropertiesFileContentMerger

DOMA的例子
eclipse {
    //it是Groovy的关键字,闭包内的唯一参数的隐式参数
    //为什么不直接用等号?
    jdt.file.withProperties { it['org.eclipse.jdt.core.compiler.processAnnotations'] = 'enabled' }
    //classpath定义了有什么用,跟上面sourceCompatibility有什么区别?
    classpath {
        containers = [
            'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
        ]
    }
}

这里只是将eclipse的注解编译器打开

eclipseJdt {
    def clos = []

    def fp = file('.factorypath')
    outputs.file fp
    clos += {
        fp.withWriter {
            def domaJar = configurations.compile.find {
                it.name.startsWith('doma-2')
            }
            new groovy.xml.MarkupBuilder(it).factorypath() {
                factorypathentry(kind:'EXTJAR', id:domaJar, enabled:true, runInBatchMode:false)
            }
        }
    }

    //定义一个prefs的方法
    def prefs = { name, contents ->
        //定义了一个文件的参数f
        def f = file(".settings/$name")
        //把contents的内容多行分解成string,赋值给f.text
        clos += {
            f.text = contents.stripMargin()
        }
        //输出内容到f这个参数定义的文件中去
        outputs.file f
    }
    //调用prefs这个方法,org.eclipse.jdt.apt.core.prefs这个文件里面,写下面四行内容。
    //第一行的“\”是什么意思→表示这行没有换行
    prefs 'org.eclipse.jdt.apt.core.prefs', """\
        |eclipse.preferences.version=1
        |org.eclipse.jdt.apt.aptEnabled=true
        |org.eclipse.jdt.apt.genSrcDir=${aptDir}
        |org.eclipse.jdt.apt.reconcileEnabled=true
        |"""

    doLast { clos*.run() }
}