Skip to content

Commit a1ef6e6

Browse files
Excavator: Allow Java 17 language features in all modules (#2898)
1 parent eb4cce4 commit a1ef6e6

File tree

11 files changed

+99
-71
lines changed

11 files changed

+99
-71
lines changed

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ allprojects {
6767
}
6868

6969
javaVersions {
70-
libraryTarget = 11
71-
runtime = 11
70+
libraryTarget = 17
7271
}
7372

7473
jdks {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: break
2+
break:
3+
description: All modules now require Java 17.
4+
links:
5+
- https://github.com/palantir/gradle-baseline/pull/2898

gradle-baseline-java/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies {
2525
implementation 'org.ow2.asm:asm'
2626
implementation 'com.googlecode.java-diff-utils:diffutils'
2727
implementation 'com.palantir.gradle.utils:lazily-configured-mapping'
28+
implementation 'com.palantir.gradle.utils:environment-variables'
2829
implementation 'com.palantir.gradle.failure-reports:gradle-failure-reports-exceptions'
2930

3031
runtimeOnly 'com.palantir.javaformat:gradle-palantir-java-format'
@@ -62,6 +63,7 @@ tasks.test.dependsOn tasks.publishToMavenLocal
6263
test {
6364
environment 'CIRCLE_ARTIFACTS', "${buildDir}/artifacts"
6465
environment 'CIRCLE_TEST_REPORTS', "${buildDir}/circle-reports"
66+
6567
systemProperty 'ignoreDeprecations', 'true'
6668
}
6769

gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/BaselineCircleCi.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.base.Preconditions;
2020
import com.google.common.base.Splitter;
21+
import com.palantir.gradle.utils.environmentvariables.EnvironmentVariables;
2122
import java.io.File;
2223
import java.io.IOException;
2324
import java.nio.file.Files;
@@ -38,16 +39,21 @@ public final class BaselineCircleCi implements Plugin<Project> {
3839

3940
@Override
4041
public void apply(Project project) {
41-
configurePluginsForReports(project);
42-
configurePluginsForArtifacts(project);
42+
EnvironmentVariables environmentVariables = project.getObjects().newInstance(EnvironmentVariables.class);
43+
44+
configurePluginsForReports(project, environmentVariables);
45+
configurePluginsForArtifacts(project, environmentVariables);
4346
Preconditions.checkState(
4447
!project.getName().equals("project"),
4548
"Please specify rootProject.name in your settings.gradle, otherwise CircleCI's"
4649
+ " checkout dir ('project') will be used instead.");
4750
}
4851

49-
private void configurePluginsForArtifacts(Project project) {
50-
String circleArtifactsDir = System.getenv("CIRCLE_ARTIFACTS");
52+
private void configurePluginsForArtifacts(Project project, EnvironmentVariables environmentVariables) {
53+
String circleArtifactsDir = environmentVariables
54+
.envVarOrFromTestingProperty("CIRCLE_ARTIFACTS")
55+
.getOrNull();
56+
5157
if (circleArtifactsDir == null) {
5258
project.getLogger().info("$CIRCLE_ARTIFACTS variable is not set, not configuring junit/profiling reports");
5359
return;
@@ -66,8 +72,11 @@ private void configurePluginsForArtifacts(Project project) {
6672
}));
6773
}
6874

69-
private void configurePluginsForReports(Project project) {
70-
String circleReportsDir = System.getenv("CIRCLE_TEST_REPORTS");
75+
private void configurePluginsForReports(Project project, EnvironmentVariables environmentVariables) {
76+
String circleReportsDir = environmentVariables
77+
.envVarOrFromTestingProperty("CIRCLE_TEST_REPORTS")
78+
.getOrNull();
79+
7180
if (circleReportsDir == null) {
7281
return;
7382
}

gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/BaselineImmutables.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public Iterable<String> asArguments() {
5555
: Collections.emptyList();
5656
}
5757
});
58+
59+
// This *attempts* to make immutables work by add the exports to the fork options.
60+
// However, this only happens if the compilation is actually forked, which at the time
61+
// of writing is not always the case. Gradle will fork the compiler if the version of
62+
// Java required by the compiler is different to the one running the Gradle daemon. But
63+
// if they are the same, it will not fork and this extra export option **will have no
64+
// effect**.
5865
javaCompileTask
5966
.getOptions()
6067
.getForkOptions()

gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineCircleCiJavaIntegrationTests.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,25 @@
2929
import org.junit.Before;
3030
import org.junit.Rule;
3131
import org.junit.Test;
32-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
3332
import org.junit.rules.TemporaryFolder;
3433

3534
public class BaselineCircleCiJavaIntegrationTests {
36-
37-
@Rule
38-
public final EnvironmentVariables env = new EnvironmentVariables();
39-
4035
@Rule
4136
public final TemporaryFolder projectDir = new TemporaryFolder();
4237

4338
private File reportsDir;
4439

4540
@Before
46-
public void before() {
41+
public void before() throws IOException {
4742
reportsDir = new File(projectDir.getRoot(), "circle/reports");
48-
env.set("CIRCLE_TEST_REPORTS", reportsDir.toString());
43+
44+
java.nio.file.Files.writeString(
45+
projectDir.newFile("gradle.properties").toPath(),
46+
"""
47+
__TESTING=true
48+
__TESTING_CIRCLE_TEST_REPORTS=%s
49+
"""
50+
.formatted(reportsDir.toString()));
4951

5052
copyTestFile("build.gradle", projectDir, "build.gradle");
5153
copyTestFile("subproject.gradle", projectDir, "subproject/build.gradle");

gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatIntegrationTest.groovy

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest {
3737
new File("../gradle-baseline-java-config/resources"),
3838
new File(projectDir, ".baseline"))
3939
// Disable copyright by default so we can test it individually
40-
file('gradle.properties') << "com.palantir.baseline-format.copyright=false\n"
40+
file('gradle.properties') << '''
41+
com.palantir.baseline-format.copyright=false
42+
# Required for the eclipse formatter. Delete once it's removed.
43+
org.gradle.jvmargs = --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
44+
'''.stripIndent(true)
4145
}
4246

47+
// language=Gradle
4348
def standardBuildFile = '''
4449
plugins {
4550
id 'java'
@@ -49,7 +54,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest {
4954
// to resolve the `palantirJavaFormat` configuration
5055
mavenCentral()
5156
}
52-
'''.stripIndent()
57+
'''.stripIndent(true)
5358

5459
def noJavaBuildFile = '''
5560
plugins {

gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineModuleJvmArgsIntegrationTest.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class BaselineModuleJvmArgsIntegrationTest extends IntegrationSpec {
4848
'''.stripIndent(true)
4949

5050
def setup() {
51-
setFork(true)
5251
buildFile << standardBuildFile
5352
}
5453

gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineImmutablesTest.groovy

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,38 @@ import nebula.test.functional.ExecutionResult
2222

2323
class BaselineImmutablesTest extends IntegrationSpec {
2424
private static final String IMMUTABLES = 'org.immutables:value:2.8.8'
25-
private static final String IMMUTABLES_ANNOTATIONS = 'org.immutables:value:2.8.8:annotations'
25+
private static final String IMMUTABLES_ANNOTATIONS = IMMUTABLES + ':annotations'
2626

27-
def 'inserts incremental compilation args into source sets that have immutables'() {
28-
buildFile << """
27+
def setup() {
28+
// language=Gradle
29+
buildFile << '''
2930
plugins {
3031
id 'org.unbroken-dome.test-sets' version '4.0.0'
3132
}
32-
33+
3334
apply plugin: 'com.palantir.baseline-immutables'
3435
apply plugin: 'java-library'
3536
3637
repositories {
3738
mavenCentral()
3839
}
3940
41+
task compileAll
42+
43+
tasks.withType(JavaCompile) { javaCompile ->
44+
doFirst {
45+
logger.lifecycle "Debug compiler args: \${javaCompile.name}: \${javaCompile.options.allCompilerArgs}"
46+
logger.lifecycle "Debug compiler fork args: \${javaCompile.name}: \${javaCompile.options.forkOptions.allJvmArgs}"
47+
logger.lifecycle "Debug compiler fork: \${javaCompile.name}: \${javaCompile.options.fork}"
48+
}
49+
50+
tasks.compileAll.dependsOn javaCompile
51+
}
52+
'''.stripIndent(true)
53+
}
54+
55+
def 'inserts incremental compilation args into source sets that have immutables'() {
56+
buildFile << """
4057
testSets {
4158
hasImmutables
4259
doesNotHaveImmutables
@@ -57,16 +74,6 @@ class BaselineImmutablesTest extends IntegrationSpec {
5774
5875
onlyHasImmutablesAnnotationsAnnotationProcessor '$IMMUTABLES_ANNOTATIONS'
5976
}
60-
61-
task compileAll
62-
63-
tasks.withType(JavaCompile) { javaCompile ->
64-
doFirst {
65-
logger.lifecycle "\${javaCompile.name}: \${javaCompile.options.allCompilerArgs}"
66-
}
67-
68-
tasks.compileAll.dependsOn javaCompile
69-
}
7077
""".stripIndent()
7178

7279
['main', 'hasImmutables', 'doesNotHaveImmutables', 'hasImmutablesAddedInAfterEvaluate', 'onlyHasImmutablesAnnotations'].each {
@@ -88,16 +95,15 @@ class BaselineImmutablesTest extends IntegrationSpec {
8895
}
8996

9097
def 'Compatible with java #javaVersion'() {
98+
// Context: https://github.com/immutables/immutables/issues/1379#issuecomment-1254224741
99+
91100
when:
92101
buildFile << """
93102
apply plugin: 'com.palantir.baseline-java-versions'
94-
apply plugin: 'com.palantir.baseline-immutables'
95-
apply plugin: 'java-library'
96-
repositories {
97-
mavenCentral()
98-
}
99103
tasks.withType(JavaCompile).configureEach({
100-
options.compilerArgs += ['-Werror']
104+
options.compilerArgs += ['-Werror']
105+
// See comment about fork options in BaselineImmutables
106+
options.fork = true
101107
})
102108
javaVersions {
103109
libraryTarget = $javaVersion
@@ -106,27 +112,33 @@ class BaselineImmutablesTest extends IntegrationSpec {
106112
annotationProcessor '$IMMUTABLES'
107113
compileOnly '$IMMUTABLES_ANNOTATIONS'
108114
}
109-
""".stripIndent()
110-
writeJavaSourceFile("""
111-
package com.palantir.one;
112-
import com.palantir.two.ImmutableTwo;
113-
import org.immutables.value.Value;
114-
@Value.Immutable
115-
public interface One {
116-
ImmutableTwo two();
117-
}
118-
""".stripIndent())
119-
writeJavaSourceFile("""
120-
package com.palantir.two;
121-
import org.immutables.value.Value;
122-
@Value.Immutable
123-
public interface Two {
124-
String value();
125-
}
126-
""".stripIndent())
115+
""".stripIndent(true)
116+
117+
// language=Java
118+
writeJavaSourceFile('''
119+
package com.palantir.one;
120+
import com.palantir.two.ImmutableTwo;
121+
import org.immutables.value.Value;
122+
@Value.Immutable
123+
public interface One {
124+
ImmutableTwo two();
125+
}
126+
'''.stripIndent(true))
127+
128+
// language=Java
129+
writeJavaSourceFile('''
130+
package com.palantir.two;
131+
import org.immutables.value.Value;
132+
@Value.Immutable
133+
public interface Two {
134+
String value();
135+
}
136+
'''.stripIndent(true))
137+
127138
then:
128139
ExecutionResult result = runTasks('compileJava')
129140
println(result.standardError)
141+
println(result.standardOutput)
130142
result.success
131143

132144
where:
@@ -135,13 +147,6 @@ class BaselineImmutablesTest extends IntegrationSpec {
135147

136148
def 'handles an annotationProcesor source set extending from another one'() {
137149
buildFile << """
138-
apply plugin: 'com.palantir.baseline-immutables'
139-
apply plugin: 'java-library'
140-
141-
repositories {
142-
mavenCentral()
143-
}
144-
145150
dependencies {
146151
annotationProcessor '$IMMUTABLES'
147152
compileOnly '$IMMUTABLES_ANNOTATIONS'
@@ -151,12 +156,6 @@ class BaselineImmutablesTest extends IntegrationSpec {
151156
testAnnotationProcessor.extendsFrom annotationProcessor
152157
testCompileOnly.extendsFrom compileOnly
153158
}
154-
155-
tasks.withType(JavaCompile) { javaCompile ->
156-
doFirst {
157-
logger.lifecycle "\${javaCompile.name}: \${javaCompile.options.allCompilerArgs}"
158-
}
159-
}
160159
""".stripIndent(true)
161160

162161
def testJava = 'src/test/java'

versions.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ com.googlecode.concurrent-trees:concurrent-trees:2.6.1 (1 constraints: 761166da)
2828
com.googlecode.java-diff-utils:diffutils:1.3.0 (1 constraints: 0605f935)
2929
com.googlecode.javaewah:JavaEWAH:1.1.12 (1 constraints: 750eee5e)
3030
com.palantir.gradle.failure-reports:gradle-failure-reports-exceptions:1.2.0 (1 constraints: 0505f635)
31-
com.palantir.gradle.utils:lazily-configured-mapping:0.1.0 (1 constraints: 0305ee35)
31+
com.palantir.gradle.utils:environment-variables:0.9.0 (1 constraints: 0b050636)
32+
com.palantir.gradle.utils:lazily-configured-mapping:0.9.0 (1 constraints: 0b050636)
3233
com.palantir.javaformat:gradle-palantir-java-format:1.1.0 (1 constraints: 0405f335)
3334
com.palantir.javaformat:palantir-java-format-spi:1.1.0 (1 constraints: 711560be)
3435
com.uber.nullaway:nullaway:0.12.1 (1 constraints: 36052a3b)

0 commit comments

Comments
 (0)