java - 无法将插件添加到Gradle构建文件中?

标签 java gradle

我是Gradle的新手,但确实很难从Maven进行切换。

我尝试遵循this guide将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"]
}

最佳答案

您已经定义了适当的任务-formatverifyFormatting-但尚未将其合并到构建中。因此,您应该能够运行
./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

关于java - 无法将插件添加到Gradle构建文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49933616/

相关文章:

android - android在gradle中添加firebase auth依赖项后显示以下错误

java - 如何生成多项目合一的 Gradle 项目?

java - 调用 RedisOperationsSessionRepository.delete 时出现 NoSuchMethodError

java - 如何停止在控制台上打印异常堆栈跟踪?

java - 如何将输入源转换为输入流,并将其作为参数提供给字符串读取器?

gradle - 将平台约束应用于所有配置

maven - Intellij IDEA 15 : Unindexed remote maven repositories found

java - IntelliJ 不使用本地存储库进行 Maven 构建

java - 字符串检查 while 循环无法正常工作

android - 多模块 gradle 项目 - 执行所有单元测试