From 421eb13b92703ad2861d8cb9e6795fd6b27a5fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Fri, 19 Jan 2024 22:57:17 +0100 Subject: [PATCH 1/9] Fix formatting --- .../jhipster/lite/module/domain/JHipsterModulesFixture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java index a3fe08f65a5..95a4bb6d5e5 100644 --- a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java +++ b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java @@ -92,7 +92,7 @@ public static JHipsterModule module() { .pluginManagement(mavenEnforcerPluginManagement()) .plugin(mavenEnforcerPlugin()) .and() - .javaBuildProfiles() + .javaBuildProfiles() .addProfile(localMavenProfile()) .activation(buildProfileActivation().activeByDefault(false)) .properties() From 22b4e36933c25fb17fa2b5676df094d890982a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Fri, 19 Jan 2024 22:58:05 +0100 Subject: [PATCH 2/9] Dependencies can be declared for maven plugins --- .../javabuild/command/AddJavaDependency.java | 35 ---- .../javabuild/command/AddMavenPlugin.java | 6 + .../domain/mavenplugin/MavenPlugin.java | 21 +++ .../maven/MavenCommandHandler.java | 37 ++-- .../module/domain/JHipsterModulesFixture.java | 11 +- ...leSystemJHipsterModulesRepositoryTest.java | 28 +++- .../maven/MavenCommandHandlerTest.java | 158 +++++++++++++++++- 7 files changed, 237 insertions(+), 59 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddJavaDependency.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddJavaDependency.java index f5b3257e8ac..f75ac7bc40d 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddJavaDependency.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddJavaDependency.java @@ -1,42 +1,7 @@ package tech.jhipster.lite.module.domain.javabuild.command; -import java.util.Collection; -import java.util.Optional; -import tech.jhipster.lite.module.domain.javabuild.VersionSlug; -import tech.jhipster.lite.module.domain.javadependency.DependencyId; import tech.jhipster.lite.module.domain.javadependency.JavaDependency; -import tech.jhipster.lite.module.domain.javadependency.JavaDependencyClassifier; -import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope; -import tech.jhipster.lite.module.domain.javadependency.JavaDependencyType; public sealed interface AddJavaDependency permits AddDirectJavaDependency, AddJavaDependencyManagement { JavaDependency dependency(); - - default Optional version() { - return dependency().version(); - } - - default Optional classifier() { - return dependency().classifier(); - } - - default JavaDependencyScope scope() { - return dependency().scope(); - } - - default boolean optional() { - return dependency().optional(); - } - - default DependencyId dependencyId() { - return dependency().id(); - } - - default Optional dependencyType() { - return dependency().type(); - } - - default Collection exclusions() { - return dependency().exclusions(); - } } diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java index e6018efc628..f162b56bd73 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java @@ -1,8 +1,10 @@ package tech.jhipster.lite.module.domain.javabuild.command; +import java.util.Collection; import java.util.Optional; import tech.jhipster.lite.module.domain.javabuild.VersionSlug; import tech.jhipster.lite.module.domain.javadependency.DependencyId; +import tech.jhipster.lite.module.domain.javadependency.JavaDependency; import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion; import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin; import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginConfiguration; @@ -23,6 +25,10 @@ default Optional executions() { return plugin().executions(); } + default Collection dependencies() { + return plugin().dependencies(); + } + default DependencyId dependencyId() { return plugin().dependencyId(); } diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java index 914cc117a76..539af8169c3 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java +++ b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Optional; import org.apache.commons.lang3.builder.ToStringBuilder; @@ -12,6 +13,7 @@ import tech.jhipster.lite.module.domain.javabuild.GroupId; import tech.jhipster.lite.module.domain.javabuild.VersionSlug; import tech.jhipster.lite.module.domain.javadependency.DependencyId; +import tech.jhipster.lite.module.domain.javadependency.JavaDependency; import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecution.MavenPluginExecutionOptionalBuilder; import tech.jhipster.lite.shared.generation.domain.ExcludeFromGeneratedCodeCoverage; @@ -21,12 +23,14 @@ public class MavenPlugin { private final Optional versionSlug; private final Optional configuration; private final Optional executions; + private final Collection dependencies; private MavenPlugin(MavenPluginBuilder builder) { dependencyId = DependencyId.of(builder.groupId, builder.artifactId); versionSlug = Optional.ofNullable(builder.versionSlug); configuration = Optional.ofNullable(builder.configuration); executions = Optional.ofNullable(builder.executions).filter(not(Collection::isEmpty)).map(MavenPluginExecutions::new); + dependencies = builder.dependencies; } public static MavenPluginGroupIdBuilder builder() { @@ -49,6 +53,10 @@ public DependencyId dependencyId() { return dependencyId; } + public Collection dependencies() { + return dependencies; + } + private static class MavenPluginBuilder implements MavenPluginGroupIdBuilder, MavenPluginArtifactIdBuilder, MavenPluginOptionalBuilder { private GroupId groupId; @@ -56,6 +64,7 @@ private static class MavenPluginBuilder implements MavenPluginGroupIdBuilder, Ma private VersionSlug versionSlug; private MavenPluginConfiguration configuration; private final List executions = new ArrayList<>(); + private final Collection dependencies = new HashSet<>(); private MavenPluginBuilder() {} @@ -92,6 +101,12 @@ public MavenPluginOptionalBuilder addExecution(MavenPluginExecution executions) return this; } + @Override + public MavenPluginOptionalBuilder addDependency(JavaDependency dependency) { + this.dependencies.add(dependency); + return this; + } + @Override public MavenPlugin build() { return new MavenPlugin(this); @@ -134,6 +149,12 @@ default MavenPluginOptionalBuilder configuration(String configuration) { default MavenPluginOptionalBuilder addExecution(MavenPluginExecutionOptionalBuilder builder) { return addExecution(builder.build()); } + + MavenPluginOptionalBuilder addDependency(JavaDependency javaDependency); + + default MavenPluginOptionalBuilder addDependency(GroupId groupId, ArtifactId artifactId, VersionSlug version) { + return addDependency(JavaDependency.builder().groupId(groupId).artifactId(artifactId).versionSlug(version).build()); + } } @Override diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java index b25f3476b48..6ad5462c8fc 100644 --- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java +++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java @@ -41,7 +41,6 @@ import tech.jhipster.lite.module.domain.javabuild.command.AddDirectMavenPlugin; import tech.jhipster.lite.module.domain.javabuild.command.AddGradlePlugin; import tech.jhipster.lite.module.domain.javabuild.command.AddJavaBuildProfile; -import tech.jhipster.lite.module.domain.javabuild.command.AddJavaDependency; import tech.jhipster.lite.module.domain.javabuild.command.AddJavaDependencyManagement; import tech.jhipster.lite.module.domain.javabuild.command.AddMavenBuildExtension; import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPlugin; @@ -53,6 +52,7 @@ import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; import tech.jhipster.lite.module.domain.javadependency.DependencyId; +import tech.jhipster.lite.module.domain.javadependency.JavaDependency; import tech.jhipster.lite.module.domain.javadependency.JavaDependencyClassifier; import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope; import tech.jhipster.lite.module.domain.mavenplugin.MavenBuildPhase; @@ -212,7 +212,7 @@ private static Extension toMavenExtension(MavenBuildExtension mavenBuildExtensio public void handle(AddJavaDependencyManagement command) { Assert.notNull(COMMAND, command); - addDependencyTo(command, dependencyManagement().getDependencies()); + addDependencyTo(command.dependency(), dependencyManagement().getDependencies()); } private DependencyManagement dependencyManagement() { @@ -226,14 +226,14 @@ private DependencyManagement dependencyManagement() { public void handle(AddDirectJavaDependency command) { Assert.notNull(COMMAND, command); - addDependencyTo(command, pomModel.getDependencies()); + addDependencyTo(command.dependency(), pomModel.getDependencies()); } - private void addDependencyTo(AddJavaDependency command, List dependencies) { - if (command.scope() == JavaDependencyScope.TEST) { - dependencies.add(toMavenDependency(command)); + private void addDependencyTo(JavaDependency dependency, List dependencies) { + if (dependency.scope() == JavaDependencyScope.TEST) { + dependencies.add(toMavenDependency(dependency)); } else { - Dependency mavenDependency = toMavenDependency(command); + Dependency mavenDependency = toMavenDependency(dependency); insertDependencyBeforeFirstTestDependency(mavenDependency, dependencies); } @@ -252,19 +252,19 @@ private void insertDependencyBeforeFirstTestDependency(Dependency mavenDependenc } } - private Dependency toMavenDependency(AddJavaDependency command) { + private Dependency toMavenDependency(JavaDependency javaDependency) { Dependency mavenDependency = new Dependency(); - mavenDependency.setGroupId(command.dependencyId().groupId().get()); - mavenDependency.setArtifactId(command.dependencyId().artifactId().get()); - command.version().map(VersionSlug::mavenVariable).ifPresent(mavenDependency::setVersion); - command.classifier().map(JavaDependencyClassifier::get).ifPresent(mavenDependency::setClassifier); - command.dependencyType().map(type -> Enums.map(type, MavenType.class)).map(MavenType::key).ifPresent(mavenDependency::setType); - command.exclusions().stream().map(toMavenExclusion()).forEach(mavenDependency::addExclusion); - - if (command.scope() != JavaDependencyScope.COMPILE) { - mavenDependency.setScope(Enums.map(command.scope(), MavenScope.class).key()); + mavenDependency.setGroupId(javaDependency.id().groupId().get()); + mavenDependency.setArtifactId(javaDependency.id().artifactId().get()); + javaDependency.version().map(VersionSlug::mavenVariable).ifPresent(mavenDependency::setVersion); + javaDependency.classifier().map(JavaDependencyClassifier::get).ifPresent(mavenDependency::setClassifier); + javaDependency.type().map(type -> Enums.map(type, MavenType.class)).map(MavenType::key).ifPresent(mavenDependency::setType); + javaDependency.exclusions().stream().map(toMavenExclusion()).forEach(mavenDependency::addExclusion); + + if (javaDependency.scope() != JavaDependencyScope.COMPILE) { + mavenDependency.setScope(Enums.map(javaDependency.scope(), MavenScope.class).key()); } - if (command.optional()) { + if (javaDependency.optional()) { mavenDependency.setOptional(true); } @@ -347,6 +347,7 @@ private Plugin toMavenPlugin(AddMavenPlugin command) { .flatMap(Collection::stream) .map(toMavenExecution()) .forEach(mavenPlugin::addExecution); + command.dependencies().stream().map(this::toMavenDependency).forEach(mavenPlugin::addDependency); return mavenPlugin; } diff --git a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java index 95a4bb6d5e5..243aaa88030 100644 --- a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java +++ b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java @@ -186,8 +186,13 @@ public static JavaDependency defaultVersionDependency() { return javaDependency().groupId("org.springframework.boot").artifactId("spring-boot-starter").build(); } - public static JavaDependency dependencyWithVersion() { - return javaDependency().groupId("io.jsonwebtoken").artifactId("jjwt-api").versionSlug("json-web-token").build(); + public static JavaDependency dependencyWithVersionAndExclusion() { + return javaDependency() + .groupId("io.jsonwebtoken") + .artifactId("jjwt-jackson") + .versionSlug("jjwt-jackson") + .addExclusion(DependencyId.of(new GroupId("com.fasterxml.jackson.core"), new ArtifactId("jackson-databind"))) + .build(); } public static JavaBuildCommands javaDependenciesCommands() { @@ -317,6 +322,8 @@ public static MavenPlugin mavenEnforcerPluginManagement() { .groupId("org.apache.maven.plugins") .artifactId("maven-enforcer-plugin") .versionSlug("maven-enforcer-plugin") + .addDependency(dependencyWithVersionAndExclusion()) + .addDependency(groupId("io.jsonwebtoken"), artifactId("jjwt-jackson"), versionSlug("jjwt-jackson")) .addExecution(pluginExecution().goals("enforce").id("enforce-versions")) .addExecution( pluginExecution() diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java index edecc00d254..6845a05bb9d 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java @@ -21,7 +21,7 @@ class FileSystemJHipsterModulesRepositoryTest { private LogsSpy logs; @Test - void shouldApplyModule() { + void shouldApplyMavenModule() { JHipsterModule module = module(); // @formatter:off @@ -165,6 +165,19 @@ void shouldApplyModule() { + + + io.jsonwebtoken + jjwt-jackson + ${jjwt-jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + + + + @@ -224,6 +237,19 @@ void shouldApplyModule() { + + + io.jsonwebtoken + jjwt-jackson + ${jjwt-jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + + + + diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java index b584712eecb..ee259dd90a6 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java @@ -763,6 +763,19 @@ private String pluginManagement() { + + + io.jsonwebtoken + jjwt-jackson + ${jjwt-jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + + + + @@ -812,7 +825,7 @@ void shouldAddBuildPluginToPomWithPlugins() { private void addMavenEnforcerPlugin(Path pom) { AddMavenPluginManagement command = AddMavenPluginManagement .builder() - .plugin(mavenEnforcerPlugin()) + .plugin(mavenEnforcerPluginManagement()) .pluginVersion(mavenEnforcerVersion()) .buildProfile(localMavenProfile()) .build(); @@ -828,6 +841,52 @@ private String plugins() { maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-versions + + enforce + + + + enforce-dependencyConvergence + + enforce + + + + + + false + + + + + + io.jsonwebtoken + jjwt-jackson + ${jjwt-jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + + + + + + + + You are running an older version of Maven. JHipster requires at least Maven ${maven.version} + [${maven.version},) + + + You are running an incompatible version of Java. JHipster engine supports JDK 21+. + [21,22) + + + @@ -921,13 +980,60 @@ void shouldAddMinimalBuildPluginExecutionToPomWithPlugins() { } private void addMavenEnforcerPlugin(Path pom) { - new MavenCommandHandler(Indentation.DEFAULT, pom).handle(AddDirectMavenPlugin.builder().plugin(mavenEnforcerPlugin()).build()); + new MavenCommandHandler(Indentation.DEFAULT, pom) + .handle(AddDirectMavenPlugin.builder().plugin(mavenEnforcerPluginManagement()).build()); } private String plugins() { return """ maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-versions + + enforce + + + + enforce-dependencyConvergence + + enforce + + + + + + false + + + + + + io.jsonwebtoken + jjwt-jackson + ${jjwt-jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + + + + + + + + You are running an older version of Maven. JHipster requires at least Maven ${maven.version} + [${maven.version},) + + + You are running an incompatible version of Java. JHipster engine supports JDK 21+. + [21,22) + + + """; @@ -967,7 +1073,7 @@ private void addMavenEnforcerPlugin(Path pom) { .handle( AddDirectMavenPlugin .builder() - .plugin(mavenEnforcerPlugin()) + .plugin(mavenEnforcerPluginManagement()) .pluginVersion(mavenEnforcerVersion()) .buildProfile(localMavenProfile()) .build() @@ -982,6 +1088,52 @@ private String plugins() { maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-versions + + enforce + + + + enforce-dependencyConvergence + + enforce + + + + + + false + + + + + + io.jsonwebtoken + jjwt-jackson + ${jjwt-jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + + + + + + + + You are running an older version of Maven. JHipster requires at least Maven ${maven.version} + [${maven.version},) + + + You are running an incompatible version of Java. JHipster engine supports JDK 21+. + [21,22) + + + From 3d34ff1310b1693d7eb6e53e40003ef3dec151e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Fri, 19 Jan 2024 23:02:31 +0100 Subject: [PATCH 3/9] Replace MavenPluginExecutions with Collection --- .../javabuild/command/AddMavenPlugin.java | 4 ++-- .../domain/mavenplugin/MavenPlugin.java | 15 +++++++------ .../mavenplugin/MavenPluginExecutions.java | 21 ------------------- .../maven/MavenCommandHandler.java | 10 +-------- 4 files changed, 12 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPluginExecutions.java diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java index f162b56bd73..09299b7618b 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPlugin.java @@ -8,7 +8,7 @@ import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion; import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin; import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginConfiguration; -import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecutions; +import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecution; public interface AddMavenPlugin { MavenPlugin plugin(); @@ -21,7 +21,7 @@ default Optional configuration() { return plugin().configuration(); } - default Optional executions() { + default Collection executions() { return plugin().executions(); } diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java index 539af8169c3..f5765aa9126 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java +++ b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPlugin.java @@ -1,7 +1,5 @@ package tech.jhipster.lite.module.domain.mavenplugin; -import static java.util.function.Predicate.not; - import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -15,6 +13,7 @@ import tech.jhipster.lite.module.domain.javadependency.DependencyId; import tech.jhipster.lite.module.domain.javadependency.JavaDependency; import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecution.MavenPluginExecutionOptionalBuilder; +import tech.jhipster.lite.shared.error.domain.Assert; import tech.jhipster.lite.shared.generation.domain.ExcludeFromGeneratedCodeCoverage; public class MavenPlugin { @@ -22,14 +21,17 @@ public class MavenPlugin { private final DependencyId dependencyId; private final Optional versionSlug; private final Optional configuration; - private final Optional executions; + private final Collection executions; private final Collection dependencies; private MavenPlugin(MavenPluginBuilder builder) { + Assert.notNull("executions", builder.executions); + Assert.notNull("dependencies", builder.dependencies); + dependencyId = DependencyId.of(builder.groupId, builder.artifactId); versionSlug = Optional.ofNullable(builder.versionSlug); configuration = Optional.ofNullable(builder.configuration); - executions = Optional.ofNullable(builder.executions).filter(not(Collection::isEmpty)).map(MavenPluginExecutions::new); + executions = builder.executions; dependencies = builder.dependencies; } @@ -45,7 +47,7 @@ public Optional configuration() { return configuration; } - public Optional executions() { + public Collection executions() { return executions; } @@ -165,7 +167,8 @@ public String toString() { .append("dependencyId", dependencyId) .append("versionSlug", versionSlug.map(VersionSlug::toString).orElse("(empty)")) .append("configuration", configuration.map(MavenPluginConfiguration::toString).orElse("(empty)")) - .append("executions", executions.map(MavenPluginExecutions::toString).orElse("(empty)")); + .append("executions", executions) + .append("dependencies", dependencies); return builder.toString(); } } diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPluginExecutions.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPluginExecutions.java deleted file mode 100644 index e91971f0dfc..00000000000 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/MavenPluginExecutions.java +++ /dev/null @@ -1,21 +0,0 @@ -package tech.jhipster.lite.module.domain.mavenplugin; - -import java.util.Collection; -import tech.jhipster.lite.shared.error.domain.Assert; -import tech.jhipster.lite.shared.generation.domain.ExcludeFromGeneratedCodeCoverage; - -public record MavenPluginExecutions(Collection executions) { - public MavenPluginExecutions { - Assert.notEmpty("executions", executions); - } - - public Collection get() { - return executions; - } - - @Override - @ExcludeFromGeneratedCodeCoverage - public String toString() { - return executions.toString(); - } -} diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java index 6ad5462c8fc..0d44176dd32 100644 --- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java +++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java @@ -9,7 +9,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collection; import java.util.List; import java.util.function.Function; import java.util.function.Predicate; @@ -60,7 +59,6 @@ import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecution; import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecutionGoal; import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecutionId; -import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecutions; import tech.jhipster.lite.module.infrastructure.secondary.javadependency.JavaDependenciesCommandHandler; import tech.jhipster.lite.shared.enumeration.domain.Enums; import tech.jhipster.lite.shared.error.domain.Assert; @@ -340,13 +338,7 @@ private Plugin toMavenPlugin(AddMavenPlugin command) { mavenPlugin.setGroupId(command.dependencyId().groupId().get()); command.versionSlug().map(VersionSlug::mavenVariable).ifPresent(mavenPlugin::setVersion); command.configuration().map(toMavenConfiguration()).ifPresent(mavenPlugin::setConfiguration); - command - .executions() - .stream() - .map(MavenPluginExecutions::get) - .flatMap(Collection::stream) - .map(toMavenExecution()) - .forEach(mavenPlugin::addExecution); + command.executions().stream().map(toMavenExecution()).forEach(mavenPlugin::addExecution); command.dependencies().stream().map(this::toMavenDependency).forEach(mavenPlugin::addDependency); return mavenPlugin; } From ca0dce6129d2638a57e5651447f821578e3f15ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sat, 20 Jan 2024 13:06:00 +0100 Subject: [PATCH 4/9] Rename nested test classes for consistency --- .../maven/MavenCommandHandlerTest.java | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java index ee259dd90a6..0981d281d89 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java @@ -9,7 +9,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.regex.Pattern; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import tech.jhipster.lite.TestFileUtils; @@ -65,8 +64,7 @@ void shouldEnforceIndentation() { } @Nested - @DisplayName("Set dependency version") - class MavenCommandHandlerSetVersionTest { + class HandleSetVersion { @Test void shouldAddPropertiesToPomWithOnlyRootDefined() { @@ -115,7 +113,6 @@ void shouldUpdateExistingProperty() { } @Nested - @DisplayName("Set build property") class HandleSetBuildProperty { @Nested @@ -254,7 +251,6 @@ void shouldUpdateExistingProperty() { } @Nested - @DisplayName("Add build profile") class HandleAddJavaBuildProfile { @Test @@ -353,8 +349,7 @@ void shouldAddProfileWithEmptyActivation() { } @Nested - @DisplayName("Remove dependency management") - class MavenCommandHandlerRemoveDependencyManagementTest { + class HandleRemoveJavaDependencyManagement { @Test void shouldNotRemoveUnknownDependency() { @@ -392,8 +387,7 @@ void shouldRemoveDependency() { } @Nested - @DisplayName("Add dependency management") - class MavenCommandHandlerAddDependencyManagementTest { + class HandleAddJavaDependencyManagement { @Test void shouldAddDependencyInPomWithoutDependenciesManagement() { @@ -468,8 +462,7 @@ void shouldAddDependencyManagementWithExclusion() { } @Nested - @DisplayName("Remove dependency") - class MavenCommandHandlerRemoveDependencyTest { + class HandleRemoveDirectJavaDependency { @Test void shouldNotRemoveUnknownDependency() { @@ -521,8 +514,7 @@ void shouldRemoveDependencyWithFullyMatchingId() { } @Nested - @DisplayName("Add dependency") - class MavenCommandHandlerAddDependencyTest { + class HandleAddDirectJavaDependency { @Test void shouldAddDependencyInPomWithoutDependencies() { @@ -653,8 +645,7 @@ void shouldAddCompileDependencyInPomWithEmptyDependencies() { } @Nested - @DisplayName("Add build plugin management") - class MavenCommandHandlerAddMavenPluginManagementTest { + class HandleAddMavenPluginManagement { @Nested class WithoutProfile { @@ -898,8 +889,7 @@ private String plugins() { } @Nested - @DisplayName("Add build plugin") - class MavenCommandHandlerAddBuildPluginTest { + class HandleAddDirectMavenPlugin { @Nested class WithoutProfile { @@ -1144,7 +1134,7 @@ private String plugins() { } @Nested - class AddBuildExtension { + class HandleAddMavenBuildExtension { @Test void shouldAddBuildExtensionInPomWithoutBuild() { @@ -1221,7 +1211,7 @@ void shouldAddBuildExtensionInPomWithExtensions() { } @Test - void addAddGradlePluginShouldNotBeHandled() { + void addGradlePluginCommandShouldNotBeHandled() { Path pom = projectWithPom("src/test/resources/projects/maven/pom.xml"); MavenCommandHandler commandHandler = new MavenCommandHandler(Indentation.DEFAULT, pom); From a4c35973747129383edf6401f0c9c8a25da68dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sat, 20 Jan 2024 13:51:06 +0100 Subject: [PATCH 5/9] Add maven properties for version dependencies of maven plugins --- .../module/domain/JHipsterModulesApplyer.java | 4 +- .../command/AddDirectMavenPlugin.java | 18 ++++++++ .../command/AddMavenPluginManagement.java | 20 +++++++- .../JHipsterModuleJavaBuildProfiles.java | 12 +++-- .../domain/javadependency/JavaDependency.java | 4 +- .../JHipsterModuleMavenPlugins.java | 46 ++++++++++++++++--- .../maven/MavenCommandHandler.java | 2 + .../module/domain/JHipsterModulesFixture.java | 10 ++-- .../JHipsterModuleMavenPluginsTest.java | 23 ++++++++-- ...leSystemJHipsterModulesRepositoryTest.java | 5 +- .../maven/MavenCommandHandlerTest.java | 36 +++++++++++++-- 11 files changed, 150 insertions(+), 30 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModulesApplyer.java b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModulesApplyer.java index 2714c43c665..ef3769e6599 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModulesApplyer.java +++ b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModulesApplyer.java @@ -124,11 +124,11 @@ private JavaBuildCommands buildDependenciesChanges(JHipsterModule module) { } private JavaBuildCommands buildProfilesChanges(JHipsterModule module) { - return module.javaBuildProfiles().buildChanges(javaVersions.get()); + return module.javaBuildProfiles().buildChanges(javaVersions.get(), projectDependencies.get(module.projectFolder())); } private JavaBuildCommands buildPluginsChanges(JHipsterModule module) { - return module.mavenPlugins().buildChanges(javaVersions.get()); + return module.mavenPlugins().buildChanges(javaVersions.get(), projectDependencies.get(module.projectFolder())); } private JavaBuildCommands buildMavenBuildExtensionsChanges(JHipsterModule module) { diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java index 024bd8dc970..85a5b774c7d 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java @@ -1,5 +1,7 @@ package tech.jhipster.lite.module.domain.javabuild.command; +import java.util.Collection; +import java.util.HashSet; import java.util.Optional; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion; @@ -11,10 +13,13 @@ public final class AddDirectMavenPlugin implements JavaBuildCommand, AddMavenPlu private final MavenPlugin plugin; private final Optional pluginVersion; private final Optional buildProfile; + private final Collection dependenciesVersions; private AddDirectMavenPlugin(AddDirectMavenPluginBuilder builder) { Assert.notNull("plugin", builder.plugin); + Assert.notNull("dependenciesVersions", builder.dependenciesVersions); this.plugin = builder.plugin; + this.dependenciesVersions = builder.dependenciesVersions; this.pluginVersion = Optional.ofNullable(builder.pluginVersion); this.buildProfile = Optional.ofNullable(builder.buildProfile); } @@ -33,6 +38,10 @@ public Optional pluginVersion() { return pluginVersion; } + public Collection dependenciesVersions() { + return dependenciesVersions; + } + public static AddDirectMavenPluginPluginBuilder builder() { return new AddDirectMavenPluginBuilder(); } @@ -42,6 +51,7 @@ private static class AddDirectMavenPluginBuilder implements AddDirectMavenPlugin private MavenPlugin plugin; private JavaDependencyVersion pluginVersion; private BuildProfileId buildProfile; + private final Collection dependenciesVersions = new HashSet<>(); private AddDirectMavenPluginBuilder() {} @@ -63,6 +73,12 @@ public AddDirectMavenPluginOptionalBuilder buildProfile(BuildProfileId buildProf return this; } + @Override + public AddDirectMavenPluginOptionalBuilder addDependencyVersion(JavaDependencyVersion version) { + this.dependenciesVersions.add(version); + return this; + } + public AddDirectMavenPlugin build() { return new AddDirectMavenPlugin(this); } @@ -75,6 +91,8 @@ public interface AddDirectMavenPluginPluginBuilder { public interface AddDirectMavenPluginOptionalBuilder { AddDirectMavenPluginOptionalBuilder pluginVersion(JavaDependencyVersion version); AddDirectMavenPluginOptionalBuilder buildProfile(BuildProfileId buildProfile); + AddDirectMavenPluginOptionalBuilder addDependencyVersion(JavaDependencyVersion version); + AddDirectMavenPlugin build(); } } diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java index c982c5cf977..cc4e1bd6ecf 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java @@ -2,6 +2,8 @@ import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; +import java.util.Collection; +import java.util.HashSet; import java.util.Optional; import org.apache.commons.lang3.builder.ToStringBuilder; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; @@ -15,10 +17,13 @@ public final class AddMavenPluginManagement implements JavaBuildCommand, AddMave private final MavenPlugin plugin; private final Optional pluginVersion; private final Optional buildProfile; + private final Collection dependenciesVersions; private AddMavenPluginManagement(AddMavenPluginManagementBuilder builder) { Assert.notNull("plugin", builder.plugin); + Assert.notNull("dependenciesVersions", builder.dependenciesVersions); this.plugin = builder.plugin; + this.dependenciesVersions = builder.dependenciesVersions; this.pluginVersion = Optional.ofNullable(builder.pluginVersion); this.buildProfile = Optional.ofNullable(builder.buildProfile); } @@ -37,6 +42,10 @@ public Optional buildProfile() { return buildProfile; } + public Collection dependenciesVersions() { + return dependenciesVersions; + } + public static AddMavenPluginManagementPluginBuilder builder() { return new AddMavenPluginManagementBuilder(); } @@ -47,6 +56,7 @@ private static class AddMavenPluginManagementBuilder private MavenPlugin plugin; private JavaDependencyVersion pluginVersion; private BuildProfileId buildProfile; + private final Collection dependenciesVersions = new HashSet<>(); private AddMavenPluginManagementBuilder() {} @@ -68,6 +78,12 @@ public AddMavenPluginManagementOptionalBuilder buildProfile(BuildProfileId build return this; } + @Override + public AddMavenPluginManagementOptionalBuilder addDependencyVersion(JavaDependencyVersion dependencyVersion) { + this.dependenciesVersions.add(dependencyVersion); + return this; + } + public AddMavenPluginManagement build() { return new AddMavenPluginManagement(this); } @@ -80,6 +96,7 @@ public interface AddMavenPluginManagementPluginBuilder { public interface AddMavenPluginManagementOptionalBuilder { AddMavenPluginManagementOptionalBuilder pluginVersion(JavaDependencyVersion version); AddMavenPluginManagementOptionalBuilder buildProfile(BuildProfileId buildProfile); + AddMavenPluginManagementOptionalBuilder addDependencyVersion(JavaDependencyVersion javaDependencyVersion); AddMavenPluginManagement build(); } @@ -88,7 +105,8 @@ public interface AddMavenPluginManagementOptionalBuilder { public String toString() { ToStringBuilder builder = new ToStringBuilder(this, SHORT_PREFIX_STYLE) .append("plugin", plugin) - .append("pluginVersion", pluginVersion.map(JavaDependencyVersion::toString).orElse("(empty)")); + .append("pluginVersion", pluginVersion.map(JavaDependencyVersion::toString).orElse("(empty)")) + .append("dependenciesVersions", pluginVersion.map(JavaDependencyVersion::toString).orElse("(empty)")); return builder.toString(); } } diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuildprofile/JHipsterModuleJavaBuildProfiles.java b/src/main/java/tech/jhipster/lite/module/domain/javabuildprofile/JHipsterModuleJavaBuildProfiles.java index 0b7aa24618e..c19a5008ec4 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuildprofile/JHipsterModuleJavaBuildProfiles.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuildprofile/JHipsterModuleJavaBuildProfiles.java @@ -13,6 +13,7 @@ import tech.jhipster.lite.module.domain.javabuild.command.SetBuildProperty; import tech.jhipster.lite.module.domain.javabuildprofile.JHipsterModuleJavaBuildProfile.JHipsterModuleJavaBuildProfileBuilder; import tech.jhipster.lite.module.domain.javadependency.JavaDependenciesVersions; +import tech.jhipster.lite.module.domain.javadependency.ProjectJavaDependencies; import tech.jhipster.lite.shared.error.domain.Assert; public class JHipsterModuleJavaBuildProfiles { @@ -27,10 +28,10 @@ public static JHipsterModuleJavaBuildProfilesBuilder builder(JHipsterModuleBuild return new JHipsterModuleJavaBuildProfilesBuilder(module); } - public JavaBuildCommands buildChanges(JavaDependenciesVersions versions) { + public JavaBuildCommands buildChanges(JavaDependenciesVersions versions, ProjectJavaDependencies projectJavaDependencies) { Stream addProfileCommands = profiles.stream().map(toAddProfileCommands()); Stream addPropertyCommands = profiles.stream().flatMap(toAddPropertyCommands()); - Stream mavenPluginCommands = profiles.stream().flatMap(toMavenPluginCommands(versions)); + Stream mavenPluginCommands = profiles.stream().flatMap(toMavenPluginCommands(versions, projectJavaDependencies)); Collection commands = Stream .of(addProfileCommands, addPropertyCommands, mavenPluginCommands) @@ -53,8 +54,11 @@ private Function> toAdd .map(property -> new SetBuildProperty(new BuildProperty(property.getKey(), property.getValue()), profile.id())); } - private Function> toMavenPluginCommands(JavaDependenciesVersions versions) { - return profile -> profile.mavenPlugins().buildChanges(versions, profile.id()).commands().stream(); + private Function> toMavenPluginCommands( + JavaDependenciesVersions versions, + ProjectJavaDependencies projectJavaDependencies + ) { + return profile -> profile.mavenPlugins().buildChanges(versions, projectJavaDependencies, profile.id()).commands().stream(); } public static class JHipsterModuleJavaBuildProfilesBuilder { diff --git a/src/main/java/tech/jhipster/lite/module/domain/javadependency/JavaDependency.java b/src/main/java/tech/jhipster/lite/module/domain/javadependency/JavaDependency.java index 18592a0ad52..5dbcea8345d 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javadependency/JavaDependency.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javadependency/JavaDependency.java @@ -53,7 +53,7 @@ Collection versionCommands(JavaDependenciesVersions currentVer return version().flatMap(toVersion(currentVersions, projectDependencies)).map(toSetVersionCommand()).map(List::of).orElse(List.of()); } - private Function> toVersion( + public static Function> toVersion( JavaDependenciesVersions currentVersions, ProjectJavaDependencies projectDependencies ) { @@ -64,7 +64,7 @@ private Function> toVersion( }; } - private Function> toVersionToUse(JavaDependencyVersion currentVersion) { + private static Function> toVersionToUse(JavaDependencyVersion currentVersion) { return version -> { if (version.equals(currentVersion)) { return Optional.empty(); diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java index ee0e811588d..4b9c0078c9c 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java +++ b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java @@ -13,6 +13,8 @@ import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommands; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; import tech.jhipster.lite.module.domain.javadependency.JavaDependenciesVersions; +import tech.jhipster.lite.module.domain.javadependency.JavaDependency; +import tech.jhipster.lite.module.domain.javadependency.ProjectJavaDependencies; import tech.jhipster.lite.shared.error.domain.Assert; public class JHipsterModuleMavenPlugins { @@ -29,31 +31,52 @@ public static JHipsterModuleMavenPluginsBuilder builder(T parentModuleBui return new JHipsterModuleMavenPluginsBuilder<>(parentModuleBuilder); } - public JavaBuildCommands buildChanges(JavaDependenciesVersions versions) { - return buildChanges(versions, Optional.empty()); + public JavaBuildCommands buildChanges(JavaDependenciesVersions versions, ProjectJavaDependencies projectJavaDependencies) { + return buildChanges(versions, projectJavaDependencies, Optional.empty()); } - public JavaBuildCommands buildChanges(JavaDependenciesVersions versions, BuildProfileId buildProfile) { + public JavaBuildCommands buildChanges( + JavaDependenciesVersions versions, + ProjectJavaDependencies projectJavaDependencies, + BuildProfileId buildProfile + ) { Assert.notNull("buildProfile", buildProfile); - return buildChanges(versions, Optional.of(buildProfile)); + return buildChanges(versions, projectJavaDependencies, Optional.of(buildProfile)); } - private JavaBuildCommands buildChanges(JavaDependenciesVersions versions, Optional buildProfile) { + private JavaBuildCommands buildChanges( + JavaDependenciesVersions versions, + ProjectJavaDependencies projectJavaDependencies, + Optional buildProfile + ) { Assert.notNull("versions", versions); - Stream managementCommands = pluginsManagement.stream().map(toAddMavenPluginManagement(versions, buildProfile)); - Stream pluginsCommands = plugins.stream().map(toAddDirectMavenPlugin(versions, buildProfile)); + Stream managementCommands = pluginsManagement + .stream() + .map(toAddMavenPluginManagement(versions, projectJavaDependencies, buildProfile)); + Stream pluginsCommands = plugins + .stream() + .map(toAddDirectMavenPlugin(versions, projectJavaDependencies, buildProfile)); return new JavaBuildCommands(Stream.concat(managementCommands, pluginsCommands).toList()); } private static Function toAddMavenPluginManagement( JavaDependenciesVersions versions, + ProjectJavaDependencies projectDependencies, Optional buildProfile ) { return plugin -> { AddMavenPluginManagementOptionalBuilder commandBuilder = AddMavenPluginManagement.builder().plugin(plugin); buildProfile.ifPresent(commandBuilder::buildProfile); + plugin + .dependencies() + .stream() + .map(JavaDependency::version) + .flatMap(Optional::stream) + .map(JavaDependency.toVersion(versions, projectDependencies)) + .flatMap(Optional::stream) + .forEach(commandBuilder::addDependencyVersion); return plugin .versionSlug() .map(versions::get) @@ -65,11 +88,20 @@ private static Function toAddMavenPluginM private static Function toAddDirectMavenPlugin( JavaDependenciesVersions versions, + ProjectJavaDependencies projectDependencies, Optional buildProfile ) { return plugin -> { AddDirectMavenPluginOptionalBuilder commandBuilder = AddDirectMavenPlugin.builder().plugin(plugin); buildProfile.ifPresent(commandBuilder::buildProfile); + plugin + .dependencies() + .stream() + .map(JavaDependency::version) + .flatMap(Optional::stream) + .map(JavaDependency.toVersion(versions, projectDependencies)) + .flatMap(Optional::stream) + .forEach(commandBuilder::addDependencyVersion); return plugin .versionSlug() .map(versions::get) diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java index 0d44176dd32..8d97f7b3522 100644 --- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java +++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandler.java @@ -283,6 +283,7 @@ public void handle(AddMavenPluginManagement command) { Assert.notNull(COMMAND, command); command.pluginVersion().ifPresent(version -> handle(new SetVersion(version))); + command.dependenciesVersions().forEach(version -> handle(new SetVersion(version))); PluginManagement pluginManagement = command .buildProfile() @@ -313,6 +314,7 @@ public void handle(AddDirectMavenPlugin command) { Assert.notNull(COMMAND, command); command.pluginVersion().ifPresent(version -> handle(new SetVersion(version))); + command.dependenciesVersions().forEach(version -> handle(new SetVersion(version))); BuildBase projectBuild = command.buildProfile().map(this::findProfile).map(this::profileBuild).orElse(projectBuild()); projectBuild.addPlugin(toMavenPlugin(command)); diff --git a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java index 243aaa88030..67b5f53d6d9 100644 --- a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java +++ b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java @@ -190,7 +190,7 @@ public static JavaDependency dependencyWithVersionAndExclusion() { return javaDependency() .groupId("io.jsonwebtoken") .artifactId("jjwt-jackson") - .versionSlug("jjwt-jackson") + .versionSlug("json-web-token") .addExclusion(DependencyId.of(new GroupId("com.fasterxml.jackson.core"), new ArtifactId("jackson-databind"))) .build(); } @@ -248,7 +248,11 @@ public static JHipsterModuleProperties testModuleProperties() { } public static JavaDependenciesVersions currentJavaDependenciesVersion() { - return new JavaDependenciesVersions(List.of(springBootVersion(), problemVersion(), mavenEnforcerVersion())); + return new JavaDependenciesVersions(List.of(springBootVersion(), problemVersion(), mavenEnforcerVersion(), jsonWebTokenVersion())); + } + + public static JavaDependencyVersion jsonWebTokenVersion() { + return new JavaDependencyVersion("json-web-token", "1.2.3"); } private static JavaDependencyVersion problemVersion() { @@ -323,7 +327,7 @@ public static MavenPlugin mavenEnforcerPluginManagement() { .artifactId("maven-enforcer-plugin") .versionSlug("maven-enforcer-plugin") .addDependency(dependencyWithVersionAndExclusion()) - .addDependency(groupId("io.jsonwebtoken"), artifactId("jjwt-jackson"), versionSlug("jjwt-jackson")) + .addDependency(groupId("io.jsonwebtoken"), artifactId("jjwt-jackson"), versionSlug("json-web-token")) .addExecution(pluginExecution().goals("enforce").id("enforce-versions")) .addExecution( pluginExecution() diff --git a/src/test/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPluginsTest.java b/src/test/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPluginsTest.java index 257b2df06cd..49b7a9854c6 100644 --- a/src/test/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPluginsTest.java +++ b/src/test/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPluginsTest.java @@ -8,6 +8,7 @@ import tech.jhipster.lite.module.domain.javabuild.command.AddDirectMavenPlugin; import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginManagement; import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommands; +import tech.jhipster.lite.module.domain.javadependency.ProjectJavaDependencies; @UnitTest class JHipsterModuleMavenPluginsTest { @@ -18,12 +19,17 @@ void shouldAddNewDependencyManagement() { .builder(emptyModuleBuilder()) .pluginManagement(mavenEnforcerPluginManagement()) .build() - .buildChanges(currentJavaDependenciesVersion()); + .buildChanges(currentJavaDependenciesVersion(), ProjectJavaDependencies.EMPTY); assertThat(changes.get()) .usingRecursiveFieldByFieldElementComparator() .containsExactly( - AddMavenPluginManagement.builder().plugin(mavenEnforcerPluginManagement()).pluginVersion(mavenEnforcerVersion()).build() + AddMavenPluginManagement + .builder() + .plugin(mavenEnforcerPluginManagement()) + .pluginVersion(mavenEnforcerVersion()) + .addDependencyVersion(jsonWebTokenVersion()) + .build() ); } @@ -31,12 +37,19 @@ void shouldAddNewDependencyManagement() { void shouldAddNewDependency() { JavaBuildCommands changes = JHipsterModuleMavenPlugins .builder(emptyModuleBuilder()) - .plugin(mavenEnforcerPlugin()) + .plugin(mavenEnforcerPluginManagement()) .build() - .buildChanges(currentJavaDependenciesVersion()); + .buildChanges(currentJavaDependenciesVersion(), ProjectJavaDependencies.EMPTY); assertThat(changes.get()) .usingRecursiveFieldByFieldElementComparator() - .containsExactly(AddDirectMavenPlugin.builder().plugin(mavenEnforcerPlugin()).build()); + .containsExactly( + AddDirectMavenPlugin + .builder() + .plugin(mavenEnforcerPluginManagement()) + .pluginVersion(mavenEnforcerVersion()) + .addDependencyVersion(jsonWebTokenVersion()) + .build() + ); } } diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java index 6845a05bb9d..d6930b1a3ef 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java @@ -122,6 +122,7 @@ void shouldApplyMavenModule() { """ ) .containing("") + .containing("") .containing( """ @@ -169,7 +170,7 @@ void shouldApplyMavenModule() { io.jsonwebtoken jjwt-jackson - ${jjwt-jackson.version} + ${json-web-token.version} com.fasterxml.jackson.core @@ -241,7 +242,7 @@ void shouldApplyMavenModule() { io.jsonwebtoken jjwt-jackson - ${jjwt-jackson.version} + ${json-web-token.version} com.fasterxml.jackson.core diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java index 0981d281d89..6ad8a6f1596 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/maven/MavenCommandHandlerTest.java @@ -681,6 +681,20 @@ void shouldAddPropertyForPluginVersion() { assertThat(content(pom)).contains("1.1.1"); } + @Test + void shouldAddPropertyForPluginDependencyVersion() { + Path pom = projectWithPom("src/test/resources/projects/empty-maven/pom.xml"); + + AddMavenPluginManagement command = AddMavenPluginManagement + .builder() + .plugin(mavenEnforcerPluginManagement()) + .addDependencyVersion(new JavaDependencyVersion("json-web-token", "1.1.1")) + .build(); + new MavenCommandHandler(Indentation.DEFAULT, pom).handle(command); + + assertThat(content(pom)).contains("1.1.1"); + } + @Test void shouldAddBuildPluginManagementToPomWithoutPluginManagement() { Path pom = projectWithPom("src/test/resources/projects/maven-empty-build/pom.xml"); @@ -758,7 +772,7 @@ private String pluginManagement() { io.jsonwebtoken jjwt-jackson - ${jjwt-jackson.version} + ${json-web-token.version} com.fasterxml.jackson.core @@ -857,7 +871,7 @@ private String plugins() { io.jsonwebtoken jjwt-jackson - ${jjwt-jackson.version} + ${json-web-token.version} com.fasterxml.jackson.core @@ -913,6 +927,20 @@ void shouldAddPropertyForPluginVersion() { assertThat(content(pom)).contains("1.1.1"); } + @Test + void shouldAddPropertyForPluginDependencyVersion() { + Path pom = projectWithPom("src/test/resources/projects/empty-maven/pom.xml"); + + AddDirectMavenPlugin command = AddDirectMavenPlugin + .builder() + .plugin(mavenEnforcerPluginManagement()) + .addDependencyVersion(new JavaDependencyVersion("json-web-token", "1.1.1")) + .build(); + new MavenCommandHandler(Indentation.DEFAULT, pom).handle(command); + + assertThat(content(pom)).contains("1.1.1"); + } + @Test void shouldAddBuildPluginToPomWithEmptyBuild() { Path pom = projectWithPom("src/test/resources/projects/maven-empty-build/pom.xml"); @@ -1003,7 +1031,7 @@ private String plugins() { io.jsonwebtoken jjwt-jackson - ${jjwt-jackson.version} + ${json-web-token.version} com.fasterxml.jackson.core @@ -1103,7 +1131,7 @@ private String plugins() { io.jsonwebtoken jjwt-jackson - ${jjwt-jackson.version} + ${json-web-token.version} com.fasterxml.jackson.core From 7e892e9a898669f4f481e8dc0d70752ca0fa210a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sat, 20 Jan 2024 14:10:01 +0100 Subject: [PATCH 6/9] Reduces duplication between AddDirectMavenPlugin and AddMavenPluginManagement By using the same builder --- .../command/AddDirectMavenPlugin.java | 69 +++---------------- .../command/AddMavenPluginBuilder.java | 65 +++++++++++++++++ .../command/AddMavenPluginManagement.java | 69 +++---------------- .../AddMavenPluginOptionalBuilder.java | 14 ++++ .../command/AddMavenPluginPluginBuilder.java | 7 ++ .../JHipsterModuleMavenPlugins.java | 11 ++- 6 files changed, 109 insertions(+), 126 deletions(-) create mode 100644 src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginBuilder.java create mode 100644 src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginOptionalBuilder.java create mode 100644 src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginPluginBuilder.java diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java index 85a5b774c7d..e8e0cd424b7 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddDirectMavenPlugin.java @@ -1,7 +1,6 @@ package tech.jhipster.lite.module.domain.javabuild.command; import java.util.Collection; -import java.util.HashSet; import java.util.Optional; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion; @@ -15,13 +14,13 @@ public final class AddDirectMavenPlugin implements JavaBuildCommand, AddMavenPlu private final Optional buildProfile; private final Collection dependenciesVersions; - private AddDirectMavenPlugin(AddDirectMavenPluginBuilder builder) { - Assert.notNull("plugin", builder.plugin); - Assert.notNull("dependenciesVersions", builder.dependenciesVersions); - this.plugin = builder.plugin; - this.dependenciesVersions = builder.dependenciesVersions; - this.pluginVersion = Optional.ofNullable(builder.pluginVersion); - this.buildProfile = Optional.ofNullable(builder.buildProfile); + private AddDirectMavenPlugin(AddMavenPluginBuilder builder) { + Assert.notNull("plugin", builder.plugin()); + Assert.notNull("dependenciesVersions", builder.dependenciesVersions()); + this.plugin = builder.plugin(); + this.dependenciesVersions = builder.dependenciesVersions(); + this.pluginVersion = Optional.ofNullable(builder.pluginVersion()); + this.buildProfile = Optional.ofNullable(builder.buildProfile()); } @Override @@ -42,57 +41,7 @@ public Collection dependenciesVersions() { return dependenciesVersions; } - public static AddDirectMavenPluginPluginBuilder builder() { - return new AddDirectMavenPluginBuilder(); - } - - private static class AddDirectMavenPluginBuilder implements AddDirectMavenPluginPluginBuilder, AddDirectMavenPluginOptionalBuilder { - - private MavenPlugin plugin; - private JavaDependencyVersion pluginVersion; - private BuildProfileId buildProfile; - private final Collection dependenciesVersions = new HashSet<>(); - - private AddDirectMavenPluginBuilder() {} - - @Override - public AddDirectMavenPluginBuilder plugin(MavenPlugin plugin) { - this.plugin = plugin; - return this; - } - - @Override - public AddDirectMavenPluginOptionalBuilder pluginVersion(JavaDependencyVersion pluginVersion) { - this.pluginVersion = pluginVersion; - return this; - } - - @Override - public AddDirectMavenPluginOptionalBuilder buildProfile(BuildProfileId buildProfile) { - this.buildProfile = buildProfile; - return this; - } - - @Override - public AddDirectMavenPluginOptionalBuilder addDependencyVersion(JavaDependencyVersion version) { - this.dependenciesVersions.add(version); - return this; - } - - public AddDirectMavenPlugin build() { - return new AddDirectMavenPlugin(this); - } - } - - public interface AddDirectMavenPluginPluginBuilder { - AddDirectMavenPluginOptionalBuilder plugin(MavenPlugin javaBuildPlugin); - } - - public interface AddDirectMavenPluginOptionalBuilder { - AddDirectMavenPluginOptionalBuilder pluginVersion(JavaDependencyVersion version); - AddDirectMavenPluginOptionalBuilder buildProfile(BuildProfileId buildProfile); - AddDirectMavenPluginOptionalBuilder addDependencyVersion(JavaDependencyVersion version); - - AddDirectMavenPlugin build(); + public static AddMavenPluginPluginBuilder builder() { + return new AddMavenPluginBuilder<>(AddDirectMavenPlugin::new); } } diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginBuilder.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginBuilder.java new file mode 100644 index 00000000000..fc01d58a516 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginBuilder.java @@ -0,0 +1,65 @@ +package tech.jhipster.lite.module.domain.javabuild.command; + +import java.util.Collection; +import java.util.HashSet; +import java.util.function.Function; +import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; +import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion; +import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin; + +class AddMavenPluginBuilder implements AddMavenPluginPluginBuilder, AddMavenPluginOptionalBuilder { + + private final Function, T> factory; + private MavenPlugin plugin; + private JavaDependencyVersion pluginVersion; + private BuildProfileId buildProfile; + private final Collection dependenciesVersions = new HashSet<>(); + + AddMavenPluginBuilder(Function, T> factory) { + this.factory = factory; + } + + public MavenPlugin plugin() { + return plugin; + } + + public JavaDependencyVersion pluginVersion() { + return pluginVersion; + } + + public BuildProfileId buildProfile() { + return buildProfile; + } + + public Collection dependenciesVersions() { + return dependenciesVersions; + } + + @Override + public AddMavenPluginBuilder plugin(MavenPlugin plugin) { + this.plugin = plugin; + return this; + } + + @Override + public AddMavenPluginOptionalBuilder pluginVersion(JavaDependencyVersion pluginVersion) { + this.pluginVersion = pluginVersion; + return this; + } + + @Override + public AddMavenPluginOptionalBuilder buildProfile(BuildProfileId buildProfile) { + this.buildProfile = buildProfile; + return this; + } + + @Override + public AddMavenPluginOptionalBuilder addDependencyVersion(JavaDependencyVersion dependencyVersion) { + this.dependenciesVersions.add(dependencyVersion); + return this; + } + + public T build() { + return factory.apply(this); + } +} diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java index cc4e1bd6ecf..1987b7e576b 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginManagement.java @@ -3,7 +3,6 @@ import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; import java.util.Collection; -import java.util.HashSet; import java.util.Optional; import org.apache.commons.lang3.builder.ToStringBuilder; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; @@ -19,13 +18,13 @@ public final class AddMavenPluginManagement implements JavaBuildCommand, AddMave private final Optional buildProfile; private final Collection dependenciesVersions; - private AddMavenPluginManagement(AddMavenPluginManagementBuilder builder) { - Assert.notNull("plugin", builder.plugin); - Assert.notNull("dependenciesVersions", builder.dependenciesVersions); - this.plugin = builder.plugin; - this.dependenciesVersions = builder.dependenciesVersions; - this.pluginVersion = Optional.ofNullable(builder.pluginVersion); - this.buildProfile = Optional.ofNullable(builder.buildProfile); + private AddMavenPluginManagement(AddMavenPluginBuilder builder) { + Assert.notNull("plugin", builder.plugin()); + Assert.notNull("dependenciesVersions", builder.dependenciesVersions()); + this.plugin = builder.plugin(); + this.dependenciesVersions = builder.dependenciesVersions(); + this.pluginVersion = Optional.ofNullable(builder.pluginVersion()); + this.buildProfile = Optional.ofNullable(builder.buildProfile()); } @Override @@ -46,58 +45,8 @@ public Collection dependenciesVersions() { return dependenciesVersions; } - public static AddMavenPluginManagementPluginBuilder builder() { - return new AddMavenPluginManagementBuilder(); - } - - private static class AddMavenPluginManagementBuilder - implements AddMavenPluginManagementPluginBuilder, AddMavenPluginManagementOptionalBuilder { - - private MavenPlugin plugin; - private JavaDependencyVersion pluginVersion; - private BuildProfileId buildProfile; - private final Collection dependenciesVersions = new HashSet<>(); - - private AddMavenPluginManagementBuilder() {} - - @Override - public AddMavenPluginManagementBuilder plugin(MavenPlugin plugin) { - this.plugin = plugin; - return this; - } - - @Override - public AddMavenPluginManagementOptionalBuilder pluginVersion(JavaDependencyVersion pluginVersion) { - this.pluginVersion = pluginVersion; - return this; - } - - @Override - public AddMavenPluginManagementOptionalBuilder buildProfile(BuildProfileId buildProfile) { - this.buildProfile = buildProfile; - return this; - } - - @Override - public AddMavenPluginManagementOptionalBuilder addDependencyVersion(JavaDependencyVersion dependencyVersion) { - this.dependenciesVersions.add(dependencyVersion); - return this; - } - - public AddMavenPluginManagement build() { - return new AddMavenPluginManagement(this); - } - } - - public interface AddMavenPluginManagementPluginBuilder { - AddMavenPluginManagementOptionalBuilder plugin(MavenPlugin plugin); - } - - public interface AddMavenPluginManagementOptionalBuilder { - AddMavenPluginManagementOptionalBuilder pluginVersion(JavaDependencyVersion version); - AddMavenPluginManagementOptionalBuilder buildProfile(BuildProfileId buildProfile); - AddMavenPluginManagementOptionalBuilder addDependencyVersion(JavaDependencyVersion javaDependencyVersion); - AddMavenPluginManagement build(); + public static AddMavenPluginPluginBuilder builder() { + return new AddMavenPluginBuilder<>(AddMavenPluginManagement::new); } @Override diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginOptionalBuilder.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginOptionalBuilder.java new file mode 100644 index 00000000000..e721177a928 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginOptionalBuilder.java @@ -0,0 +1,14 @@ +package tech.jhipster.lite.module.domain.javabuild.command; + +import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; +import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion; + +public interface AddMavenPluginOptionalBuilder { + AddMavenPluginOptionalBuilder pluginVersion(JavaDependencyVersion version); + + AddMavenPluginOptionalBuilder buildProfile(BuildProfileId buildProfile); + + AddMavenPluginOptionalBuilder addDependencyVersion(JavaDependencyVersion javaDependencyVersion); + + T build(); +} diff --git a/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginPluginBuilder.java b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginPluginBuilder.java new file mode 100644 index 00000000000..c66a8f77ad2 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/module/domain/javabuild/command/AddMavenPluginPluginBuilder.java @@ -0,0 +1,7 @@ +package tech.jhipster.lite.module.domain.javabuild.command; + +import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin; + +public interface AddMavenPluginPluginBuilder { + AddMavenPluginOptionalBuilder plugin(MavenPlugin plugin); +} diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java index 4b9c0078c9c..d827d9498bb 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java +++ b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java @@ -6,9 +6,8 @@ import java.util.function.Function; import java.util.stream.Stream; import tech.jhipster.lite.module.domain.javabuild.command.AddDirectMavenPlugin; -import tech.jhipster.lite.module.domain.javabuild.command.AddDirectMavenPlugin.AddDirectMavenPluginOptionalBuilder; import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginManagement; -import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginManagement.AddMavenPluginManagementOptionalBuilder; +import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginOptionalBuilder; import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommand; import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommands; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; @@ -67,7 +66,7 @@ private static Function toAddMavenPluginM Optional buildProfile ) { return plugin -> { - AddMavenPluginManagementOptionalBuilder commandBuilder = AddMavenPluginManagement.builder().plugin(plugin); + var commandBuilder = AddMavenPluginManagement.builder().plugin(plugin); buildProfile.ifPresent(commandBuilder::buildProfile); plugin .dependencies() @@ -81,7 +80,7 @@ private static Function toAddMavenPluginM .versionSlug() .map(versions::get) .map(commandBuilder::pluginVersion) - .map(AddMavenPluginManagementOptionalBuilder::build) + .map(AddMavenPluginOptionalBuilder::build) .orElse(commandBuilder.build()); }; } @@ -92,7 +91,7 @@ private static Function toAddDirectMavenPlugi Optional buildProfile ) { return plugin -> { - AddDirectMavenPluginOptionalBuilder commandBuilder = AddDirectMavenPlugin.builder().plugin(plugin); + var commandBuilder = AddDirectMavenPlugin.builder().plugin(plugin); buildProfile.ifPresent(commandBuilder::buildProfile); plugin .dependencies() @@ -106,7 +105,7 @@ private static Function toAddDirectMavenPlugi .versionSlug() .map(versions::get) .map(commandBuilder::pluginVersion) - .map(AddDirectMavenPluginOptionalBuilder::build) + .map(AddMavenPluginOptionalBuilder::build) .orElse(commandBuilder.build()); }; } From aca60eb3a138e86dd4f01c214e09162b098e5217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sat, 20 Jan 2024 14:19:24 +0100 Subject: [PATCH 7/9] Reduces duplication in creation of AddDirectMavenPlugin and AddMavenPluginManagement commands --- .../JHipsterModuleMavenPlugins.java | 40 +++++-------------- 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java index d827d9498bb..bfcc530344a 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java +++ b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java @@ -4,10 +4,13 @@ import java.util.Collection; import java.util.Optional; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Stream; import tech.jhipster.lite.module.domain.javabuild.command.AddDirectMavenPlugin; +import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPlugin; import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginManagement; import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginOptionalBuilder; +import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginPluginBuilder; import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommand; import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommands; import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; @@ -49,49 +52,26 @@ private JavaBuildCommands buildChanges( Optional buildProfile ) { Assert.notNull("versions", versions); + Assert.notNull("projectJavaDependencies", projectJavaDependencies); Stream managementCommands = pluginsManagement .stream() - .map(toAddMavenPluginManagement(versions, projectJavaDependencies, buildProfile)); + .map(toAddMavenPlugin(versions, projectJavaDependencies, buildProfile, AddMavenPluginManagement::builder)); Stream pluginsCommands = plugins .stream() - .map(toAddDirectMavenPlugin(versions, projectJavaDependencies, buildProfile)); + .map(toAddMavenPlugin(versions, projectJavaDependencies, buildProfile, AddDirectMavenPlugin::builder)); return new JavaBuildCommands(Stream.concat(managementCommands, pluginsCommands).toList()); } - private static Function toAddMavenPluginManagement( + private static Function toAddMavenPlugin( JavaDependenciesVersions versions, ProjectJavaDependencies projectDependencies, - Optional buildProfile - ) { - return plugin -> { - var commandBuilder = AddMavenPluginManagement.builder().plugin(plugin); - buildProfile.ifPresent(commandBuilder::buildProfile); - plugin - .dependencies() - .stream() - .map(JavaDependency::version) - .flatMap(Optional::stream) - .map(JavaDependency.toVersion(versions, projectDependencies)) - .flatMap(Optional::stream) - .forEach(commandBuilder::addDependencyVersion); - return plugin - .versionSlug() - .map(versions::get) - .map(commandBuilder::pluginVersion) - .map(AddMavenPluginOptionalBuilder::build) - .orElse(commandBuilder.build()); - }; - } - - private static Function toAddDirectMavenPlugin( - JavaDependenciesVersions versions, - ProjectJavaDependencies projectDependencies, - Optional buildProfile + Optional buildProfile, + Supplier> builderFactory ) { return plugin -> { - var commandBuilder = AddDirectMavenPlugin.builder().plugin(plugin); + AddMavenPluginOptionalBuilder commandBuilder = builderFactory.get().plugin(plugin); buildProfile.ifPresent(commandBuilder::buildProfile); plugin .dependencies() From 3a7d814fe443a2487ebcb856ba9835b447734476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sat, 20 Jan 2024 14:38:27 +0100 Subject: [PATCH 8/9] Simplify code of retrieval of dependencies versions for maven plugin --- .../mavenplugin/JHipsterModuleMavenPlugins.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java index bfcc530344a..e32f2bc6f81 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java +++ b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java @@ -16,6 +16,7 @@ import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId; import tech.jhipster.lite.module.domain.javadependency.JavaDependenciesVersions; import tech.jhipster.lite.module.domain.javadependency.JavaDependency; +import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion; import tech.jhipster.lite.module.domain.javadependency.ProjectJavaDependencies; import tech.jhipster.lite.shared.error.domain.Assert; @@ -76,10 +77,7 @@ private static Function toAddMavenPlu plugin .dependencies() .stream() - .map(JavaDependency::version) - .flatMap(Optional::stream) - .map(JavaDependency.toVersion(versions, projectDependencies)) - .flatMap(Optional::stream) + .flatMap(toDependencyVersion(versions, projectDependencies)) .forEach(commandBuilder::addDependencyVersion); return plugin .versionSlug() @@ -90,6 +88,13 @@ private static Function toAddMavenPlu }; } + private static Function> toDependencyVersion( + JavaDependenciesVersions versions, + ProjectJavaDependencies projectDependencies + ) { + return dependency -> dependency.version().flatMap(JavaDependency.toVersion(versions, projectDependencies)).stream(); + } + public static class JHipsterModuleMavenPluginsBuilder { private final T parentModuleBuilder; From 6730f26ba27c13aa127e923ab74f4b80c8a94d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sat, 20 Jan 2024 14:38:49 +0100 Subject: [PATCH 9/9] Simplify code --- .../domain/mavenplugin/JHipsterModuleMavenPlugins.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java index e32f2bc6f81..5c50830d4ad 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java +++ b/src/main/java/tech/jhipster/lite/module/domain/mavenplugin/JHipsterModuleMavenPlugins.java @@ -79,12 +79,8 @@ private static Function toAddMavenPlu .stream() .flatMap(toDependencyVersion(versions, projectDependencies)) .forEach(commandBuilder::addDependencyVersion); - return plugin - .versionSlug() - .map(versions::get) - .map(commandBuilder::pluginVersion) - .map(AddMavenPluginOptionalBuilder::build) - .orElse(commandBuilder.build()); + plugin.versionSlug().map(versions::get).ifPresent(commandBuilder::pluginVersion); + return commandBuilder.build(); }; }