Skip to content

Commit

Permalink
Specify additional task outputs to improve the Spotless build process (
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jun 14, 2022
2 parents 9778473 + 9940d74 commit 24a32c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
10 changes: 9 additions & 1 deletion gradle/java-setup.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ apply plugin: 'com.github.spotbugs'
spotbugs {
toolVersion = VER_SPOTBUGS
ignoreFailures = false // bug free or it doesn't ship!
reportsDir = file('build/spotbugs')
reportLevel = 'medium' // low|medium|high (low = sensitive to even minor mistakes)
omitVisitors = [
'FindReturnRef'] // https://spotbugs.readthedocs.io/en/latest/detectors.html#findreturnref
}
tasks.named('spotbugsTest') {
enabled = false
}

tasks.withType(com.github.spotbugs.snom.SpotBugsTask).configureEach {
outputs.file(project.layout.buildDirectory.file("reports/spotbugs/${it.name}.html"))
outputs.file(project.layout.buildDirectory.file("spotbugs/auxclasspath/${it.name}"))
reports {
html.enabled = true
}
}

tasks.named('spotbugsMain') {
// only run on Java 8 (no benefit to running twice)
enabled = org.gradle.api.JavaVersion.current() == org.gradle.api.JavaVersion.VERSION_11
Expand Down
32 changes: 15 additions & 17 deletions plugin-maven/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ visteg {
import com.github.mustachejava.DefaultMustacheFactory

import java.nio.file.Files
import java.nio.file.Paths

import static java.nio.charset.StandardCharsets.UTF_8
import static java.nio.file.StandardOpenOption.CREATE_NEW
import static java.nio.file.StandardOpenOption.CREATE
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING

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

final PROJECT_DIR = project.projectDir.toString()
final BUILD_DIR = project.buildDir.toString()
final MAVEN_PROJECT_DIR = "${BUILD_DIR}/mavenProject"
final LOCAL_MAVEN_REPO_DIR = "${BUILD_DIR}/localMavenRepository"
final MAVEN_PROJECT_DIR = project.layout.buildDirectory.dir("mavenProject").get()
final LOCAL_MAVEN_REPO_DIR = project.layout.buildDirectory.dir("localMavenRepository").get()

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

task cleanMavenProjectDir(type: Delete) { delete MAVEN_PROJECT_DIR }

task copySourceFiles(type: Sync, dependsOn: cleanMavenProjectDir) {
task copySourceFiles(type: Sync) {
from "src/main/java"
into "${MAVEN_PROJECT_DIR}/src/main/java"
into MAVEN_PROJECT_DIR.dir("src/main/java")
}

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

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

task createPomXml(dependsOn: installLocalDependencies) {
def newPomXml = MAVEN_PROJECT_DIR.file("pom.xml").asFile.toPath()

outputs.file(newPomXml)
doLast {
def additionalDependencies = project.configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.findAll {
return !libs.contains(it.moduleVersion.id.name)
Expand All @@ -157,11 +155,10 @@ task createPomXml(dependsOn: installLocalDependencies) {
additionalDependencies : additionalDependencies
]

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

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

task runMavenBuild(type: Exec, dependsOn: [
cleanMavenProjectDir,
copySourceFiles,
copyMvnw,
createPomXml
]) {
outputs.dir(LOCAL_MAVEN_REPO_DIR)

workingDir MAVEN_PROJECT_DIR
// -B batch mode to make dependency download logging less verbose
commandLine mvnw("clean install -B -Dmaven.repo.local=${LOCAL_MAVEN_REPO_DIR}")
}

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

tasks.withType(Test) {
systemProperty "localMavenRepositoryDir", LOCAL_MAVEN_REPO_DIR
systemProperty "localMavenRepositoryDir", LOCAL_MAVEN_REPO_DIR.asFile
systemProperty "spotlessMavenPluginVersion", project.version
dependsOn(jar)
}

0 comments on commit 24a32c2

Please sign in to comment.