diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BomContainer.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BomContainer.java index 452ccb60..e43ee95a 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BomContainer.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BomContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,12 @@ import java.util.function.Function; */ public class BomContainer extends BuildItemContainer { - BomContainer(Function itemResolver) { + /** + * Create an instance with the specified {@code itemResolver}. + * @param itemResolver the function that returns a {@link BillOfMaterials} based on an + * identifier. + */ + public BomContainer(Function itemResolver) { super(new LinkedHashMap<>(), itemResolver); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/Build.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/Build.java index b5b95a57..789398cc 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/Build.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/Build.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ public abstract class Build { this.pluginRepositories = new MavenRepositoryContainer(resolver::resolveRepository); } - private static BuildItemResolver determineBuildItemResolver(BuildItemResolver buildItemResolver) { + protected static BuildItemResolver determineBuildItemResolver(BuildItemResolver buildItemResolver) { if (buildItemResolver != null) { return buildItemResolver; } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BuildItemResolver.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BuildItemResolver.java index 23b8a391..d631171a 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BuildItemResolver.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/BuildItemResolver.java @@ -23,6 +23,11 @@ package io.spring.initializr.generator.buildsystem; */ public interface BuildItemResolver { + /** + * A default {@link BuildItemResolver} that bypass resolution. + */ + SimpleBuildItemResolver NO_OP = new SimpleBuildItemResolver((id) -> null, (id) -> null, (id) -> null); + /** * Resolve the {@link Dependency} with the specified {@code id}. * @param id the id of the dependency diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/DependencyContainer.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/DependencyContainer.java index 2636ee19..a897b6c8 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/DependencyContainer.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/DependencyContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,12 @@ import java.util.function.Function; */ public class DependencyContainer extends BuildItemContainer { - DependencyContainer(Function itemResolver) { + /** + * Create an instance with the specified {@code itemResolver}. + * @param itemResolver the function that returns a {@link Dependency} based on an + * identifier. + */ + public DependencyContainer(Function itemResolver) { super(new LinkedHashMap<>(), itemResolver); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryContainer.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryContainer.java index cf544916..5c82ca7e 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryContainer.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,12 @@ import java.util.function.Function; */ public class MavenRepositoryContainer extends BuildItemContainer { - MavenRepositoryContainer(Function itemResolver) { + /** + * Create an instance with the specified {@code itemResolver}. + * @param itemResolver the function that returns a {@link MavenRepository} based on an + * identifier. + */ + public MavenRepositoryContainer(Function itemResolver) { super(new LinkedHashMap<>(), itemResolver); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java index d98b6f0f..643fe28f 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,8 +39,11 @@ public class MavenBuild extends Build { private final MavenDistributionManagement.Builder distributionManagement = new MavenDistributionManagement.Builder(); + private final MavenProfileContainer profiles; + public MavenBuild(BuildItemResolver buildItemResolver) { super(buildItemResolver); + this.profiles = new MavenProfileContainer(determineBuildItemResolver(buildItemResolver)); } public MavenBuild() { @@ -102,4 +105,13 @@ public class MavenBuild extends Build { return this.plugins; } + /** + * Return the {@linkplain MavenProfileContainer profile container} to use to configure + * profiles. + * @return the {@link MavenProfileContainer} + */ + public MavenProfileContainer profiles() { + return this.profiles; + } + } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java index 24d77003..3967d51e 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java @@ -23,17 +23,20 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.function.BiConsumer; +import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; import io.spring.initializr.generator.buildsystem.BillOfMaterials; +import io.spring.initializr.generator.buildsystem.BomContainer; import io.spring.initializr.generator.buildsystem.Dependency; import io.spring.initializr.generator.buildsystem.Dependency.Exclusion; import io.spring.initializr.generator.buildsystem.DependencyComparator; import io.spring.initializr.generator.buildsystem.DependencyContainer; import io.spring.initializr.generator.buildsystem.DependencyScope; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryContainer; import io.spring.initializr.generator.buildsystem.PropertyContainer; import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.DeploymentRepository; import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.Relocation; @@ -75,11 +78,12 @@ public class MavenBuildWriter { writeCollectionElement(writer, "developers", settings.getDevelopers(), this::writeDeveloper); writeScm(writer, settings.getScm()); writeProperties(writer, build.properties()); - writeDependencies(writer, build); - writeDependencyManagement(writer, build); + writeDependencies(writer, build.dependencies()); + writeDependencyManagement(writer, build.boms()); writeBuild(writer, build); - writeRepositories(writer, build); - writeDistributionManagement(writer, build); + writeRepositories(writer, build.repositories(), build.pluginRepositories()); + writeDistributionManagement(writer, build.getDistributionManagement()); + writeProfiles(writer, build); }); } @@ -194,11 +198,10 @@ public class MavenBuildWriter { } } - private void writeDependencies(IndentingWriter writer, MavenBuild build) { - if (build.dependencies().isEmpty()) { + private void writeDependencies(IndentingWriter writer, DependencyContainer dependencies) { + if (dependencies.isEmpty()) { return; } - DependencyContainer dependencies = build.dependencies(); writer.println(); writeElement(writer, "dependencies", () -> { Collection compiledDependencies = writeDependencies(writer, dependencies, @@ -281,15 +284,15 @@ public class MavenBuildWriter { || dependency.getScope() == DependencyScope.COMPILE_ONLY); } - private void writeDependencyManagement(IndentingWriter writer, MavenBuild build) { - if (build.boms().isEmpty()) { + private void writeDependencyManagement(IndentingWriter writer, BomContainer boms) { + if (boms.isEmpty()) { return; } - List boms = build.boms().items().sorted(Comparator.comparing(BillOfMaterials::getOrder)) - .collect(Collectors.toList()); writer.println(); writeElement(writer, "dependencyManagement", - () -> writeCollectionElement(writer, "dependencies", boms, this::writeBom)); + () -> writeCollectionElement(writer, "dependencies", boms.items() + .sorted(Comparator.comparing(BillOfMaterials::getOrder)).collect(Collectors.toList()), + this::writeBom)); } private void writeBom(IndentingWriter writer, BillOfMaterials bom) { @@ -323,15 +326,15 @@ public class MavenBuildWriter { writeSingleElement(writer, "finalName", settings.getFinalName()); writeSingleElement(writer, "sourceDirectory", settings.getSourceDirectory()); writeSingleElement(writer, "testSourceDirectory", settings.getTestSourceDirectory()); - writeResources(writer, build); + writeResources(writer, build.resources(), build.testResources()); writeCollectionElement(writer, "plugins", build.plugins().values(), this::writePlugin); - }); } - private void writeResources(IndentingWriter writer, MavenBuild build) { - writeCollectionElement(writer, "resources", build.resources().values(), this::writeResource); - writeCollectionElement(writer, "testResources", build.testResources().values(), this::writeTestResource); + private void writeResources(IndentingWriter writer, MavenResourceContainer resources, + MavenResourceContainer testResources) { + writeCollectionElement(writer, "resources", resources.values(), this::writeResource); + writeCollectionElement(writer, "testResources", testResources.values(), this::writeTestResource); } private void writeResource(IndentingWriter writer, MavenResource resource) { @@ -386,7 +389,7 @@ public class MavenBuildWriter { @SuppressWarnings("unchecked") private void writeSetting(IndentingWriter writer, Setting setting) { if (setting.getValue() instanceof String) { - writeSingleElement(writer, setting.getName(), (String) setting.getValue()); + writeSingleElement(writer, setting.getName(), setting.getValue()); } else if (setting.getValue() instanceof List) { writeCollectionElement(writer, setting.getName(), (List) setting.getValue(), this::writeSetting); @@ -413,9 +416,10 @@ public class MavenBuildWriter { }); } - private void writeRepositories(IndentingWriter writer, MavenBuild build) { - List repositories = filterRepositories(build.repositories().items()); - List pluginRepositories = filterRepositories(build.pluginRepositories().items()); + private void writeRepositories(IndentingWriter writer, MavenRepositoryContainer buildRepositories, + MavenRepositoryContainer buildPluginRepositories) { + List repositories = filterRepositories(buildRepositories.items()); + List pluginRepositories = filterRepositories(buildPluginRepositories.items()); if (repositories.isEmpty() && pluginRepositories.isEmpty()) { return; } @@ -448,8 +452,8 @@ public class MavenBuildWriter { }); } - private void writeDistributionManagement(IndentingWriter writer, MavenBuild build) { - MavenDistributionManagement distributionManagement = build.getDistributionManagement(); + private void writeDistributionManagement(IndentingWriter writer, + MavenDistributionManagement distributionManagement) { if (distributionManagement.isEmpty()) { return; } @@ -491,8 +495,70 @@ public class MavenBuildWriter { } } - private void writeSingleElement(IndentingWriter writer, String name, String text) { - if (text != null) { + private void writeProfiles(IndentingWriter writer, MavenBuild build) { + MavenProfileContainer profiles = build.profiles(); + if (profiles.isEmpty()) { + return; + } + writer.println(); + writeElement(writer, "profiles", () -> profiles.values().forEach((profile) -> writeProfile(writer, profile))); + } + + private void writeProfile(IndentingWriter writer, MavenProfile profile) { + writeElement(writer, "profile", () -> { + writeSingleElement(writer, "id", profile.getId()); + writeProfileActivation(writer, profile.getActivation()); + writeProperties(writer, profile.properties()); + writeDependencies(writer, profile.dependencies()); + writeDependencyManagement(writer, profile.boms()); + writeProfileBuild(writer, profile); + writeRepositories(writer, profile.repositories(), profile.pluginRepositories()); + writeDistributionManagement(writer, profile.getDistributionManagement()); + }); + } + + private void writeProfileActivation(IndentingWriter writer, MavenProfileActivation activation) { + if (activation.isEmpty()) { + return; + } + writeElement(writer, "activation", () -> { + writeSingleElement(writer, "activeByDefault", activation.getActiveByDefault()); + writeSingleElement(writer, "jdk", activation.getJdk()); + ifNotNull(activation.getOs(), (os) -> writeElement(writer, "os", () -> { + writeSingleElement(writer, "name", os.getName()); + writeSingleElement(writer, "arch", os.getArch()); + writeSingleElement(writer, "family", os.getFamily()); + writeSingleElement(writer, "version", os.getVersion()); + })); + ifNotNull(activation.getProperty(), (property) -> writeElement(writer, "property", () -> { + writeSingleElement(writer, "name", property.getName()); + writeSingleElement(writer, "value", property.getValue()); + })); + ifNotNull(activation.getFile(), (file) -> writeElement(writer, "file", () -> { + writeSingleElement(writer, "exists", file.getExists()); + writeSingleElement(writer, "missing", file.getMissing()); + })); + }); + } + + private void writeProfileBuild(IndentingWriter writer, MavenProfile profile) { + MavenProfile.Settings settings = profile.getSettings(); + if (settings.getDefaultGoal() == null && settings.getFinalName() == null && profile.resources().isEmpty() + && profile.testResources().isEmpty() && profile.plugins().isEmpty()) { + return; + } + writer.println(); + writeElement(writer, "build", () -> { + writeSingleElement(writer, "defaultGoal", settings.getDefaultGoal()); + writeSingleElement(writer, "finalName", settings.getFinalName()); + writeResources(writer, profile.resources(), profile.testResources()); + writeCollectionElement(writer, "plugins", profile.plugins().values(), this::writePlugin); + }); + } + + private void writeSingleElement(IndentingWriter writer, String name, Object value) { + if (value != null) { + CharSequence text = (value instanceof CharSequence) ? (CharSequence) value : value.toString(); writer.print(String.format("<%s>", name)); writer.print(encodeText(text)); writer.println(String.format("", name)); @@ -524,7 +590,13 @@ public class MavenBuildWriter { } } - private String encodeText(String text) { + private void ifNotNull(T value, Consumer elementWriter) { + if (value != null) { + elementWriter.accept(value); + } + } + + private String encodeText(CharSequence text) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char character = text.charAt(i); diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfile.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfile.java new file mode 100644 index 00000000..d3c54f41 --- /dev/null +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfile.java @@ -0,0 +1,270 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +import io.spring.initializr.generator.buildsystem.BomContainer; +import io.spring.initializr.generator.buildsystem.BuildItemResolver; +import io.spring.initializr.generator.buildsystem.DependencyContainer; +import io.spring.initializr.generator.buildsystem.MavenRepositoryContainer; +import io.spring.initializr.generator.buildsystem.PropertyContainer; + +/** + * A profile in a {@link MavenBuild}. + * + * @author Daniel Andres Pelaez Lopez + * @author Stephane Nicoll + */ +public class MavenProfile { + + private final String id; + + private final MavenProfileActivation.Builder activation = new MavenProfileActivation.Builder(); + + private final SettingsBuilder settings = new SettingsBuilder(); + + private final PropertyContainer properties = new PropertyContainer(); + + private final DependencyContainer dependencies; + + private final MavenResourceContainer resources = new MavenResourceContainer(); + + private final MavenResourceContainer testResources = new MavenResourceContainer(); + + private final MavenPluginContainer plugins = new MavenPluginContainer(); + + private final BomContainer boms; + + private final MavenRepositoryContainer repositories; + + private final MavenRepositoryContainer pluginRepositories; + + private final MavenDistributionManagement.Builder distributionManagement = new MavenDistributionManagement.Builder(); + + protected MavenProfile(String id, BuildItemResolver buildItemResolver) { + this.id = id; + this.dependencies = new DependencyContainer(buildItemResolver::resolveDependency); + this.boms = new BomContainer(buildItemResolver::resolveBom); + this.repositories = new MavenRepositoryContainer(buildItemResolver::resolveRepository); + this.pluginRepositories = new MavenRepositoryContainer(buildItemResolver::resolveRepository); + } + + /** + * Return the identifier of the profile. + * @return the profile id + */ + public String getId() { + return this.id; + } + + /** + * Return a builder to configure how this profile should be + * {@link MavenProfileActivation activated}. + * @return a builder for {@link MavenProfileActivation}. + */ + public MavenProfileActivation.Builder activation() { + return this.activation; + } + + /** + * Return the {@link MavenProfileActivation} of this profile. + * @return the {@link MavenProfileActivation} + */ + public MavenProfileActivation getActivation() { + return this.activation.build(); + } + + /** + * Return a builder to configure the general settings of this profile. + * @return a builder for {@link SettingsBuilder}. + */ + public SettingsBuilder settings() { + return this.settings; + } + + /** + * Return the settings of this profile. + * @return a {@link Settings} + */ + public Settings getSettings() { + return this.settings.build(); + } + + /** + * Return the {@linkplain PropertyContainer property container} to use to configure + * properties. + * @return the {@link PropertyContainer} + */ + public PropertyContainer properties() { + return this.properties; + } + + /** + * Return the {@linkplain DependencyContainer dependency container} to use to + * configure dependencies. + * @return the {@link DependencyContainer} + */ + public DependencyContainer dependencies() { + return this.dependencies; + } + + /** + * Return the {@linkplain BomContainer bom container} to use to configure Bill of + * Materials. + * @return the {@link BomContainer} + */ + public BomContainer boms() { + return this.boms; + } + + /** + * Return the {@linkplain MavenRepositoryContainer repository container} to use to + * configure repositories. + * @return the {@link MavenRepositoryContainer} for repositories + */ + public MavenRepositoryContainer repositories() { + return this.repositories; + } + + /** + * Return the {@linkplain MavenRepositoryContainer repository container} to use to + * configure plugin repositories. + * @return the {@link MavenRepositoryContainer} for plugin repositories + */ + public MavenRepositoryContainer pluginRepositories() { + return this.pluginRepositories; + } + + /** + * Return a builder to configure the {@linkplain MavenDistributionManagement + * distribution management} of this profile. + * @return a builder for {@link MavenDistributionManagement} + */ + public MavenDistributionManagement.Builder distributionManagement() { + return this.distributionManagement; + } + + /** + * Return the {@linkplain MavenDistributionManagement distribution management} of this + * profile. + * @return the {@link MavenDistributionManagement} + */ + public MavenDistributionManagement getDistributionManagement() { + return this.distributionManagement.build(); + } + + /** + * Return the {@linkplain MavenResource resource container} to use to configure main + * resources. + * @return the {@link MavenRepositoryContainer} for main resources + */ + public MavenResourceContainer resources() { + return this.resources; + } + + /** + * Return the {@linkplain MavenResource resource container} to use to configure test + * resources. + * @return the {@link MavenRepositoryContainer} for test resources + */ + public MavenResourceContainer testResources() { + return this.testResources; + } + + /** + * Return the {@linkplain MavenPluginContainer plugin container} to use to configure + * plugins. + * @return the {@link MavenPluginContainer} + */ + public MavenPluginContainer plugins() { + return this.plugins; + } + + /** + * Builder for {@link Settings}. + */ + public static class SettingsBuilder { + + private String defaultGoal; + + private String finalName; + + protected SettingsBuilder() { + } + + /** + * Set the default goal or phase to execute if none is given when this profile is + * active. + * @param defaultGoal the default goal or {@code null} to use the value in the + * build + * @return this for method chaining + */ + public SettingsBuilder defaultGoal(String defaultGoal) { + this.defaultGoal = defaultGoal; + return this; + } + + /** + * Set the name of the bundled project when it is finally built when this profile + * is active. + * @param finalName the final name of the artifact or {@code null} to use the + * value in the build. + * @return this for method chaining + */ + public SettingsBuilder finalName(String finalName) { + this.finalName = finalName; + return this; + } + + public Settings build() { + return new Settings(this); + } + + } + + /** + * Maven profile settings. + */ + public static final class Settings { + + private final String defaultGoal; + + private final String finalName; + + protected Settings(SettingsBuilder builder) { + this.defaultGoal = builder.defaultGoal; + this.finalName = builder.finalName; + } + + /** + * Return the default goal or phase to execute if none is given. + * @return the default goal or {@code null} to use the default + */ + public String getDefaultGoal() { + return this.defaultGoal; + } + + /** + * Return the final name of the artifact. + * @return the final name or {@code null} to use the default + */ + public String getFinalName() { + return this.finalName; + } + + } + +} diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileActivation.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileActivation.java new file mode 100644 index 00000000..ba5fe344 --- /dev/null +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileActivation.java @@ -0,0 +1,324 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +/** + * A {@link MavenProfile profile} activation in a {@link MavenBuild}. + * + * @author Stephane Nicoll + */ +public class MavenProfileActivation { + + private final Boolean activeByDefault; + + private final String jdk; + + private final Os os; + + private final Property property; + + private final File file; + + protected MavenProfileActivation(Builder builder) { + this.activeByDefault = builder.activeByDefault; + this.jdk = builder.jdk; + this.os = builder.os; + this.property = builder.property; + this.file = (builder.fileExists != null || builder.fileMissing != null) + ? new File(builder.fileExists, builder.fileMissing) : null; + } + + /** + * Specify if this activation has any non-default value. + * @return {@code true} if there are no non-default values + */ + public boolean isEmpty() { + return (this.activeByDefault == null && this.jdk == null && this.os == null && this.property == null + && this.file == null); + } + + /** + * Specify if the profile should be activated by default, or {@code null} to use the + * default value. + * @return {@code true} to active the profile if no other profile is active + */ + public Boolean getActiveByDefault() { + return this.activeByDefault; + } + + /** + * Specify the JDK(s) that should match for the profile to be activated, or + * {@code null} to not enable the profile based on the JDK. + * @return the jdk (or jdks range) that should match or {@code null} + */ + public String getJdk() { + return this.jdk; + } + + /** + * Return the operating system activation settings, or {@code null} to not enable the + * profile based on the OS. + * @return the operating system activation settings or {@code null} + */ + public Os getOs() { + return this.os; + } + + /** + * Return the property to match to enable the profile, or {@code null} to not enable + * the profile based on a property. + * @return the property to match or {@code null} + */ + public Property getProperty() { + return this.property; + } + + /** + * Return the file activation settings, or {@code null} to not enable the profile + * based on the presence or absence of a file. + * @return the file activation settings or {@code null} + */ + public File getFile() { + return this.file; + } + + /** + * Operating System activation settings. + */ + public static final class Os { + + private final String name; + + private final String family; + + private final String arch; + + private final String version; + + Os(String name, String family, String arch, String version) { + this.name = name; + this.family = family; + this.arch = arch; + this.version = version; + } + + /** + * Return the name of the OS to match or {@code null}. + * @return the name of the OS + */ + public String getName() { + return this.name; + } + + /** + * Return the family of OS to match or {@code null}. Can be for instance + * {@code mac}, {@code windows}, {@code unix}, {@code os/400}, etc. + * @return the family of OS + */ + public String getFamily() { + return this.family; + } + + /** + * Return the cpu architecture of the OS to match or {@code null}. + * @return the cpu architecture of the OS + */ + public String getArch() { + return this.arch; + } + + /** + * Return the version of the OS to match or {@code null}. + * @return the version of the OS + */ + public String getVersion() { + return this.version; + } + + } + + /** + * Property activation settings. + */ + public static final class Property { + + private final String name; + + private final String value; + + Property(String name, String value) { + this.name = name; + this.value = value; + } + + /** + * Return the name of the property. + * @return the property name + */ + public String getName() { + return this.name; + } + + /** + * Return the value of the property. + * @return the property value + */ + public String getValue() { + return this.value; + } + + } + + /** + * File activation settings. + */ + public static final class File { + + private final String exists; + + private final String missing; + + File(String exists, String missing) { + this.missing = missing; + this.exists = exists; + } + + /** + * Return the file that should exists for the profile to match or {@code null}. + * @return the file that should exist + */ + public String getExists() { + return this.exists; + } + + /** + * Return the file that should be missing for the profile to match or + * {@code null}. + * @return the file that should be missing + */ + public String getMissing() { + return this.missing; + } + + } + + /** + * Builder for {@link MavenProfileActivation}. + */ + public static class Builder { + + private Boolean activeByDefault; + + private String jdk; + + private Os os; + + private Property property; + + private String fileExists; + + private String fileMissing; + + protected Builder() { + } + + /** + * Specify if the profile should be enabled if no profile is active. + * @param activeByDefault whether to enable the profile is no profile is active + * @return this for method chaining + */ + public Builder activeByDefault(Boolean activeByDefault) { + this.activeByDefault = activeByDefault; + return this; + } + + /** + * Specify the JDK(s) to match to enable the profile. Can be a JDK value or an + * OSGi range. + * @param jdk the jdk (or JDKs range) to match + * @return this for method chaining + */ + public Builder jdk(String jdk) { + this.jdk = jdk; + return this; + } + + /** + * Specify the OS to match to enable the profile. + * @param name the name of the OS + * @param family the family os OS + * @param arch the cpu architecture + * @param version the version of the OS + * @return this for method chaining + */ + public Builder os(String name, String family, String arch, String version) { + if (name == null && family == null && arch == null && version == null) { + this.os = null; + } + else { + this.os = new Os(name, family, arch, version); + } + return this; + } + + /** + * Specify the property to match to enable the profile. + * @param name the name of the property + * @param value the value of the property + * @return this for method chaining + */ + public Builder property(String name, String value) { + if (name == null) { + this.property = null; + } + else { + this.property = new Property(name, value); + } + return this; + } + + /** + * Specify the file that should exist to enable the profile. + * @param existingFile the file that should exist + * @return this for method chaining + */ + public Builder fileExists(String existingFile) { + this.fileExists = existingFile; + return this; + } + + /** + * Specify the file that should be missing to enable the profile. + * @param missingFile the file that should be missing + * @return this for method chaining + */ + public Builder fileMissing(String missingFile) { + this.fileMissing = missingFile; + return this; + } + + /** + * Create a {@link MavenProfileActivation} with the current state of this builder. + * @return a {@link MavenProfileActivation}. + */ + public MavenProfileActivation build() { + return new MavenProfileActivation(this); + } + + } + +} diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileContainer.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileContainer.java new file mode 100644 index 00000000..7b86edbd --- /dev/null +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileContainer.java @@ -0,0 +1,97 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.stream.Stream; + +import io.spring.initializr.generator.buildsystem.BuildItemResolver; + +/** + * A container for {@link MavenProfile maven profiles}. + * + * @author Stephane Nicoll + * @author Daniel Andres Pelaez Lopez + */ +public class MavenProfileContainer { + + private final Map profiles = new LinkedHashMap<>(); + + private final BuildItemResolver buildItemResolver; + + /** + * Create an instance with the {@link BuildItemResolver} to use. + * @param buildItemResolver the build item resolver to use + */ + public MavenProfileContainer(BuildItemResolver buildItemResolver) { + this.buildItemResolver = buildItemResolver; + } + + /** + * Specify if this container is empty. + * @return {@code true} if no {@link MavenProfile} is added + */ + public boolean isEmpty() { + return this.profiles.isEmpty(); + } + + /** + * Specify if this container has a profile with the specified {@code id}. + * @param id the id of the profile + * @return {@code true} if a profile with the specified {@code id} exists + */ + public boolean has(String id) { + return this.profiles.containsKey(id); + } + + /** + * Return a {@link Stream} of registered profile identifiers. + * @return a stream of profile ids + */ + public Stream ids() { + return this.profiles.keySet().stream(); + } + + /** + * Returns a {@link Stream} of registered {@link MavenProfile}s. + * @return a stream of {@link MavenProfile}s + */ + public Stream values() { + return this.profiles.values().stream(); + } + + /** + * Return the profile with the specified {@code id}. If no such profile exists a new + * profile is created. + * @param id the id of the profile + * @return the {@link MavenProfile} for that id + */ + public MavenProfile id(String id) { + return this.profiles.computeIfAbsent(id, (key) -> new MavenProfile(id, this.buildItemResolver)); + } + + /** + * Remove the profile with the specified {@code id}. + * @param id the id of the profile + * @return {@code true} if such a profile was registered, {@code false} otherwise + */ + public boolean remove(String id) { + return this.profiles.remove(id) != null; + } + +} diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java index 97ebcb7f..20a8eee6 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java @@ -139,4 +139,46 @@ class MavenBuildTests { .satisfies((testPlugin) -> assertThat(testPlugin.isExtensions()).isTrue()); } + @Test + void mavenProfileCanBeConfigured() { + MavenBuild build = new MavenBuild(); + build.profiles().id("test").activation().jdk("15"); + build.profiles().id("test").properties().property("test", "value"); + assertThat(build.profiles().values()).singleElement().satisfies((profile) -> { + assertThat(profile.getActivation().getActiveByDefault()).isNull(); + assertThat(profile.getActivation().getJdk()).isEqualTo("15"); + assertThat(profile.getActivation().getOs()).isNull(); + assertThat(profile.getActivation().getProperty()).isNull(); + assertThat(profile.getActivation().getFile()).isNull(); + assertThat(profile.properties().values()).singleElement().satisfies((property) -> { + assertThat(property.getKey()).isEqualTo("test"); + assertThat(property.getValue()).isEqualTo("value"); + }); + }); + } + + @Test + void mavenProfileActivationCanBeAmended() { + MavenBuild build = new MavenBuild(); + build.profiles().id("test").activation().jdk("15"); + build.profiles().id("test").activation().jdk(null).activeByDefault(true); + assertThat(build.profiles().values()).singleElement().satisfies((profile) -> { + assertThat(profile.getActivation().getActiveByDefault()).isTrue(); + assertThat(profile.getActivation().getJdk()).isNull(); + assertThat(profile.getActivation().getOs()).isNull(); + assertThat(profile.getActivation().getProperty()).isNull(); + assertThat(profile.getActivation().getFile()).isNull(); + }); + } + + @Test + void mavenProfileCanBeRemoved() { + MavenBuild build = new MavenBuild(); + build.profiles().id("test").activation().jdk("15"); + assertThat(build.profiles().ids()).containsOnly("test"); + build.profiles().remove("test"); + assertThat(build.profiles().ids()).isEmpty(); + assertThat(build.profiles().values()).isEmpty(); + } + } diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java index 118cd938..3ffb6c3e 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java @@ -861,6 +861,184 @@ class MavenBuildWriterTests { "A "demo" project for 'developers' & 'testers'"); } + @Test + void pomWithNoProfile() { + MavenBuild build = new MavenBuild(); + build.settings().coordinates("com.example.demo", "demo"); + generatePom(build, (pom) -> assertThat(pom).textAtPath("/project/profiles/").isNullOrEmpty()); + } + + @Test + void powWithEmptyProfile() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).nodeAtPath("activation").isNull(); + assertThat(profile).nodeAtPath("build").isNull(); + assertThat(profile).nodeAtPath("repositories").isNull(); + assertThat(profile).nodeAtPath("pluginRepositories").isNull(); + assertThat(profile).nodeAtPath("dependencies").isNull(); + assertThat(profile).nodeAtPath("dependencyManagement").isNull(); + assertThat(profile).nodeAtPath("distributionManagement").isNull(); + }); + } + + @Test + void powWithProfileActivationActiveByDefaultAndJdk() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").activation().activeByDefault(true).jdk("11"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).textAtPath("activation/activeByDefault").isEqualTo("true"); + assertThat(profile).textAtPath("activation/jdk").isEqualTo("11"); + assertThat(profile).nodeAtPath("activation/os").isNull(); + assertThat(profile).nodeAtPath("activation/property").isNull(); + assertThat(profile).nodeAtPath("activation/file").isNull(); + }); + } + + @Test + void powWithProfileActivationOs() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").activation().os("linux", "intel", "x68", "1.0"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).nodeAtPath("activation/jdk").isNull(); + assertThat(profile).textAtPath("activation/os/name").isEqualTo("linux"); + assertThat(profile).textAtPath("activation/os/family").isEqualTo("intel"); + assertThat(profile).textAtPath("activation/os/arch").isEqualTo("x68"); + assertThat(profile).textAtPath("activation/os/version").isEqualTo("1.0"); + assertThat(profile).nodeAtPath("activation/property").isNull(); + assertThat(profile).nodeAtPath("activation/file").isNull(); + }); + } + + @Test + void powWithProfileActivationProperty() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").activation().property("name1", "value1"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).nodeAtPath("activation/jdk").isNull(); + assertThat(profile).nodeAtPath("activation/os").isNull(); + assertThat(profile).textAtPath("activation/property/name").isEqualTo("name1"); + assertThat(profile).textAtPath("activation/property/value").isEqualTo("value1"); + assertThat(profile).nodeAtPath("activation/file").isNull(); + }); + } + + @Test + void powWithProfileActivationFileExists() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").activation().fileExists("test.txt"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).nodeAtPath("activation/jdk").isNull(); + assertThat(profile).nodeAtPath("activation/os").isNull(); + assertThat(profile).nodeAtPath("activation/property").isNull(); + assertThat(profile).textAtPath("activation/file/exists").isEqualTo("test.txt"); + assertThat(profile).nodeAtPath("activation/file/missing").isNull(); + }); + } + + @Test + void powWithProfileActivationFileMissing() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").activation().fileMissing("test.txt"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).nodeAtPath("activation/jdk").isNull(); + assertThat(profile).nodeAtPath("activation/os").isNull(); + assertThat(profile).nodeAtPath("activation/property").isNull(); + assertThat(profile).nodeAtPath("activation/file/exists").isNull(); + assertThat(profile).textAtPath("activation/file/missing").isEqualTo("test.txt"); + }); + } + + @Test + void powWithProfileSettings() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").settings().defaultGoal("compile").finalName("app"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).textAtPath("build/defaultGoal").isEqualTo("compile"); + assertThat(profile).textAtPath("build/finalName").isEqualTo("app"); + }); + } + + @Test + void pomWithProfileResources() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").resources().add("src/main/custom", + (resource) -> resource.includes("**/*.properties")); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).textAtPath("build/resources/resource/directory").isEqualTo("src/main/custom"); + assertThat(profile).textAtPath("build/resources/resource/targetPath").isNullOrEmpty(); + assertThat(profile).textAtPath("build/resources/resource/filtering").isNullOrEmpty(); + assertThat(profile).textAtPath("build/resources/resource/includes/include").isEqualTo("**/*.properties"); + assertThat(profile).textAtPath("build/resources/resource/excludes").isNullOrEmpty(); + assertThat(profile).textAtPath("build/testResources").isNullOrEmpty(); + }); + } + + @Test + void pomWithProfileTestResources() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").testResources().add("src/test/custom", + (resource) -> resource.excludes("**/*.gen").filtering(true).targetPath("test")); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + assertThat(profile).textAtPath("build/resources").isNullOrEmpty(); + assertThat(profile).textAtPath("build/testResources/testResource/directory").isEqualTo("src/test/custom"); + assertThat(profile).textAtPath("build/testResources/testResource/targetPath").isEqualTo("test"); + assertThat(profile).textAtPath("build/testResources/testResource/filtering").isEqualTo("true"); + assertThat(profile).textAtPath("build/testResources/testResource/includes").isNullOrEmpty(); + assertThat(profile).textAtPath("build/testResources/testResource/excludes/exclude").isEqualTo("**/*.gen"); + }); + } + + @Test + void pomWithProfilePlugin() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").plugins().add("org.springframework.boot", "spring-boot-maven-plugin"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + NodeAssert plugin = profile.nodeAtPath("build/plugins/plugin"); + assertThat(plugin).textAtPath("groupId").isEqualTo("org.springframework.boot"); + assertThat(plugin).textAtPath("artifactId").isEqualTo("spring-boot-maven-plugin"); + assertThat(plugin).textAtPath("version").isNullOrEmpty(); + assertThat(plugin).textAtPath("extensions").isNullOrEmpty(); + }); + } + + @Test + void pomWithProfileDistributionManagement() { + MavenBuild build = new MavenBuild(); + build.profiles().id("profile1").distributionManagement().downloadUrl("https://example.com/download"); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + NodeAssert distributionManagement = profile.nodeAtPath("distributionManagement"); + assertThat(distributionManagement).textAtPath("downloadUrl").isEqualTo("https://example.com/download"); + assertThat(distributionManagement).nodeAtPath("repository").isNull(); + assertThat(distributionManagement).nodeAtPath("snapshotRepository").isNull(); + assertThat(distributionManagement).nodeAtPath("site").isNull(); + assertThat(distributionManagement).nodeAtPath("relocation").isNull(); + }); + } + private void generatePom(MavenBuild mavenBuild, Consumer consumer) { consumer.accept(new NodeAssert(writePom(new MavenBuildWriter(), mavenBuild))); } diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileActivationTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileActivationTests.java new file mode 100644 index 00000000..e87d05ba --- /dev/null +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileActivationTests.java @@ -0,0 +1,114 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +import io.spring.initializr.generator.buildsystem.maven.MavenProfileActivation.Builder; +import io.spring.initializr.generator.buildsystem.maven.MavenProfileActivation.Os; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link MavenProfileActivation}. + */ +public class MavenProfileActivationTests { + + @Test + void profileWithNoActivation() { + assertThat(createProfileActivation().build().isEmpty()).isTrue(); + } + + @Test + void profileActiveByDefault() { + assertThat(createProfileActivation().activeByDefault(true).build().getActiveByDefault()).isTrue(); + } + + @Test + void profileActiveByDefaultCanBeAmended() { + assertThat(createProfileActivation().activeByDefault(true).activeByDefault(null).build().getActiveByDefault()) + .isNull(); + } + + @Test + void profileActivationJdk() { + assertThat(createProfileActivation().jdk("15").build().getJdk()).isEqualTo("15"); + } + + @Test + void profileActivationCanBeAmended() { + assertThat(createProfileActivation().jdk("15").jdk(null).build().getJdk()).isNull(); + } + + @Test + void profileActivationOs() { + Os os = createProfileActivation().os("test-name", null, "arm64", null).build().getOs(); + assertThat(os).isNotNull(); + assertThat(os.getName()).isEqualTo("test-name"); + assertThat(os.getFamily()).isNull(); + assertThat(os.getArch()).isEqualTo("arm64"); + assertThat(os.getVersion()).isNull(); + } + + @Test + void profileActivationOsCanBeDisabled() { + assertThat( + createProfileActivation().os("test-name", null, null, null).os(null, null, null, null).build().getOs()) + .isNull(); + } + + @Test + void profileActivationProperty() { + assertThat(createProfileActivation().property("test", "1").build().getProperty()).satisfies((property) -> { + assertThat(property).isNotNull(); + assertThat(property.getName()).isEqualTo("test"); + assertThat(property.getValue()).isEqualTo("1"); + }); + } + + @Test + void profileActivationPropertyCanBeDisabled() { + assertThat(createProfileActivation().property("test", "1").property(null, null).build().getProperty()).isNull(); + } + + @Test + void profileActivationFileExisting() { + assertThat(createProfileActivation().fileExists("test.txt").build().getFile()).satisfies((file) -> { + assertThat(file).isNotNull(); + assertThat(file.getExists()).isEqualTo("test.txt"); + assertThat(file.getMissing()).isNull(); + }); + } + + @Test + void profileActivationFileMissing() { + assertThat(createProfileActivation().fileMissing("test.txt").build().getFile()).satisfies((file) -> { + assertThat(file).isNotNull(); + assertThat(file.getExists()).isNull(); + assertThat(file.getMissing()).isEqualTo("test.txt"); + }); + } + + @Test + void profileActivationFileCanBeDisabled() { + assertThat(createProfileActivation().fileMissing("test.txt").fileMissing(null).build().getFile()).isNull(); + } + + private MavenProfileActivation.Builder createProfileActivation() { + return new Builder(); + } + +} diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileContainerTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileContainerTests.java new file mode 100644 index 00000000..39e5733b --- /dev/null +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileContainerTests.java @@ -0,0 +1,96 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +import io.spring.initializr.generator.buildsystem.BuildItemResolver; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link MavenProfileContainer}. + */ +class MavenProfileContainerTests { + + @Test + void profileWithSameIdReturnSameInstance() { + MavenProfileContainer container = createTestContainer(); + MavenProfile profile = container.id("profile1"); + assertThat(container.id("profile1")).isSameAs(profile); + } + + @Test + void isEmptyWithEmptyContainer() { + MavenProfileContainer container = createTestContainer(); + assertThat(container.isEmpty()).isTrue(); + } + + @Test + void isEmptyWithRegisteredProfile() { + MavenProfileContainer container = createTestContainer(); + container.id("profile1"); + assertThat(container.isEmpty()).isFalse(); + } + + @Test + void idsWithEmptyContainer() { + MavenProfileContainer container = createTestContainer(); + assertThat(container.ids()).isEmpty(); + } + + @Test + void idsWithRegisteredProfile() { + MavenProfileContainer container = createTestContainer(); + container.id("profile1"); + assertThat(container.ids()).containsOnly("profile1"); + } + + @Test + void hasProfileWithMatchingProfile() { + MavenProfileContainer container = createTestContainer(); + container.id("profile1"); + assertThat(container.has("profile1")).isTrue(); + } + + @Test + void hasProfileWithNonMatchingProfile() { + MavenProfileContainer container = createTestContainer(); + container.id("profile1"); + assertThat(container.has("profile2")).isFalse(); + } + + @Test + void removeWithMatchingProfile() { + MavenProfileContainer container = createTestContainer(); + container.id("profile1"); + assertThat(container.remove("profile1")).isTrue(); + assertThat(container.isEmpty()).isTrue(); + } + + @Test + void removeWithNonMatchingProfile() { + MavenProfileContainer container = createTestContainer(); + container.id("profile1"); + assertThat(container.remove("profile2")).isFalse(); + assertThat(container.isEmpty()).isFalse(); + } + + private MavenProfileContainer createTestContainer() { + return new MavenProfileContainer(BuildItemResolver.NO_OP); + } + +} diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileTests.java new file mode 100644 index 00000000..57dcbb4f --- /dev/null +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenProfileTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +import io.spring.initializr.generator.buildsystem.BuildItemResolver; +import io.spring.initializr.generator.buildsystem.maven.MavenProfile.Settings; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link MavenProfile}. + */ +class MavenProfileTests { + + @Test + void profileWithNoCustomization() { + MavenProfile profile = createProfile("test"); + assertThat(profile.getId()).isEqualTo("test"); + assertThat(profile.getActivation().isEmpty()).isTrue(); + assertThat(profile.properties().isEmpty()).isTrue(); + assertThat(profile.dependencies().isEmpty()).isTrue(); + assertThat(profile.resources().isEmpty()).isTrue(); + assertThat(profile.testResources().isEmpty()).isTrue(); + assertThat(profile.plugins().isEmpty()).isTrue(); + assertThat(profile.boms().isEmpty()).isTrue(); + assertThat(profile.repositories().isEmpty()).isTrue(); + assertThat(profile.pluginRepositories().isEmpty()).isTrue(); + assertThat(profile.getDistributionManagement().isEmpty()).isTrue(); + } + + @Test + void profileWithActivation() { + MavenProfile profile = createProfile("test"); + profile.activation().jdk("15").property("test", "value").jdk(null); + assertThat(profile.getActivation().getProperty()).satisfies((property) -> { + assertThat(property.getName()).isEqualTo("test"); + assertThat(property.getValue()).isEqualTo("value"); + }); + assertThat(profile.getActivation().getJdk()).isNull(); + } + + @Test + void profileWithDefaultGoal() { + MavenProfile profile = createProfile("test"); + profile.settings().defaultGoal("verify"); + Settings settings = profile.getSettings(); + assertThat(settings.getDefaultGoal()).isEqualTo("verify"); + assertThat(settings.getFinalName()).isNull(); + } + + @Test + void profileWithFinalName() { + MavenProfile profile = createProfile("test"); + profile.settings().finalName("test-app"); + Settings settings = profile.getSettings(); + assertThat(settings.getDefaultGoal()).isNull(); + assertThat(settings.getFinalName()).isEqualTo("test-app"); + } + + @Test + void profileWithDistributionManagement() { + MavenProfile profile = createProfile("test"); + profile.distributionManagement().downloadUrl("https://example.com/download"); + MavenDistributionManagement dm = profile.getDistributionManagement(); + assertThat(dm.getDownloadUrl()).isEqualTo("https://example.com/download"); + assertThat(dm.getRepository().isEmpty()).isTrue(); + assertThat(dm.getSnapshotRepository().isEmpty()).isTrue(); + assertThat(dm.getSite().isEmpty()).isTrue(); + assertThat(dm.getRepository().isEmpty()).isTrue(); + } + + private MavenProfile createProfile(String id) { + return new MavenProfile(id, BuildItemResolver.NO_OP); + } + +}