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)
}
// 參照圖七↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Gradle學習記錄025 Gradle核心插件eclipse plugin part2

圖一

Gradle學習記錄025 Gradle核心插件eclipse plugin part2

圖二

Gradle學習記錄025 Gradle核心插件eclipse plugin part2

圖三

Gradle學習記錄025 Gradle核心插件eclipse plugin part2

圖四

Gradle學習記錄025 Gradle核心插件eclipse plugin part2

圖五

Gradle學習記錄025 Gradle核心插件eclipse plugin part2

圖六

Gradle學習記錄025 Gradle核心插件eclipse plugin part2

圖七


分享到:


相關文章: