Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor cleanup of Maven plugin #792

Merged
merged 2 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <a href="mailto:stalep@gmail.com">Ståle Pedersen</a>
*/
Expand All @@ -30,6 +29,7 @@ public class AddExtensionCommand implements Command<CommandInvocation>{
@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"));
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -40,7 +42,7 @@ public static List<Extension> get() {
}
}

public boolean addExtensions(final List<String> extensions) throws IOException {
public boolean addExtensions(final Set<String> extensions) throws IOException {
if (extensions == null || extensions.isEmpty()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);

Expand All @@ -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".
* <p>
* 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<String, Object> 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<String, Dependency> 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");
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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;
import org.apache.maven.plugins.annotations.Mojo;
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 {
Expand All @@ -22,15 +22,15 @@ public class AddExtensionMojo extends AbstractMojo {
protected MavenProject project;

@Parameter(property = "extensions")
private List<String> extensions;
private Set<String> extensions;

@Override
public void execute() throws MojoExecutionException {
try {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -68,7 +69,7 @@ public class CreateProjectMojo extends AbstractMojo {
private String className;

@Parameter(property = "extensions")
private List<String> extensions;
private Set<String> extensions;

@Parameter(defaultValue = "${session}")
private MavenSession session;
Expand Down Expand Up @@ -223,6 +224,8 @@ private void sanitizeOptions() {
path = "/" + path;
}
}

extensions = extensions.stream().map(String::trim).collect(Collectors.toSet());
}

private void printUserInstructions(File root) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down