From 4d060c38e4c44fb71d2493075983aeba401927bd Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 7 Feb 2019 11:37:18 +0100 Subject: [PATCH 1/2] Sanitize the extensions input and make it a Set --- .../cli/commands/AddExtensionCommand.java | 12 ++-- .../shamrock/cli/commands/AddExtensions.java | 22 +++---- .../cli/commands/AddExtensionsTest.java | 13 +++-- .../cli/commands/ListExtensionsTest.java | 57 +++++++++++++++---- .../shamrock/maven/AddExtensionMojo.java | 12 ++-- .../shamrock/maven/CreateProjectMojo.java | 21 ++++--- 6 files changed, 89 insertions(+), 48 deletions(-) diff --git a/cli/aesh/src/main/java/org/jboss/shamrock/cli/commands/AddExtensionCommand.java b/cli/aesh/src/main/java/org/jboss/shamrock/cli/commands/AddExtensionCommand.java index 84a91d1ea84bc..435e2a409e3c2 100644 --- a/cli/aesh/src/main/java/org/jboss/shamrock/cli/commands/AddExtensionCommand.java +++ b/cli/aesh/src/main/java/org/jboss/shamrock/cli/commands/AddExtensionCommand.java @@ -1,5 +1,9 @@ package org.jboss.shamrock.cli.commands; +import java.io.File; +import java.io.IOException; +import java.util.Collections; + import org.aesh.command.Command; import org.aesh.command.CommandDefinition; import org.aesh.command.CommandException; @@ -10,11 +14,6 @@ import org.aesh.io.Resource; import org.jboss.shamrock.dependencies.Extension; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; - /** * @author Ståle Pedersen */ @@ -30,6 +29,7 @@ public class AddExtensionCommand implements Command{ @Argument(required = true, description = "Path to the project pom the extension will be added") private Resource pom; + @Override public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { if(help) { commandInvocation.println(commandInvocation.getHelpInfo("protean add-extension")); @@ -43,7 +43,7 @@ public CommandResult execute(CommandInvocation commandInvocation) throws Command else if (pom.isLeaf()){ try { AddExtensions project = new AddExtensions(new File(pom.getAbsolutePath())); - project.addExtensions(Arrays.asList(extension)); + project.addExtensions(Collections.singleton(extension)); } catch(IOException e) { e.printStackTrace(); diff --git a/cli/common/src/main/java/org/jboss/shamrock/cli/commands/AddExtensions.java b/cli/common/src/main/java/org/jboss/shamrock/cli/commands/AddExtensions.java index bcbbe76a878b3..daec7669a1901 100644 --- a/cli/common/src/main/java/org/jboss/shamrock/cli/commands/AddExtensions.java +++ b/cli/common/src/main/java/org/jboss/shamrock/cli/commands/AddExtensions.java @@ -1,21 +1,23 @@ package org.jboss.shamrock.cli.commands; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.Model; -import org.jboss.shamrock.dependencies.Extension; -import org.jboss.shamrock.maven.utilities.MojoUtils; +import static org.jboss.shamrock.maven.utilities.MojoUtils.getBomArtifactId; +import static org.jboss.shamrock.maven.utilities.MojoUtils.readPom; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.List; import java.util.Optional; +import java.util.Set; -import static org.jboss.shamrock.maven.utilities.MojoUtils.getBomArtifactId; -import static org.jboss.shamrock.maven.utilities.MojoUtils.readPom; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Model; +import org.jboss.shamrock.dependencies.Extension; +import org.jboss.shamrock.maven.utilities.MojoUtils; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; public class AddExtensions { private Model model; @@ -40,7 +42,7 @@ public static List get() { } } - public boolean addExtensions(final List extensions) throws IOException { + public boolean addExtensions(final Set extensions) throws IOException { if (extensions == null || extensions.isEmpty()) { return false; } diff --git a/cli/common/src/test/java/org/jboss/shamrock/cli/commands/AddExtensionsTest.java b/cli/common/src/test/java/org/jboss/shamrock/cli/commands/AddExtensionsTest.java index f17f06e4a5a44..ea75c769f942f 100644 --- a/cli/common/src/test/java/org/jboss/shamrock/cli/commands/AddExtensionsTest.java +++ b/cli/common/src/test/java/org/jboss/shamrock/cli/commands/AddExtensionsTest.java @@ -1,15 +1,16 @@ package org.jboss.shamrock.cli.commands; -import org.apache.maven.model.Model; -import org.jboss.shamrock.maven.utilities.MojoUtils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import static java.util.Arrays.asList; import java.io.File; import java.io.IOException; import java.util.HashMap; +import java.util.HashSet; -import static java.util.Arrays.asList; +import org.apache.maven.model.Model; +import org.jboss.shamrock.maven.utilities.MojoUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class AddExtensionsTest { @Test @@ -24,7 +25,7 @@ public void addExtension() throws IOException { .doCreateProject(new HashMap<>()); new AddExtensions(pom) - .addExtensions(asList("agroal", "arc", "hibernate-validator")); + .addExtensions(new HashSet<>(asList("agroal", "arc", " hibernate-validator"))); Model model = MojoUtils.readPom(pom); hasDependency(model, "shamrock-agroal-deployment"); diff --git a/cli/common/src/test/java/org/jboss/shamrock/cli/commands/ListExtensionsTest.java b/cli/common/src/test/java/org/jboss/shamrock/cli/commands/ListExtensionsTest.java index 475e5f3b5e959..7cff223d691c8 100644 --- a/cli/common/src/test/java/org/jboss/shamrock/cli/commands/ListExtensionsTest.java +++ b/cli/common/src/test/java/org/jboss/shamrock/cli/commands/ListExtensionsTest.java @@ -1,22 +1,25 @@ package org.jboss.shamrock.cli.commands; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.Model; -import org.jboss.shamrock.maven.utilities.MojoUtils; -import org.jboss.shamrock.maven.utilities.ShamrockDependencyPredicate; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import static java.util.Arrays.asList; +import static org.jboss.shamrock.maven.utilities.MojoUtils.getPluginGroupId; +import static org.jboss.shamrock.maven.utilities.MojoUtils.getPluginVersion; +import static org.jboss.shamrock.maven.utilities.MojoUtils.readPom; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; -import static java.util.Arrays.asList; -import static org.jboss.shamrock.maven.utilities.MojoUtils.*; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Model; +import org.jboss.shamrock.maven.utilities.MojoUtils; +import org.jboss.shamrock.maven.utilities.ShamrockDependencyPredicate; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class ListExtensionsTest { @@ -34,7 +37,7 @@ public void listWithBom() throws IOException { .doCreateProject(context); new AddExtensions(pom) - .addExtensions(asList("commons-io:commons-io:2.5", "Agroal")); + .addExtensions(new HashSet<>(asList("commons-io:commons-io:2.5", "Agroal"))); Model model = readPom(pom); @@ -45,6 +48,38 @@ public void listWithBom() throws IOException { Assertions.assertNotNull(installed.get(getPluginGroupId() + ":shamrock-agroal-deployment")); } + /** + * When creating a project with Maven, you could have -Dextensions="jaxrs, hibernate-validator". + *

+ * Having a space is not automatically handled by the Maven converter injecting the properties + * so we added code for that and we need to test it. + */ + @Test + public void listWithBomExtensionWithSpaces() throws IOException { + final File pom = new File("target/list-extensions-test", "pom.xml"); + + CreateProjectTest.delete(pom.getParentFile()); + final HashMap context = new HashMap<>(); + + new CreateProject(pom.getParentFile()) + .groupId(getPluginGroupId()) + .artifactId("add-extension-test") + .version("0.0.1-SNAPSHOT") + .doCreateProject(context); + + new AddExtensions(pom) + .addExtensions(new HashSet<>(asList("jaxrs", " hibernate-validator "))); + + Model model = readPom(pom); + + final ListExtensions listExtensions = new ListExtensions(model); + + final Map installed = listExtensions.findInstalled(); + + Assertions.assertNotNull(installed.get(getPluginGroupId() + ":shamrock-jaxrs-deployment")); + Assertions.assertNotNull(installed.get(getPluginGroupId() + ":shamrock-hibernate-validator-deployment")); + } + @Test public void listWithoutBom() throws IOException { final File pom = new File("target/list-extensions-test", "pom.xml"); @@ -68,7 +103,7 @@ public void listWithoutBom() throws IOException { MojoUtils.write(model, pom); new AddExtensions(pom) - .addExtensions(asList("commons-io:commons-io:2.5", "Agroal")); + .addExtensions(new HashSet<>(asList("commons-io:commons-io:2.5", "Agroal"))); model = readPom(pom); diff --git a/cli/maven/src/main/java/org/jboss/shamrock/maven/AddExtensionMojo.java b/cli/maven/src/main/java/org/jboss/shamrock/maven/AddExtensionMojo.java index 958cd1ea2e189..789a6b1262081 100644 --- a/cli/maven/src/main/java/org/jboss/shamrock/maven/AddExtensionMojo.java +++ b/cli/maven/src/main/java/org/jboss/shamrock/maven/AddExtensionMojo.java @@ -1,5 +1,9 @@ package org.jboss.shamrock.maven; +import java.io.IOException; +import java.util.Set; +import java.util.stream.Collectors; + import org.apache.maven.model.Model; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -7,10 +11,6 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.jboss.shamrock.cli.commands.AddExtensions; -import org.jboss.shamrock.maven.utilities.MojoUtils; - -import java.io.IOException; -import java.util.List; @Mojo(name = "add-extension") public class AddExtensionMojo extends AbstractMojo { @@ -22,7 +22,7 @@ public class AddExtensionMojo extends AbstractMojo { protected MavenProject project; @Parameter(property = "extensions") - private List extensions; + private Set extensions; @Override public void execute() throws MojoExecutionException { @@ -30,7 +30,7 @@ public void execute() throws MojoExecutionException { Model model = project.getOriginalModel().clone(); new AddExtensions(model.getPomFile()) - .addExtensions(extensions); + .addExtensions(extensions.stream().map(String::trim).collect(Collectors.toSet())); } catch (IOException e) { throw new MojoExecutionException("Unable to update the pom.xml file", e); } diff --git a/cli/maven/src/main/java/org/jboss/shamrock/maven/CreateProjectMojo.java b/cli/maven/src/main/java/org/jboss/shamrock/maven/CreateProjectMojo.java index c71e5beb6f253..1d545d6524bb3 100644 --- a/cli/maven/src/main/java/org/jboss/shamrock/maven/CreateProjectMojo.java +++ b/cli/maven/src/main/java/org/jboss/shamrock/maven/CreateProjectMojo.java @@ -17,6 +17,15 @@ package org.jboss.shamrock.maven; +import static org.fusesource.jansi.Ansi.ansi; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + import org.apache.commons.lang3.StringUtils; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; @@ -31,14 +40,6 @@ import org.jboss.shamrock.maven.components.Prompter; import org.jboss.shamrock.maven.utilities.MojoUtils; -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.fusesource.jansi.Ansi.ansi; - /** * This goal helps in setting up Shamrock Maven project with shamrock-maven-plugin, with sensible defaults */ @@ -68,7 +69,7 @@ public class CreateProjectMojo extends AbstractMojo { private String className; @Parameter(property = "extensions") - private List extensions; + private Set extensions; @Parameter(defaultValue = "${session}") private MavenSession session; @@ -223,6 +224,8 @@ private void sanitizeOptions() { path = "/" + path; } } + + extensions = extensions.stream().map(String::trim).collect(Collectors.toSet()); } private void printUserInstructions(File root) { From 8df4895f0a3001430c5cdecfc28630d9fad44e83 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 7 Feb 2019 14:08:14 +0100 Subject: [PATCH 2/2] Remove unused import from the Maven plugin --- .../org/jboss/shamrock/maven/BuildMojo.java | 2 +- .../org/jboss/shamrock/maven/DevMojo.java | 1 - .../maven/it/assertions/SetupVerifier.java | 19 ++++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cli/maven/src/main/java/org/jboss/shamrock/maven/BuildMojo.java b/cli/maven/src/main/java/org/jboss/shamrock/maven/BuildMojo.java index c0d7eb51d9699..10497c0007778 100644 --- a/cli/maven/src/main/java/org/jboss/shamrock/maven/BuildMojo.java +++ b/cli/maven/src/main/java/org/jboss/shamrock/maven/BuildMojo.java @@ -19,9 +19,9 @@ import java.io.File; import java.util.List; + import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; diff --git a/cli/maven/src/main/java/org/jboss/shamrock/maven/DevMojo.java b/cli/maven/src/main/java/org/jboss/shamrock/maven/DevMojo.java index ade164e7e648b..c8a38fc009727 100644 --- a/cli/maven/src/main/java/org/jboss/shamrock/maven/DevMojo.java +++ b/cli/maven/src/main/java/org/jboss/shamrock/maven/DevMojo.java @@ -17,7 +17,6 @@ package org.jboss.shamrock.maven; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetAddress; diff --git a/cli/maven/src/test/java/org/jboss/shamrock/maven/it/assertions/SetupVerifier.java b/cli/maven/src/test/java/org/jboss/shamrock/maven/it/assertions/SetupVerifier.java index ffd82baece78f..bd809410dde47 100644 --- a/cli/maven/src/test/java/org/jboss/shamrock/maven/it/assertions/SetupVerifier.java +++ b/cli/maven/src/test/java/org/jboss/shamrock/maven/it/assertions/SetupVerifier.java @@ -1,23 +1,24 @@ package org.jboss.shamrock.maven.it.assertions; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Optional; +import java.util.Properties; + import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.apache.maven.model.Profile; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.jboss.shamrock.maven.CreateProjectMojo; import org.jboss.shamrock.maven.utilities.MojoUtils; -import java.io.File; -import java.io.FileInputStream; -import java.util.Optional; -import java.util.Properties; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; - public class SetupVerifier { public static void assertThatJarExists(File archive) throws Exception {