Skip to content

Commit bba3ea1

Browse files
authored
Modernize the maven build (#1496)
2 parents 9f24a34 + b256bd4 commit bba3ea1

23 files changed

+235
-526
lines changed

.github/workflows/ci.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ jobs:
3030
- name: spotlessCheck
3131
run: ./gradlew spotlessCheck --build-cache
3232
- name: assemble testClasses
33-
run: ./gradlew assemble testClasses --build-cache -PSPOTLESS_EXCLUDE_MAVEN=true
34-
# If this gets resolved, remove the EXCLUDE_MAVEN https://github.com/diffplug/spotless/issues/554
33+
run: ./gradlew assemble testClasses --build-cache
3534
build:
3635
needs: sanityCheck
3736
strategy:

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111

1212
## [Unreleased]
1313
### Added
14+
* `ProcessRunner` has added some convenience methods so it can be used for maven testing. ([#1496](https://github.com/diffplug/spotless/pull/1496))
1415
### Fixed
1516
* The default list of type annotations used by `formatAnnotations` has had 8 more annotations from the Checker Framework added [#1494](https://github.com/diffplug/spotless/pull/1494)
1617
### Changes

gradle.properties

+1-7
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,4 @@ VER_DURIAN=1.2.0
2828
VER_JGIT=5.13.1.202206130422-r
2929
VER_JUNIT=5.9.2
3030
VER_ASSERTJ=3.24.2
31-
VER_MOCKITO=4.11.0
32-
33-
# Used for Maven Plugin
34-
VER_MAVEN_API=3.0
35-
VER_ECLIPSE_AETHER=1.1.0
36-
VER_MUSTACHE=0.9.10
37-
VER_PLEXUS_RESOURCES=1.2.0
31+
VER_MOCKITO=4.11.0

lib-extra/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ dependencies {
2525
// we'll hold the core lib to a high standard
2626
spotbugs { reportLevel = 'low' } // low|medium|high (low = sensitive to even minor mistakes)
2727

28-
test {
29-
useJUnitPlatform()
28+
apply from: rootProject.file('gradle/special-tests.gradle')
29+
tasks.named('test') {
3030
if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16)) {
3131
// needed for EclipseCdtFormatterStepTest
3232
jvmArgs '--add-opens=java.base/java.lang=ALL-UNNAMED'

lib/build.gradle

+1-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ dependencies {
112112
// we'll hold the core lib to a high standard
113113
spotbugs { reportLevel = 'low' } // low|medium|high (low = sensitive to even minor mistakes)
114114

115-
tasks.withType(Test).configureEach {
116-
useJUnitPlatform()
117-
}
118-
115+
apply from: rootProject.file('gradle/special-tests.gradle')
119116
jar {
120117
for (glue in NEEDS_GLUE) {
121118
from sourceSets.getByName(glue).output.classesDirs

lib/src/main/java/com/diffplug/spotless/ProcessRunner.java

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,18 +16,22 @@
1616
package com.diffplug.spotless;
1717

1818
import java.io.ByteArrayOutputStream;
19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.io.OutputStream;
2223
import java.nio.charset.Charset;
24+
import java.nio.charset.StandardCharsets;
2325
import java.util.Arrays;
2426
import java.util.List;
27+
import java.util.Map;
2528
import java.util.concurrent.ExecutionException;
2629
import java.util.concurrent.ExecutorService;
2730
import java.util.concurrent.Executors;
2831
import java.util.concurrent.Future;
2932
import java.util.function.BiConsumer;
3033

34+
import edu.umd.cs.findbugs.annotations.Nullable;
3135
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3236

3337
/**
@@ -55,13 +59,18 @@ public Result shell(String cmd) throws IOException, InterruptedException {
5559

5660
/** Executes the given shell command (using {@code cmd} on windows and {@code sh} on unix). */
5761
public Result shellWinUnix(String cmdWin, String cmdUnix) throws IOException, InterruptedException {
62+
return shellWinUnix(null, null, cmdWin, cmdUnix);
63+
}
64+
65+
/** Executes the given shell command (using {@code cmd} on windows and {@code sh} on unix). */
66+
public Result shellWinUnix(@Nullable File cwd, @Nullable Map<String, String> environment, String cmdWin, String cmdUnix) throws IOException, InterruptedException {
5867
List<String> args;
5968
if (FileSignature.machineIsWin()) {
6069
args = Arrays.asList("cmd", "/c", cmdWin);
6170
} else {
6271
args = Arrays.asList("sh", "-c", cmdUnix);
6372
}
64-
return exec(args);
73+
return exec(cwd, environment, null, args);
6574
}
6675

6776
/** Creates a process with the given arguments. */
@@ -70,18 +79,32 @@ public Result exec(String... args) throws IOException, InterruptedException {
7079
}
7180

7281
/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
73-
public Result exec(byte[] stdin, String... args) throws IOException, InterruptedException {
82+
public Result exec(@Nullable byte[] stdin, String... args) throws IOException, InterruptedException {
7483
return exec(stdin, Arrays.asList(args));
7584
}
7685

7786
/** Creates a process with the given arguments. */
7887
public Result exec(List<String> args) throws IOException, InterruptedException {
79-
return exec(new byte[0], args);
88+
return exec(null, args);
8089
}
8190

8291
/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
83-
public Result exec(byte[] stdin, List<String> args) throws IOException, InterruptedException {
92+
public Result exec(@Nullable byte[] stdin, List<String> args) throws IOException, InterruptedException {
93+
return exec(null, null, stdin, args);
94+
}
95+
96+
/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
97+
public Result exec(@Nullable File cwd, @Nullable Map<String, String> environment, @Nullable byte[] stdin, List<String> args) throws IOException, InterruptedException {
8498
ProcessBuilder builder = new ProcessBuilder(args);
99+
if (cwd != null) {
100+
builder.directory(cwd);
101+
}
102+
if (environment != null) {
103+
builder.environment().putAll(environment);
104+
}
105+
if (stdin == null) {
106+
stdin = new byte[0];
107+
}
85108
Process process = builder.start();
86109
Future<byte[]> outputFut = threadStdOut.submit(() -> drainToBytes(process.getInputStream(), bufStdOut));
87110
Future<byte[]> errorFut = threadStdErr.submit(() -> drainToBytes(process.getErrorStream(), bufStdErr));
@@ -147,6 +170,14 @@ public byte[] stdErr() {
147170
return stdErr;
148171
}
149172

173+
public String stdOutUtf8() {
174+
return new String(stdOut, StandardCharsets.UTF_8);
175+
}
176+
177+
public String stdErrUtf8() {
178+
return new String(stdErr, StandardCharsets.UTF_8);
179+
}
180+
150181
/** Returns true if the exit code was not zero. */
151182
public boolean exitNotZero() {
152183
return exitCode != 0;

plugin-gradle/build.gradle

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ dependencies {
2727
testImplementation "com.diffplug.durian:durian-testlib:${VER_DURIAN}"
2828
}
2929

30-
test {
31-
useJUnitPlatform()
30+
apply from: rootProject.file('gradle/special-tests.gradle')
31+
tasks.named('test') {
3232
testLogging.showStandardStreams = true
3333
}
3434

35-
apply from: rootProject.file('gradle/special-tests.gradle')
36-
3735
//////////////////////////
3836
// GRADLE PLUGIN PORTAL //
3937
//////////////////////////

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
### Fixed
88
* The default list of type annotations used by `formatAnnotations` has had 8 more annotations from the Checker Framework added [#1494](https://github.com/diffplug/spotless/pull/1494)
99
### Changes
10+
* Spotless' custom build was replaced by [`maven-plugin-development`](https://github.com/britter/maven-plugin-development). ([#1496](https://github.com/diffplug/spotless/pull/1496) fixes [#554](https://github.com/diffplug/spotless/issues/554))
1011

1112
## [2.30.0] - 2023-01-13
1213
### Added

plugin-maven/build.gradle

+23-165
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,29 @@
1-
buildscript {
2-
repositories { mavenCentral() }
3-
dependencies { classpath "com.github.spullara.mustache.java:compiler:${VER_MUSTACHE}" }
4-
}
51
plugins {
6-
id 'cz.malohlava.visteg' version '1.0.5' // https://github.com/mmalohlava/gradle-visteg
2+
// https://www.benediktritter.de/maven-plugin-development/#release-history
3+
id 'de.benediktritter.maven-plugin-development' version '0.4.0'
74
}
5+
86
repositories { mavenCentral() }
97
apply from: rootProject.file('gradle/changelog.gradle')
10-
apply from: rootProject.file('gradle/spotless-freshmark.gradle')
11-
12-
// to generate taskGraph.pdf
13-
// - set enabled (below) to true
14-
// - run: ./gradlew :plugin-maven:test
15-
// - run: rm plugin-maven/output.pdf
16-
// - run: dot -Tpdf plugin-maven/build/reports/visteg.dot > plugin-maven/taskGraph.pdf
17-
visteg {
18-
enabled = false
19-
nodeShape = 'box'
20-
startNodeShape = 'box'
21-
endNodeShape = 'box'
22-
colorscheme = 'pastel24' // https://www.graphviz.org/doc/info/colors.html
23-
}
24-
25-
import com.github.mustachejava.DefaultMustacheFactory
26-
27-
import java.nio.file.Files
28-
29-
import static java.nio.charset.StandardCharsets.UTF_8
30-
import static java.nio.file.StandardOpenOption.CREATE
31-
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING
32-
338
ext.artifactId = project.artifactIdMaven
349
version = spotlessChangelog.versionNext
35-
apply from: rootProject.file("gradle/java-setup.gradle")
36-
apply from: rootProject.file("gradle/java-publish.gradle")
3710

38-
final MAVEN_PROJECT_DIR = project.layout.buildDirectory.dir("mavenProject").get()
39-
final LOCAL_MAVEN_REPO_DIR = project.layout.buildDirectory.dir("localMavenRepository").get()
11+
apply from: rootProject.file("gradle/java-setup.gradle")
12+
apply from: rootProject.file('gradle/spotless-freshmark.gradle')
4013

41-
def mvnw(String args) {
42-
boolean isWin = System.getProperty('os.name').toLowerCase().contains('win')
43-
if (isWin) {
44-
return [
45-
'cmd',
46-
'/c',
47-
'mvnw.cmd -e ' + args
48-
]
49-
} else {
50-
return [
51-
'/bin/sh',
52-
'-c',
53-
'./mvnw -e ' + args
54-
]
55-
}
14+
apply plugin: 'de.benediktritter.maven-plugin-development'
15+
mavenPlugin {
16+
name = 'Spotless Maven Plugin'
17+
artifactId = project.artifactIdMaven
18+
description = project.description
5619
}
5720

58-
String libVersion = version.endsWith('-SNAPSHOT') ?
59-
rootProject.spotlessChangelog.versionNext :
60-
rootProject.spotlessChangelog.versionLast
21+
String VER_MAVEN_API = '3.0'
22+
String VER_ECLIPSE_AETHER = '1.1.0'
23+
String VER_PLEXUS_RESOURCES = '1.2.0'
6124
dependencies {
62-
if (version.endsWith('-SNAPSHOT') || (rootProject.spotlessChangelog.versionNext == rootProject.spotlessChangelog.versionLast)) {
63-
implementation project(':lib')
64-
implementation project(':lib-extra')
65-
} else {
66-
implementation "com.diffplug.spotless:spotless-lib:${libVersion}"
67-
implementation "com.diffplug.spotless:spotless-lib-extra:${libVersion}"
68-
}
25+
implementation project(':lib')
26+
implementation project(':lib-extra')
6927

7028
compileOnly "org.apache.maven:maven-plugin-api:${VER_MAVEN_API}"
7129
compileOnly "org.apache.maven.plugin-tools:maven-plugin-annotations:${VER_MAVEN_API}"
@@ -83,119 +41,19 @@ dependencies {
8341
testImplementation "org.assertj:assertj-core:${VER_ASSERTJ}"
8442
testImplementation "org.mockito:mockito-core:${VER_MOCKITO}"
8543
testImplementation "com.diffplug.durian:durian-io:${VER_DURIAN}"
86-
testImplementation "com.github.spullara.mustache.java:compiler:${VER_MUSTACHE}"
44+
testImplementation 'com.github.spullara.mustache.java:compiler:0.9.10'
8745
testImplementation "org.apache.maven:maven-plugin-api:${VER_MAVEN_API}"
8846
testImplementation "org.eclipse.aether:aether-api:${VER_ECLIPSE_AETHER}"
8947
testImplementation "org.codehaus.plexus:plexus-resources:${VER_PLEXUS_RESOURCES}"
9048
testImplementation "org.apache.maven:maven-core:${VER_MAVEN_API}"
9149
}
9250

93-
task copySourceFiles(type: Sync) {
94-
from "src/main/java"
95-
into MAVEN_PROJECT_DIR.dir("src/main/java")
96-
}
97-
98-
task copyMvnw(type: Copy, dependsOn: copySourceFiles) {
99-
from 'src/test/resources'
100-
include 'mvnw'
101-
include 'mvnw.cmd'
102-
include '.mvn/**'
103-
into MAVEN_PROJECT_DIR
104-
}
105-
106-
task installLocalDependencies
107-
def libs = [
108-
'lib',
109-
'lib-extra',
110-
'testlib'
111-
]
112-
libs.each {
113-
def groupId = 'com.diffplug.spotless'
114-
def artifactId = "spotless-${it}"
115-
def jarTask = tasks.getByPath(":${it}:jar")
116-
def file = jarTask.archivePath
117-
118-
def installDependency = task "install_${artifactId}"(type: Exec) {
119-
workingDir MAVEN_PROJECT_DIR
120-
121-
inputs.file(file)
122-
outputs.dir(LOCAL_MAVEN_REPO_DIR.file(groupId.replace('.', '/') + "/" + artifactId + "/" + version))
123-
commandLine mvnw("org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file " +
124-
"-Dfile=${file} " +
125-
"-DgroupId=${groupId} " +
126-
"-DartifactId=${artifactId} " +
127-
"-Dversion=${libVersion} " +
128-
"-Dpackaging=jar " +
129-
"-DlocalRepositoryPath=${LOCAL_MAVEN_REPO_DIR}")
130-
}
131-
installDependency.dependsOn(jarTask)
132-
133-
installLocalDependencies.dependsOn installDependency
134-
}
135-
136-
task createPomXml(dependsOn: installLocalDependencies) {
137-
def newPomXml = MAVEN_PROJECT_DIR.file("pom.xml").asFile.toPath()
138-
139-
outputs.file(newPomXml)
140-
doLast {
141-
def additionalDependencies = project.configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.findAll {
142-
return !libs.contains(it.moduleVersion.id.name)
143-
}.collect {
144-
return " <dependency>\n" +
145-
" <groupId>${it.moduleVersion.id.group}</groupId>\n" +
146-
" <artifactId>${it.moduleVersion.id.name}</artifactId>\n" +
147-
" <version>${it.moduleVersion.id.version}</version>\n" +
148-
" </dependency>\n"
149-
}.join()
150-
151-
def versions = [
152-
spotlessMavenPluginVersion: version,
153-
mavenApiVersion : VER_MAVEN_API,
154-
eclipseAetherVersion : VER_ECLIPSE_AETHER,
155-
spotlessLibVersion : libVersion,
156-
jsr305Version : VER_JSR_305,
157-
additionalDependencies : additionalDependencies
158-
]
159-
160-
def pomXmlTemplate = project.layout.projectDirectory.file("src/test/resources/pom-build.xml.mustache").asFile.toPath()
161-
162-
Files.newBufferedReader(pomXmlTemplate).withCloseable { reader ->
163-
Files.newBufferedWriter(newPomXml, UTF_8, CREATE, TRUNCATE_EXISTING).withCloseable { writer ->
164-
def mustache = new DefaultMustacheFactory().compile(reader, "pom")
165-
mustache.execute(writer, versions)
166-
}
167-
}
168-
}
169-
}
170-
171-
task runMavenBuild(type: Exec, dependsOn: [
172-
copySourceFiles,
173-
copyMvnw,
174-
createPomXml
175-
]) {
176-
outputs.dir(LOCAL_MAVEN_REPO_DIR)
177-
178-
workingDir MAVEN_PROJECT_DIR
179-
// -B batch mode to make dependency download logging less verbose
180-
commandLine mvnw("clean install -B -Dmaven.repo.local=${LOCAL_MAVEN_REPO_DIR}")
181-
}
182-
183-
jar.setActions Arrays.asList()
184-
jar.dependsOn(runMavenBuild)
185-
File jarIn = MAVEN_PROJECT_DIR.file("target/spotless-maven-plugin-${version}.jar").asFile
186-
File jarOut = jar.archivePath
187-
jar.inputs.file(jarIn)
188-
jar.outputs.file(jarOut)
189-
jar.doLast {
190-
Files.copy(jarIn.toPath(), jarOut.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING)
191-
}
192-
193-
test { useJUnitPlatform() }
194-
19551
apply from: rootProject.file('gradle/special-tests.gradle')
196-
197-
tasks.withType(Test) {
198-
systemProperty "localMavenRepositoryDir", LOCAL_MAVEN_REPO_DIR.asFile
199-
systemProperty "spotlessMavenPluginVersion", project.version
200-
dependsOn(jar)
52+
tasks.withType(Test).configureEach {
53+
systemProperty 'spotlessMavenPluginVersion', project.version
54+
dependsOn 'publishToMavenLocal'
55+
dependsOn ':lib:publishToMavenLocal'
56+
dependsOn ':lib-extra:publishToMavenLocal'
20157
}
58+
59+
apply from: rootProject.file("gradle/java-publish.gradle")

0 commit comments

Comments
 (0)