From 3d5a6d9abc8c03a867417107c1f18f5a806a9b45 Mon Sep 17 00:00:00 2001 From: Brian Laub Date: Tue, 11 Jul 2023 15:43:50 -0400 Subject: [PATCH] set the jakartaPackages flag via a gradle property If the gradle property `conjure.java.jakartaPackages` is set to `true`, then the corresponding flag will be set within the `java` options container in `ConjureExtension` at the time the extension object is created. This ensures that we can eagerly read the property value and configure the extension early in the gradle lifecycle, before any subprojects are configured, and the value will propagate correctly. This change removes previously-added `afterEvaluate` calls in some places. --- .../palantir/gradle/conjure/api/ConjureExtension.java | 7 ++++++- .../palantir/gradle/conjure/ConjureBasePlugin.java | 3 ++- .../gradle/conjure/ConjureJavaLocalCodegenPlugin.java | 11 +++++------ .../palantir/gradle/conjure/ConjureLocalPlugin.java | 2 +- .../com/palantir/gradle/conjure/ConjurePlugin.java | 4 ++-- .../palantir/gradle/conjure/ConjurePluginTest.groovy | 10 +++------- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/gradle-conjure-api/src/main/java/com/palantir/gradle/conjure/api/ConjureExtension.java b/gradle-conjure-api/src/main/java/com/palantir/gradle/conjure/api/ConjureExtension.java index 5a03dd634..da6a751f5 100644 --- a/gradle-conjure-api/src/main/java/com/palantir/gradle/conjure/api/ConjureExtension.java +++ b/gradle-conjure-api/src/main/java/com/palantir/gradle/conjure/api/ConjureExtension.java @@ -20,6 +20,7 @@ import groovy.lang.DelegatesTo; import java.util.HashMap; import java.util.Map; +import org.gradle.api.Project; public class ConjureExtension { @@ -31,12 +32,16 @@ public class ConjureExtension { private final GeneratorOptions pythonOptions = new GeneratorOptions(); private final Map genericOptions = new HashMap<>(); - public ConjureExtension() { + public ConjureExtension(Project project) { // Projects using sufficiently new gradle-conjure have jetbrains-annotations // added by default. Conjure generators ignore unknown flags and will not be // impacted if generators have not been updated. // See https://github.com/palantir/conjure-java/pull/1884 javaOptions.addFlag("jetbrainsContractAnnotations"); + + if (Boolean.parseBoolean((String) project.findProperty("conjure.java.jakartaPackages"))) { + javaOptions.addFlag("jakartaPackages"); + } } public final void typescript( diff --git a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureBasePlugin.java b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureBasePlugin.java index 79805c922..00bab5c0b 100644 --- a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureBasePlugin.java +++ b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureBasePlugin.java @@ -59,7 +59,8 @@ public void apply(Project project) { ConjureProductDependenciesExtension.EXTENSION_NAME, ConjureProductDependenciesExtension.class, project); - conjureExtension = project.getExtensions().create(ConjureExtension.EXTENSION_NAME, ConjureExtension.class); + conjureExtension = + project.getExtensions().create(ConjureExtension.EXTENSION_NAME, ConjureExtension.class, project); SourceDirectorySet conjureSourceSet = createConjureSourceSet(project); TaskProvider copyConjureSourcesTask = createCopyConjureSourceTask(project, conjureSourceSet); compileIrProvider = diff --git a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureJavaLocalCodegenPlugin.java b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureJavaLocalCodegenPlugin.java index 72c30066f..b53d5a439 100644 --- a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureJavaLocalCodegenPlugin.java +++ b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureJavaLocalCodegenPlugin.java @@ -53,7 +53,7 @@ public void apply(Project project) { project.getPlugins().apply(JavaBasePlugin.class); ConjureExtension extension = - project.getExtensions().create(ConjureExtension.EXTENSION_NAME, ConjureExtension.class); + project.getExtensions().create(ConjureExtension.EXTENSION_NAME, ConjureExtension.class, project); Configuration conjureIrConfiguration = project.getConfigurations().create(CONJURE_CONFIGURATION); TaskProvider extractConjureIr = project.getTasks().register("extractConjureIr", Copy.class, task -> { @@ -79,12 +79,11 @@ private static void setupSubprojects( createGenerateTask(subproject, extension, extractJavaTask, extractConjureIr); }); + project.getChildProjects().forEach((_name, subproject) -> { + configureDependencies(subproject, extension); + }); + project.afterEvaluate(_p -> { - project.getChildProjects().forEach((_name, subproject) -> { - // Configure subproject dependencies in after-evaluate to ensure - // extension the has been evaluated. - configureDependencies(subproject, extension); - }); // Validating that each subproject has a corresponding definition and vice versa. // We do this in afterEvaluate to ensure the configuration is populated. Set apis = conjureIrConfiguration.getAllDependencies().stream() diff --git a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureLocalPlugin.java b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureLocalPlugin.java index bc3424e2b..a45eb4857 100644 --- a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureLocalPlugin.java +++ b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjureLocalPlugin.java @@ -57,7 +57,7 @@ public void apply(Project project) { project.getConfigurations().maybeCreate(ConjurePlugin.CONJURE_GENERATORS_CONFIGURATION_NAME); ConjureExtension extension = - project.getExtensions().create(ConjureExtension.EXTENSION_NAME, ConjureExtension.class); + project.getExtensions().create(ConjureExtension.EXTENSION_NAME, ConjureExtension.class, project); TaskProvider generateConjure = project.getTasks().register("generateConjure", task -> { task.setDescription("Generates code for all requested languages (for which there is a subproject) " diff --git a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjurePlugin.java b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjurePlugin.java index e5b62d2b8..9fbcb90d7 100644 --- a/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjurePlugin.java +++ b/gradle-conjure/src/main/java/com/palantir/gradle/conjure/ConjurePlugin.java @@ -173,7 +173,7 @@ private void setupConjureJavaProjects( + difference); } - project.afterEvaluate(_p -> configs.forEach((suffix, config) -> setupDerivedJavaProject( + configs.forEach((suffix, config) -> setupDerivedJavaProject( suffix, project, optionsSupplier, @@ -181,7 +181,7 @@ private void setupConjureJavaProjects( compileIrTask, productDependencyExt, extractJavaTask, - config))); + config)); } private static Project setupDerivedJavaProject( diff --git a/gradle-conjure/src/test/groovy/com/palantir/gradle/conjure/ConjurePluginTest.groovy b/gradle-conjure/src/test/groovy/com/palantir/gradle/conjure/ConjurePluginTest.groovy index 0dbc08188..3835f9b78 100644 --- a/gradle-conjure/src/test/groovy/com/palantir/gradle/conjure/ConjurePluginTest.groovy +++ b/gradle-conjure/src/test/groovy/com/palantir/gradle/conjure/ConjurePluginTest.groovy @@ -523,13 +523,9 @@ class ConjurePluginTest extends IntegrationSpec { } } """.stripIndent() - file('api/build.gradle') << ''' - conjure { - java { - jakartaPackages = true - } - } - '''.stripIndent() + createFile('gradle.properties') << """ + conjure.java.jakartaPackages=true + """.stripIndent() updateSettings(prefix) when: