Skip to content

Commit

Permalink
Add VersionReference support to MavenPlugin and MavenExtension
Browse files Browse the repository at this point in the history
Closes gh-1243
  • Loading branch information
mhalbritter committed Dec 18, 2024
1 parent 5bec8f6 commit e645e5c
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.spring.build.BuildMetadataResolver;
import io.spring.initializr.generator.version.VersionReference;
import io.spring.initializr.metadata.InitializrMetadata;

/**
Expand Down Expand Up @@ -51,7 +52,8 @@ public void customize(MavenBuild build) {
option.add("option", "all-open:annotation=jakarta.persistence.Embeddable");
});
});
kotlinPlugin.dependency("org.jetbrains.kotlin", "kotlin-maven-noarg", "${kotlin.version}");
kotlinPlugin.dependency("org.jetbrains.kotlin", "kotlin-maven-noarg",
VersionReference.ofProperty(KotlinMavenBuildCustomizer.KOTLIN_VERSION_PROPERTY));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.generator.version.VersionProperty;
import io.spring.initializr.generator.version.VersionRange;
import io.spring.initializr.generator.version.VersionReference;

/**
* {@link BuildCustomizer} for Kotlin projects build with Maven when Kotlin is supported
Expand All @@ -34,6 +36,8 @@
*/
class KotlinMavenBuildCustomizer implements BuildCustomizer<MavenBuild> {

static final VersionProperty KOTLIN_VERSION_PROPERTY = VersionProperty.of("kotlin.version");

private static final VersionRange KOTLIN_ONE_EIGHT_OR_LATER = new VersionRange(Version.parse("1.8.0"));

private final KotlinProjectSettings settings;
Expand All @@ -44,7 +48,7 @@ class KotlinMavenBuildCustomizer implements BuildCustomizer<MavenBuild> {

@Override
public void customize(MavenBuild build) {
build.properties().version("kotlin.version", this.settings.getVersion());
build.properties().version(KOTLIN_VERSION_PROPERTY, this.settings.getVersion());
build.settings()
.sourceDirectory("${project.basedir}/src/main/kotlin")
.testSourceDirectory("${project.basedir}/src/test/kotlin");
Expand All @@ -55,7 +59,8 @@ public void customize(MavenBuild build) {
configuration.configure("compilerPlugins",
(compilerPlugins) -> compilerPlugins.add("plugin", "spring"));
});
kotlinMavenPlugin.dependency("org.jetbrains.kotlin", "kotlin-maven-allopen", "${kotlin.version}");
kotlinMavenPlugin.dependency("org.jetbrains.kotlin", "kotlin-maven-allopen",
VersionReference.ofProperty(KOTLIN_VERSION_PROPERTY));
});
String artifactId = KotlinMavenBuildCustomizer.KOTLIN_ONE_EIGHT_OR_LATER
.match(Version.parse(this.settings.getVersion())) ? "kotlin-stdlib" : "kotlin-stdlib-jdk8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.version.VersionReference;

/**
* {@link BuildCustomizer} for Kotlin projects build with Maven when Kotlin is not
Expand All @@ -35,12 +36,13 @@ class KotlinMavenFullBuildCustomizer implements BuildCustomizer<MavenBuild> {

@Override
public void customize(MavenBuild build) {
build.properties().version("kotlin.version", this.settings.getVersion());
build.properties().version(KotlinMavenBuildCustomizer.KOTLIN_VERSION_PROPERTY, this.settings.getVersion());
build.settings()
.sourceDirectory("${project.basedir}/src/main/kotlin")
.testSourceDirectory("${project.basedir}/src/test/kotlin");
build.plugins().add("org.jetbrains.kotlin", "kotlin-maven-plugin", (kotlinMavenPlugin) -> {
kotlinMavenPlugin.version("${kotlin.version}");
kotlinMavenPlugin
.versionReference(VersionReference.ofProperty(KotlinMavenBuildCustomizer.KOTLIN_VERSION_PROPERTY));
kotlinMavenPlugin.configuration((configuration) -> {
configuration.configure("args",
(args) -> this.settings.getCompilerArgs().forEach((arg) -> args.add("arg", arg)));
Expand All @@ -51,7 +53,8 @@ public void customize(MavenBuild build) {
kotlinMavenPlugin.execution("compile", (compile) -> compile.phase("compile").goal("compile"));
kotlinMavenPlugin.execution("test-compile",
(compile) -> compile.phase("test-compile").goal("test-compile"));
kotlinMavenPlugin.dependency("org.jetbrains.kotlin", "kotlin-maven-allopen", "${kotlin.version}");
kotlinMavenPlugin.dependency("org.jetbrains.kotlin", "kotlin-maven-allopen",
VersionReference.ofProperty(KotlinMavenBuildCustomizer.KOTLIN_VERSION_PROPERTY));
});

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void customizeRegisterSpringBootPlugin() {
assertThat(build.plugins().values()).singleElement().satisfies((mavenPlugin) -> {
assertThat(mavenPlugin.getGroupId()).isEqualTo("org.springframework.boot");
assertThat(mavenPlugin.getArtifactId()).isEqualTo("spring-boot-maven-plugin");
assertThat(mavenPlugin.getVersion()).isNull();
assertThat(mavenPlugin.getVersionReference()).isNull();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Configuration;
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Execution;
import io.spring.initializr.generator.version.VersionReference;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -37,7 +38,7 @@ void groovyMavenPluginIsConfigured() {
assertThat(build.plugins().values()).singleElement().satisfies((groovyPlugin) -> {
assertThat(groovyPlugin.getGroupId()).isEqualTo("org.codehaus.gmavenplus");
assertThat(groovyPlugin.getArtifactId()).isEqualTo("gmavenplus-plugin");
assertThat(groovyPlugin.getVersion()).isEqualTo("1.13.1");
assertThat(groovyPlugin.getVersionReference()).isEqualTo(VersionReference.ofValue("1.13.1"));
Configuration configuration = groovyPlugin.getConfiguration();
assertThat(configuration).isNull();
assertThat(groovyPlugin.getExecutions()).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.spring.initializr.generator.project.MutableProjectDescription;
import io.spring.initializr.generator.test.InitializrMetadataTestBuilder;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.generator.version.VersionReference;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.support.MetadataBuildItemResolver;
Expand Down Expand Up @@ -59,7 +60,7 @@ void customizeWhenJpaFacetPresentShouldAddKotlinJpaPlugin() {
MavenPlugin.Dependency pluginDependency = plugin.getDependencies().get(0);
assertThat(pluginDependency.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(pluginDependency.getArtifactId()).isEqualTo("kotlin-maven-noarg");
assertThat(pluginDependency.getVersion()).isEqualTo("${kotlin.version}");
assertThat(pluginDependency.getVersionReference()).isEqualTo(VersionReference.ofProperty("kotlin.version"));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Dependency;
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Setting;
import io.spring.initializr.generator.version.VersionProperty;
import io.spring.initializr.generator.version.VersionReference;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -61,7 +62,7 @@ void kotlinMavenPluginIsConfigured() {
assertThat(build.plugins().values()).singleElement().satisfies((kotlinPlugin) -> {
assertThat(kotlinPlugin.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(kotlinPlugin.getArtifactId()).isEqualTo("kotlin-maven-plugin");
assertThat(kotlinPlugin.getVersion()).isNull();
assertThat(kotlinPlugin.getVersionReference()).isNull();
Configuration configuration = kotlinPlugin.getConfiguration();
assertThat(configuration).isNotNull();
assertThat(configuration.getSettings()).hasSize(2);
Expand All @@ -84,7 +85,7 @@ void kotlinMavenPluginIsConfigured() {
Dependency allOpen = kotlinPlugin.getDependencies().get(0);
assertThat(allOpen.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(allOpen.getArtifactId()).isEqualTo("kotlin-maven-allopen");
assertThat(allOpen.getVersion()).isEqualTo("${kotlin.version}");
assertThat(allOpen.getVersionReference()).isEqualTo(VersionReference.ofProperty("kotlin.version"));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Execution;
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Setting;
import io.spring.initializr.generator.version.VersionProperty;
import io.spring.initializr.generator.version.VersionReference;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -58,7 +59,7 @@ void kotlinMavenPluginIsConfigured() {
assertThat(build.plugins().values()).singleElement().satisfies((kotlinPlugin) -> {
assertThat(kotlinPlugin.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(kotlinPlugin.getArtifactId()).isEqualTo("kotlin-maven-plugin");
assertThat(kotlinPlugin.getVersion()).isEqualTo("${kotlin.version}");
assertThat(kotlinPlugin.getVersionReference()).isEqualTo(VersionReference.ofProperty("kotlin.version"));
Configuration configuration = kotlinPlugin.getConfiguration();
assertThat(configuration).isNotNull();
assertThat(configuration.getSettings()).hasSize(3);
Expand Down Expand Up @@ -94,7 +95,7 @@ void kotlinMavenPluginIsConfigured() {
Dependency allOpen = kotlinPlugin.getDependencies().get(0);
assertThat(allOpen.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(allOpen.getArtifactId()).isEqualTo("kotlin-maven-allopen");
assertThat(allOpen.getVersion()).isEqualTo("${kotlin.version}");
assertThat(allOpen.getVersionReference()).isEqualTo(VersionReference.ofProperty("kotlin.version"));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private void writePlugin(IndentingWriter writer, MavenPlugin plugin) {
writeElement(writer, "plugin", () -> {
writeSingleElement(writer, "groupId", plugin.getGroupId());
writeSingleElement(writer, "artifactId", plugin.getArtifactId());
writeSingleElement(writer, "version", plugin.getVersion());
writeSingleElement(writer, "version", determineVersion(plugin.getVersionReference()));
if (!plugin.isInherited()) {
writeSingleElement(writer, "inherited", "false");
}
Expand Down Expand Up @@ -468,7 +468,7 @@ private void writePluginDependency(IndentingWriter writer, MavenPlugin.Dependenc
writeElement(writer, "dependency", () -> {
writeSingleElement(writer, "groupId", dependency.getGroupId());
writeSingleElement(writer, "artifactId", dependency.getArtifactId());
writeSingleElement(writer, "version", dependency.getVersion());
writeSingleElement(writer, "version", determineVersion(dependency.getVersionReference()));
});
}

Expand Down Expand Up @@ -556,7 +556,7 @@ private void writeExtension(IndentingWriter writer, MavenExtension extension) {
writeElement(writer, "extension", () -> {
writeSingleElement(writer, "groupId", extension.getGroupId());
writeSingleElement(writer, "artifactId", extension.getArtifactId());
writeSingleElement(writer, "version", extension.getVersion());
writeSingleElement(writer, "version", determineVersion(extension.getVersionReference()));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.spring.initializr.generator.buildsystem.maven;

import io.spring.initializr.generator.version.VersionReference;

/**
* A build extension in a @{@link MavenBuild}.
*
Expand All @@ -28,7 +30,7 @@ public class MavenExtension {

private final String artifactId;

private final String version;
private final VersionReference version;

protected MavenExtension(Builder builder) {
this.groupId = builder.groupId;
Expand All @@ -54,9 +56,19 @@ public String getArtifactId() {

/**
* Return the version of the extension.
* @return the artifact id
* @return the version
* @deprecated for removal in favor of {@link #getVersionReference()}
*/
@Deprecated(forRemoval = true)
public String getVersion() {
return (this.version != null) ? this.version.getValue() : null;
}

