Gradle学习记录025 Gradle核心插件eclipse plugin part2

这是一个比较完整的使用Gradle的eclipse-wtp插件来配置工程倒入Eclipse的示例。使用通过https://start.spring.io/生成Gradle构建的springboot基础工程,引入了
eclipse-wtp插件。

同时这是Gradle学习记录的最后一篇。学习记录一共25篇,足以支撑笔者最初学习Gradle的目的:构建一个复杂多工程java web项目。之后有时间会把自定义插件部分补足。

可以帮助我们理解Eclipse IDE的配置文件和IDE的关系,也可以帮助我们理解如何使用Gradle的eclipse插件来修改配置文件。

plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
id 'eclipse-wtp'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
// 从此开始是针对eclipse的配置
eclipse {
// 取得.classpath文件
classpath.file {
// 参照图一图二 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 对.classpath文件进行操作,这个文件的内容符合xml语法
withXml { xml ->
def node = xml.asNode()
// 找到所有的path属性为src/test/java或者src/test/resources的标签
node.classpathentry.findAll {
it.@path == 'src/test/java' || it.@path == 'src/test/resources'


}.each {
// 将ignore_optional_problems属性添加至对象标签中
it.attributes[0].appendNode('attribute', [name: 'ignore_optional_problems', value: 'true'])
}
// 参照图一图↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}
}

wtp {
// 参照图三图四 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 取得org.eclipse.wst.common.project.facet.core.xml
facet {
file {
withXml { xml ->
def node = xml.asNode()
// 如果不存在name属性为myRuntime的标签,则生成一个
if (node.runtime.findAll { it.@name == 'myRuntime' }.isEmpty()) {
node.appendNode('runtime', [name: 'myRuntime'])
}
}
// 指定名字为jst.java的标签的版本为1.7
// 下面的图三图四展示了这个操作
whenMerged { wtpFacet ->
wtpFacet.facets.each {
if (it.name == 'jst.java') {
it.version = '1.7'
}
}

}
}
}
// 参照图三图↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}

// 参照图五图六 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 取得.project文件,并且添加工程特性。
project {
buildCommand 'net.sf.eclipsecs.core.CheckstyleBuilder'
natures 'net.sf.eclipsecs.core.CheckstyleNature'
}

// 参照图五图六↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}
// 参照图七 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 取得jdt文件,比如示例是在workspace\.metadata\.plugins\
// org.eclipse.jst.common.frameworks文件夹下
// 生成文件classpath.decorations.xml
eclipseJdt.doLast {
def classpathDecorationsDirPath = 'F:/02_eclipse/workspace/.metadata/.plugins/org.eclipse.jst.common.frameworks'
def classpathDecorationsDir = file(classpathDecorationsDirPath)
def classpathDecorationsFilePath = classpathDecorationsDirPath + '/classpath.decorations.xml'
def classpathDecorationsFile = file(classpathDecorationsFilePath)
if (!classpathDecorationsDir.exists()) {
classpathDecorationsDir.mkdirs()
}
if (!classpathDecorationsFile.exists()) {
classpathDecorationsFile.text = '<classpath>'
}
def classpathDecorations = new XmlParser().parse(classpathDecorationsFile)
def classpathDecorationsWriter = new XmlNodePrinter(new PrintWriter(new FileWriter(classpathDecorationsFile)))
classpathDecorationsWriter.preserveWhitespace =true
classpathDecorationsWriter.print(classpathDecorations)
}
// 参照图七↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

图一

图二

图三

图四

图五

图六

图七