![](/img/trans.png)
[英]Cannot build Gradle project with Gradle Support plugin for NetBeans 8.1
[英]Cannot add plugin to gradle build file?
我是Gradle的新手,但確實很難從Maven進行切換。
我嘗試按照本指南將Google格式化程序添加到我的版本中。
理想情況下,我希望每次有人運行gradle build時檢查代碼的格式是否正確。 我嘗試了以下策略。 效果不是很好。 這看起來像是精心設計的gradle文件嗎,我該如何使用該Google格式化程序呢?
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.6"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
apply plugin: 'com.github.sherter.google-java-format'
group = 'com.remindful'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-jersey')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-web-services')
compile('org.springframework.boot:spring-boot-starter-websocket')
compile('org.springframework.session:spring-session-core')
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.3.5.RELEASE")
compile("com.h2database:h2:1.4.191")
compile group: 'com.h2database', name: 'h2', version: '1.4.197'
testCompile 'junit:junit:4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.1.RELEASE'
}
task format(type: GoogleJavaFormat) {
source 'src/main'
source 'src/test'
include '**/*.java'
exclude '**/*Template.java'
}
task verifyFormatting(type: VerifyGoogleJavaFormat) {
source 'src/main'
include '**/*.java'
ignoreFailures true
}
// To force debug on application boot, switch suspend to y
bootRun {
systemProperties System.properties
jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"]
}
您已經定義了適當的任務- format
和verifyFormatting
但尚未將其合並到構建中。 因此,您應該能夠運行
./gradlew format
和
./gradlew verifyFormatting
但是./gradlew build
將不會對此進行任何格式化或驗證。
您需要決定的第一件事是何時要運行這些任務。 我將把format
保留為手動步驟,即有人應該顯式運行該任務以格式化其代碼。 在這種情況下,您什么也不做,因為./gradlew format
應該已經可以工作了。
另一方面, verifyFormatting
應該始終為完整版本運行。 Maven讓您將此類任務附加到特定階段 ,但這不是Gradle使用的模型。 相反,它與任務圖相互協作,在其中任務之間相互依賴。 您的目標是將verifyFormatting
任務插入該圖中的適當位置。
鑒於此,第二件事是將verifyFormatting
附加到適當的任務。 這將取決於您的要求,但是在這種特定情況下,我建議使用Base Plugin提供的check
任務。 該插件由Java插件和許多其他插件自動應用,因此通常可以使用。
check
任務已經取決於test
任務,因此它是理想的選擇。 只需將其添加到您的構建腳本中:
task verifyFormatting(...) {
...
}
check.dependsOn verifyFormatting
請注意, build
任務已經取決於check
,因此./gradlew build
現在還將運行verifyFormatting
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.