这里是 Java 8 和 Gradle 4.6。我有一个带有以下 build.gradle
的 Spring Boot 应用程序:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE")
}
}
plugins {
id 'java-library'
id 'checkstyle'
id 'jacoco'
}
apply plugin: 'org.springframework.boot'
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
configurations {
dev
}
dependencies {
compile(
,'ch.qos.logback:logback-classic:1.2.3'
,'org.slf4j:jul-to-slf4j:1.7.25'
,'org.apache.logging.log4j:log4j-to-slf4j:2.9.1'
,'org.apache.commons:commons-lang3:3.7'
,'org.springframework.boot:spring-boot-starter-actuator'
,'org.apache.commons:commons-text:1.2'
,'commons-beanutils:commons-beanutils-core:1.8.3'
)
testCompile(
'junit:junit:4.12'
,'org.mockito:mockito-core:2.23.0'
)
dev('org.springframework.boot:spring-boot-devtools')
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
String buildName = 'myapp'
jar {
baseName = buildName
}
bootRun {
if(project.hasProperty('debugMode')) {
jvmArgs = [ "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" ]
}
classpath = sourceSets.main.runtimeClasspath + configurations.dev
}
checkstyle {
toolVersion = '8.12'
ignoreFailures = false
}
jacocoTestReport {
reports {
xml.enabled false
html.enabled true
html.destination file("${buildDir}/reports/jacoco/")
}
}
check.dependsOn jacocoTestCoverageVerification
jacocoTestCoverageVerification.dependsOn jacocoTestReport
这是一个 Spring Boot Java 应用程序,也使用 Checkstyle和 Jacoco Gradle 插件。
我认为“完整构建”是这样的调用:
- 编译
- 运行 Checkstyle
- 运行单元测试 (JUnit)
- 运行 Jacoco 进行代码覆盖率分析
- 使用 Spring Boot 的库构建“胖”(自包含)可执行 jar
鉴于我当前的 Gradle 构建文件,我运行完整的构建,如下所示:
./gradlew clean build
但是,这可能需要几分钟才能完成所有单元测试,并且变得很麻烦。我想介绍一个 “快速构建”选项仅编译代码并为我创建 Spring Boot fat jar。这将有助于加快开发速度 时间非常长。
我希望像这样调用快速构建:
./gradlew clean quickbuild
到目前为止我已经得到了这个:
tasks.register("quickbuild") {
doLast {
// ???
}
}
但不确定如何将编译和 fatjar 任务链接到它(更重要的是;跳过我不想要的所有其他内容!)。 关于我缺少什么有什么想法吗?
<小时/>更新
bootJar
任务似乎不存在或未配置(请检查上面提供的 build.gradle
文件!):
$ ./gradlew clean bootJar
FAILURE: Build failed with an exception.
* What went wrong:
Task 'bootJar' not found in root project 'myapp'. Some candidates are: 'bootRun'.
* Try:
Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
当我尝试运行 bootRun
时:
$ ./gradlew clean bootRun
它尝试实际运行我的应用程序!那不是我想要的!我只想编译并构建 fat jar!
最佳答案
请参阅此处 Java 插件的文档:https://docs.gradle.org/current/userguide/java_plugin.html#lifecycle_tasks
您可以创建一个新任务(quickbuild
)并使其依赖于所需的任务(在您的情况下,我猜它可能是assemble
生命周期任务,或者可能是 bootJar
任务(对于 SpringBoot v2.x)或 bootRepackage
(对于 SpringBoot v1.5.x))
tasks.register('quickbuild'){
dependsOn assemble
}
但是如果quickbuild
任务的唯一目的是触发Jar的创建,最简单的解决方案是直接执行assemble
./gradlew clean assemble
关于java - Spring Boot Gradle 快速构建,跳过非必要内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54293205/