Polish "Add support for Maven extensions"

See gh-1290
This commit is contained in:
Stephane Nicoll 2023-06-12 12:12:27 +02:00
parent 26da5ee6aa
commit 4d574a9e01
4 changed files with 61 additions and 31 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,7 +38,7 @@ public class MavenBuild extends Build {
private final MavenPluginContainer plugins = new MavenPluginContainer(); private final MavenPluginContainer plugins = new MavenPluginContainer();
private final MavenExtensionsContainer extensions = new MavenExtensionsContainer(); private final MavenExtensionContainer extensions = new MavenExtensionContainer();
private final MavenDistributionManagement.Builder distributionManagement = new MavenDistributionManagement.Builder(); private final MavenDistributionManagement.Builder distributionManagement = new MavenDistributionManagement.Builder();
@ -109,11 +109,11 @@ public class MavenBuild extends Build {
} }
/** /**
* Return the {@linkplain MavenExtensionsContainer extensions container} to use to * Return the {@linkplain MavenExtensionContainer extension container} to use to
* configure extensions. * configure extensions.
* @return the {@link MavenExtensionsContainer} * @return the {@link MavenExtensionContainer}
*/ */
public MavenExtensionsContainer extensions() { public MavenExtensionContainer extensions() {
return this.extensions; return this.extensions;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,9 +17,10 @@
package io.spring.initializr.generator.buildsystem.maven; package io.spring.initializr.generator.buildsystem.maven;
/** /**
* An extension entry in a @{@link MavenBuild}. * A build extension in a @{@link MavenBuild}.
* *
* @author Niklas Herder * @author Niklas Herder
* @author Stephane Nicoll
*/ */
public class MavenExtension { public class MavenExtension {
@ -27,7 +28,7 @@ public class MavenExtension {
private final String artifactId; private final String artifactId;
private String version; private final String version;
protected MavenExtension(Builder builder) { protected MavenExtension(Builder builder) {
this.groupId = builder.groupId; this.groupId = builder.groupId;
@ -35,14 +36,26 @@ public class MavenExtension {
this.version = builder.version; this.version = builder.version;
} }
/**
* Return the group ID of the extension.
* @return the group id
*/
public String getGroupId() { public String getGroupId() {
return this.groupId; return this.groupId;
} }
/**
* Return the artifact ID of the extension.
* @return the artifact id
*/
public String getArtifactId() { public String getArtifactId() {
return this.artifactId; return this.artifactId;
} }
/**
* Return the version of the extension.
* @return the artifact id
*/
public String getVersion() { public String getVersion() {
return this.version; return this.version;
} }
@ -60,11 +73,20 @@ public class MavenExtension {
this.artifactId = artifactId; this.artifactId = artifactId;
} }
/**
* Set the version of the extension.
* @param version the version of the extension
* @return this for method chaining
*/
public Builder version(String version) { public Builder version(String version) {
this.version = version; this.version = version;
return this; return this;
} }
/**
* Build a {@link MavenExtension} with the current state of this builder.
* @return a {@link MavenExtension}
*/
public MavenExtension build() { public MavenExtension build() {
return new MavenExtension(this); return new MavenExtension(this);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,7 +21,13 @@ import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;
public class MavenExtensionsContainer { /**
* A container for {@link MavenExtension maven extensions}.
*
* @author Niklas Herder
* @author Stephane Nicoll
*/
public class MavenExtensionContainer {
private final Map<String, MavenExtension.Builder> extensions = new LinkedHashMap<>(); private final Map<String, MavenExtension.Builder> extensions = new LinkedHashMap<>();
@ -54,14 +60,16 @@ public class MavenExtensionsContainer {
} }
/** /**
* Add a {@link MavenExtension} with the specified {@code groupId} and * Add a {@link MavenExtension} with the specified {@code groupId},
* {@code artifactId}. Does nothing if the extension has already been added. * {@code artifactId}, and {@code version}. If the extension has already been added,
* only update the version if necessary.
* @param groupId the groupId of the extension * @param groupId the groupId of the extension
* @param artifactId the artifactId of the extension * @param artifactId the artifactId of the extension
* @param version the version of the extension
* @see #add(String, String, Consumer) * @see #add(String, String, Consumer)
*/ */
public void add(String groupId, String artifactId) { public void add(String groupId, String artifactId, String version) {
addExtension(groupId, artifactId); add(groupId, artifactId, (extension) -> extension.version(version));
} }
/** /**

View File

@ -700,6 +700,23 @@ class MavenBuildWriterTests {
}); });
} }
@Test
void pomWithoutExtensions() {
MavenBuild build = new MavenBuild();
generatePom(build, (pom) -> assertThat(pom).nodeAtPath("/project/build/extensions").isNull());
}
@Test
void pomWithExtension() {
MavenBuild build = new MavenBuild();
build.extensions().add("com.example", "testExtension", "1.6.1");
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/build/extensions/extension/groupId").isEqualTo("com.example");
assertThat(pom).textAtPath("/project/build/extensions/extension/artifactId").isEqualTo("testExtension");
assertThat(pom).textAtPath("/project/build/extensions/extension/version").isEqualTo("1.6.1");
});
}
@Test @Test
void pomWithEmptyBuild() { void pomWithEmptyBuild() {
MavenBuild build = new MavenBuild(); MavenBuild build = new MavenBuild();
@ -903,23 +920,6 @@ class MavenBuildWriterTests {
}); });
} }
@Test
void pomWithoutExtensions() {
MavenBuild build = new MavenBuild();
generatePom(build, (pom) -> assertThat(pom).nodeAtPath("/project/build/extensions").isNull());
}
@Test
void pomWithExtension() {
MavenBuild build = new MavenBuild();
build.extensions().add("com.example", "testExtension", (builder) -> builder.version("1.6.1"));
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/build/extensions/extension/groupId").isEqualTo("com.example");
assertThat(pom).textAtPath("/project/build/extensions/extension/artifactId").isEqualTo("testExtension");
assertThat(pom).textAtPath("/project/build/extensions/extension/version").isEqualTo("1.6.1");
});
}
@Test @Test
void pomWithDistributionManagementRepository() { void pomWithDistributionManagementRepository() {
MavenBuild build = new MavenBuild(); MavenBuild build = new MavenBuild();