diff --git a/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java b/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java index 78fea9adf..023108ac1 100644 --- a/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java +++ b/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java @@ -32,6 +32,9 @@ public abstract class NativeImageFormatProviderPlugin implements Plugin static final String NATIVE_CONFIGURATION_NAME = "palantirJavaFormatNative"; + /** The version of open-java-format-native that formats with {@code openjavaformat.native.formatter}. */ + static final String NATIVE_VERSION_PROPERTY = "openjavaformat.native.formatter.version"; + @Override public void apply(Project rootProject) { checkState( @@ -39,17 +42,23 @@ public void apply(Project rootProject) { "May only apply dev.openjavaformat.java-format-provider to the root project"); Provider platform = NativePlatform.current(getProviderFactory()); - String implementationVersion = JavaFormatExtension.class.getPackage().getImplementationVersion(); + // The plugin's own version, unless the build names another: a release line that publishes no native image + // of its own, as the one for Java 17, formats with a native image of another line. + Provider version = getProviderFactory() + .gradleProperty(NATIVE_VERSION_PROPERTY) + .orElse(String.valueOf(JavaFormatExtension.class.getPackage().getImplementationVersion())); var nativeImage = rootProject.getConfigurations().register(NATIVE_CONFIGURATION_NAME, conf -> { conf.setDescription("Internal configuration for resolving the open-java-format native image"); conf.setCanBeConsumed(false); conf.setCanBeResolved(true); conf.defaultDependencies(deps -> { - deps.addAllLater(platform.map(p -> singletonList(rootProject - .getDependencies() - .create(String.format( - "dev.openjavaformat:open-java-format-native:%s:%s@%s", - implementationVersion, p.classifier(), p.extension()))))); + deps.addAllLater(platform.zip( + version, + (p, v) -> singletonList(rootProject + .getDependencies() + .create(String.format( + "dev.openjavaformat:open-java-format-native:%s:%s@%s", + v, p.classifier(), p.extension()))))); }); }); // Resolved in each project that formats, through its own repositories; formatDiff copies the image it gets diff --git a/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java b/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java index 7c0f05b29..5ffaeb5d1 100644 --- a/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java +++ b/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java @@ -89,21 +89,48 @@ void formatDiff_runs_the_native_image_in_a_parallel_build() throws Exception { assertFormatted(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); } + /** + * A build that declares no image and names its version in openjavaformat.native.formatter.version formats with the + * image of that version, not of the plugin's: the release line for Java 17 publishes none of its own. + */ + @Test + void formatDiff_runs_the_native_image_of_the_version_the_build_names() throws Exception { + var project = projectNamingTheVersion(); + commitThenChange(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); + + var result = project.succeeds("formatDiff", "--info"); + + assertThat(result.getOutput()).contains("Using the native-image formatter"); + assertFormatted(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); + } + /** * The layout of the docs, with the repositories in the subprojects only, and the image declared in the root project * as a release's would be by default. Prints the Gradle user home, where the image is copied to. */ private GradleTestProject project() throws IOException { var platform = currentPlatform(); - writeNativeImage(platform); + return project(""" + dependencies { + palantirJavaFormatNative 'dev.openjavaformat:open-java-format-native:%s:%s@%s' + } + """.formatted(VERSION, platform.classifier(), platform.extension())); + } + + /** The same layout, with no image declared: the plugin asks for the version the build names. */ + private GradleTestProject projectNamingTheVersion() throws IOException { + return project("").gradleProperties(NativeImageFormatProviderPlugin.NATIVE_VERSION_PROPERTY + "=" + VERSION); + } + + private GradleTestProject project(String rootDependencies) throws IOException { + writeNativeImage(currentPlatform()); return new GradleTestProject(projectDir) .configurationCache(configurationCache) .withoutRepositories() .pluginsNotApplied("dev.openjavaformat.java-format") .subprojects("a", "b") .gradleProperties("openjavaformat.native.formatter=true") - .buildGradle( - """ + .buildGradle(""" subprojects { apply plugin: 'java' apply plugin: 'dev.openjavaformat.java-format' @@ -111,15 +138,9 @@ private GradleTestProject project() throws IOException { maven { url = rootProject.file("%s") } } } - dependencies { - palantirJavaFormatNative 'dev.openjavaformat:open-java-format-native:%s:%s@%s' - } + %s println "gradleUserHome=${gradle.gradleUserHomeDir}" - """, - NATIVE_REPO.toAbsolutePath().toString().replace('\\', '/'), - VERSION, - platform.classifier(), - platform.extension()); + """, NATIVE_REPO.toAbsolutePath().toString().replace('\\', '/'), rootDependencies); } private static void assertFormatted(GradleTestProject project, String... files) {