/**
* Return the version of the extension.
* @return the version
*/
public VersionReference getVersionReference() {
return this.version;
}

Expand All @@ -66,7 +78,7 @@ public static class Builder {

private final String artifactId;

private String version;
private VersionReference version;

protected Builder(String groupId, String artifactId) {
this.groupId = groupId;
Expand All @@ -79,6 +91,18 @@ protected Builder(String groupId, String artifactId) {
* @return this for method chaining
*/
public Builder version(String version) {
if (version == null) {
return versionReference(null);
}
return versionReference(VersionReference.ofValue(version));
}

/**
* Set the version of the extension.
* @param version the version of the extension
* @return this for method chaining
*/
public Builder versionReference(VersionReference version) {
this.version = version;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Map;
import java.util.function.Consumer;

import io.spring.initializr.generator.version.VersionReference;

/**
* A plugin in a {@link MavenBuild}.
*
Expand All @@ -36,7 +38,7 @@ public class MavenPlugin {

private final String artifactId;

private final String version;
private final VersionReference version;

private final boolean extensions;

Expand Down Expand Up @@ -77,10 +79,21 @@ public String getArtifactId() {

/**
* Return the version of the plugin or {@code null} if the version of the plugin is
* managed.
* managed. Also returns {@code null} if the version references a property.
* @return the version or {@code null}
* @deprecated for removal in favor of {@link #getVersionReference()}
*/
@Deprecated(forRemoval = true)
public String getVersion() {
return (this.version != null) ? this.version.getValue() : null;
}

/**
* Return the version of the plugin or {@code null} if the version of the plugin is
* managed.
* @return the version or {@code null}
*/
public VersionReference getVersionReference() {
return this.version;
}

Expand Down Expand Up @@ -133,7 +146,7 @@ public static class Builder {

private final String artifactId;

private String version;
private VersionReference version;

private boolean extensions;

Expand All @@ -157,6 +170,19 @@ protected Builder(String groupId, String artifactId) {
* @return this for method chaining
*/
public Builder version(String version) {
if (version == null) {
return versionReference(null);
}
return versionReference(VersionReference.ofValue(version));
}

/**
* Set the version of the plugin or {@code null} if the version is managed by the
* project.
* @param version the version of the plugin or {@code null}
* @return this for method chaining
*/
public Builder versionReference(VersionReference version) {
this.version = version;
return this;
}
Expand Down Expand Up @@ -215,6 +241,17 @@ public Builder execution(String id, Consumer<ExecutionBuilder> execution) {
* @return this for method chaining
*/
public Builder dependency(String groupId, String artifactId, String version) {
return dependency(groupId, artifactId, (version != null) ? VersionReference.ofValue(version) : null);
}

/**
* Add a plugin dependency.
* @param groupId the group ID of the dependency
* @param artifactId the artifact ID of the dependency
* @param version the version of the dependency
* @return this for method chaining
*/
public Builder dependency(String groupId, String artifactId, VersionReference version) {
this.dependencies.add(new Dependency(groupId, artifactId, version));
return this;
}
Expand Down Expand Up @@ -487,9 +524,9 @@ public static final class Dependency {

private final String artifactId;

private final String version;
private final VersionReference version;

private Dependency(String groupId, String artifactId, String version) {
private Dependency(String groupId, String artifactId, VersionReference version) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
Expand All @@ -514,8 +551,18 @@ public String getArtifactId() {
/**
* Return the version of the plugin dependency.
* @return the version
* @deprecated for removal in favor of {@link #getVersionReference()}.
*/
@Deprecated(forRemoval = true)
public String getVersion() {
return (this.version != null) ? this.version.getValue() : null;
}

/**
* Return the version of the plugin dependency.
* @return the version
*/
public VersionReference getVersionReference() {
return this.version;
}

Expand Down
Loading

0 comments on commit e645e5c

Please sign in to comment.