Skip to content

Commit 24a32c2

Browse files
authored
Specify additional task outputs to improve the Spotless build process (#1237)
2 parents 9778473 + 9940d74 commit 24a32c2

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

gradle/java-setup.gradle

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ apply plugin: 'com.github.spotbugs'
1717
spotbugs {
1818
toolVersion = VER_SPOTBUGS
1919
ignoreFailures = false // bug free or it doesn't ship!
20-
reportsDir = file('build/spotbugs')
2120
reportLevel = 'medium' // low|medium|high (low = sensitive to even minor mistakes)
2221
omitVisitors = [
2322
'FindReturnRef'] // https://spotbugs.readthedocs.io/en/latest/detectors.html#findreturnref
2423
}
2524
tasks.named('spotbugsTest') {
2625
enabled = false
2726
}
27+
28+
tasks.withType(com.github.spotbugs.snom.SpotBugsTask).configureEach {
29+
outputs.file(project.layout.buildDirectory.file("reports/spotbugs/${it.name}.html"))
30+
outputs.file(project.layout.buildDirectory.file("spotbugs/auxclasspath/${it.name}"))
31+
reports {
32+
html.enabled = true
33+
}
34+
}
35+
2836
tasks.named('spotbugsMain') {
2937
// only run on Java 8 (no benefit to running twice)
3038
enabled = org.gradle.api.JavaVersion.current() == org.gradle.api.JavaVersion.VERSION_11

plugin-maven/build.gradle

+15-17
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,18 @@ visteg {
2424
import com.github.mustachejava.DefaultMustacheFactory
2525

2626
import java.nio.file.Files
27-
import java.nio.file.Paths
2827

2928
import static java.nio.charset.StandardCharsets.UTF_8
30-
import static java.nio.file.StandardOpenOption.CREATE_NEW
29+
import static java.nio.file.StandardOpenOption.CREATE
3130
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING
3231

3332
ext.artifactId = project.artifactIdMaven
3433
version = spotlessChangelog.versionNext
3534
apply from: rootProject.file("gradle/java-setup.gradle")
3635
apply from: rootProject.file("gradle/java-publish.gradle")
3736

38-
final PROJECT_DIR = project.projectDir.toString()
39-
final BUILD_DIR = project.buildDir.toString()
40-
final MAVEN_PROJECT_DIR = "${BUILD_DIR}/mavenProject"
41-
final LOCAL_MAVEN_REPO_DIR = "${BUILD_DIR}/localMavenRepository"
37+
final MAVEN_PROJECT_DIR = project.layout.buildDirectory.dir("mavenProject").get()
38+
final LOCAL_MAVEN_REPO_DIR = project.layout.buildDirectory.dir("localMavenRepository").get()
4239

4340
def mvnw(String args) {
4441
boolean isWin = System.getProperty('os.name').toLowerCase().contains('win')
@@ -91,11 +88,9 @@ dependencies {
9188
testImplementation "org.apache.maven:maven-core:${VER_MAVEN_API}"
9289
}
9390

94-
task cleanMavenProjectDir(type: Delete) { delete MAVEN_PROJECT_DIR }
95-
96-
task copySourceFiles(type: Sync, dependsOn: cleanMavenProjectDir) {
91+
task copySourceFiles(type: Sync) {
9792
from "src/main/java"
98-
into "${MAVEN_PROJECT_DIR}/src/main/java"
93+
into MAVEN_PROJECT_DIR.dir("src/main/java")
9994
}
10095

10196
task copyMvnw(type: Copy, dependsOn: copySourceFiles) {
@@ -122,7 +117,7 @@ libs.each {
122117
workingDir MAVEN_PROJECT_DIR
123118

124119
inputs.file(file)
125-
outputs.dir(project.file("${LOCAL_MAVEN_REPO_DIR}/${groupId.replace('.', '/')}/${artifactId}/${version}"))
120+
outputs.dir(LOCAL_MAVEN_REPO_DIR.file(groupId.replace('.', '/') + "/" + artifactId + "/" + version))
126121
commandLine mvnw("org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file " +
127122
"-Dfile=${file} " +
128123
"-DgroupId=${groupId} " +
@@ -137,6 +132,9 @@ libs.each {
137132
}
138133

139134
task createPomXml(dependsOn: installLocalDependencies) {
135+
def newPomXml = MAVEN_PROJECT_DIR.file("pom.xml").asFile.toPath()
136+
137+
outputs.file(newPomXml)
140138
doLast {
141139
def additionalDependencies = project.configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.findAll {
142140
return !libs.contains(it.moduleVersion.id.name)
@@ -157,11 +155,10 @@ task createPomXml(dependsOn: installLocalDependencies) {
157155
additionalDependencies : additionalDependencies
158156
]
159157

160-
def pomXmlTemplate = Paths.get(PROJECT_DIR, "src/test/resources/pom-build.xml.mustache")
161-
def newPomXml = Paths.get(MAVEN_PROJECT_DIR, "pom.xml")
158+
def pomXmlTemplate = project.layout.projectDirectory.file("src/test/resources/pom-build.xml.mustache").asFile.toPath()
162159

163160
Files.newBufferedReader(pomXmlTemplate).withCloseable { reader ->
164-
Files.newBufferedWriter(newPomXml, UTF_8, CREATE_NEW, TRUNCATE_EXISTING).withCloseable { writer ->
161+
Files.newBufferedWriter(newPomXml, UTF_8, CREATE, TRUNCATE_EXISTING).withCloseable { writer ->
165162
def mustache = new DefaultMustacheFactory().compile(reader, "pom")
166163
mustache.execute(writer, versions)
167164
}
@@ -170,19 +167,20 @@ task createPomXml(dependsOn: installLocalDependencies) {
170167
}
171168

172169
task runMavenBuild(type: Exec, dependsOn: [
173-
cleanMavenProjectDir,
174170
copySourceFiles,
175171
copyMvnw,
176172
createPomXml
177173
]) {
174+
outputs.dir(LOCAL_MAVEN_REPO_DIR)
175+
178176
workingDir MAVEN_PROJECT_DIR
179177
// -B batch mode to make dependency download logging less verbose
180178
commandLine mvnw("clean install -B -Dmaven.repo.local=${LOCAL_MAVEN_REPO_DIR}")
181179
}
182180

183181
jar.setActions Arrays.asList()
184182
jar.dependsOn(runMavenBuild)
185-
File jarIn = file("${MAVEN_PROJECT_DIR}/target/spotless-maven-plugin-${version}.jar")
183+
File jarIn = MAVEN_PROJECT_DIR.file("target/spotless-maven-plugin-${version}.jar").asFile
186184
File jarOut = jar.archivePath
187185
jar.inputs.file(jarIn)
188186
jar.outputs.file(jarOut)
@@ -195,7 +193,7 @@ test { useJUnitPlatform() }
195193
apply from: rootProject.file('gradle/special-tests.gradle')
196194

197195
tasks.withType(Test) {
198-
systemProperty "localMavenRepositoryDir", LOCAL_MAVEN_REPO_DIR
196+
systemProperty "localMavenRepositoryDir", LOCAL_MAVEN_REPO_DIR.asFile
199197
systemProperty "spotlessMavenPluginVersion", project.version
200198
dependsOn(jar)
201199
}

0 commit comments

Comments
 (0)