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

Fix missing version in extension #442

Merged
merged 3 commits into from
Jan 8, 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
27 changes: 27 additions & 0 deletions maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-development-mode</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.10.0</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-classworlds</artifactId>
</exclusion>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- user prompt -->
<dependency>
Expand Down
23 changes: 20 additions & 3 deletions maven/src/main/java/org/jboss/shamrock/maven/AddExtensionMojo.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.jboss.shamrock.maven;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.jboss.shamrock.maven.components.dependencies.Extensions;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import static org.jboss.shamrock.maven.components.dependencies.Extensions.addExtensions;

@Mojo(name = "add-extension", requiresProject = true)
public class AddExtensionMojo extends AbstractMojo {

Expand All @@ -27,11 +29,26 @@ public class AddExtensionMojo extends AbstractMojo {
@Parameter(property = "extensions")
private List<String> extensions;

/**
* Remote repositories used for the project.
*/
@Parameter(defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true)
private List<ArtifactRepository> repositories;

/**
* The current build session instance. This is used for
* plugin manager API calls.
*/
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

@Component
private Extensions ext;

@Override
public void execute() throws MojoExecutionException {
Model model = project.getOriginalModel().clone();
if (addExtensions(model, extensions, getLog())) {
if (ext.addExtensions(model, extensions, session, repositories, getLog())) {
File pomFile = project.getFile();
save(pomFile, model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.jboss.shamrock.maven;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.*;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
Expand All @@ -29,6 +31,7 @@
import org.fusesource.jansi.Ansi;
import org.jboss.shamrock.maven.components.Prompter;
import org.jboss.shamrock.maven.components.SetupTemplates;
import org.jboss.shamrock.maven.components.dependencies.Extensions;
import org.jboss.shamrock.maven.utilities.MojoUtils;

import java.io.File;
Expand All @@ -38,7 +41,6 @@
import java.util.*;

import static org.fusesource.jansi.Ansi.ansi;
import static org.jboss.shamrock.maven.components.dependencies.Extensions.addExtensions;
import static org.jboss.shamrock.maven.utilities.MojoUtils.configuration;
import static org.jboss.shamrock.maven.utilities.MojoUtils.plugin;

Expand Down Expand Up @@ -92,6 +94,21 @@ public class CreateProjectMojo extends AbstractMojo {
@Component
private SetupTemplates templates;

@Component
private Extensions ext;

/**
* Remote repositories used for the project.
*/
@Parameter(defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true)
private List<ArtifactRepository> repositories;

/**
* The current build session instance. This is used for plugin manager API calls.
*/
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

@Override
public void execute() throws MojoExecutionException {
File pomFile = project.getFile();
Expand All @@ -117,7 +134,7 @@ public void execute() throws MojoExecutionException {
addVersionProperty(model);
addBom(model);
addMainPluginConfig(model);
addExtensions(model, extensions, getLog());
ext.addExtensions(model, extensions, session, repositories, getLog());
addNativeProfile(model);
save(pomFile, model);
}
Expand Down Expand Up @@ -301,11 +318,12 @@ private File createPomFileFromUserInputs() throws MojoExecutionException {
project.setPomFile(pomFile);
project.setOriginalModel(model); // the current model is the original model as well

addExtensions(model, extensions, getLog());
ext.addExtensions(model, extensions, session, repositories, getLog());
save(pomFile, model);
return pomFile;
}


private void save(File pomFile, Model model) throws MojoExecutionException {
MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
try (FileWriter pomFileWriter = new FileWriter(pomFile)) {
Expand Down Expand Up @@ -341,4 +359,5 @@ private boolean isParentPom(Model model) {
return "pom".equals(model.getPackaging());
}


}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package org.jboss.shamrock.maven;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.jboss.shamrock.maven.components.dependencies.Extensions;

@Mojo(name = "list-extensions", requiresProject = false)
public class ListExtensionsMojo extends AbstractMojo {

@Component
Extensions extensions;

@Override
public void execute() {
getLog().info("Available extensions:");
Extensions.get().stream()
extensions.get().stream()
.sorted((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()))
.forEach(ext -> getLog().info("\t * " + ext.getName() + " (" + ext.getGroupId() + ":" + ext.getArtifactId() + ")"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,52 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.jboss.shamrock.maven.utilities.MojoUtils;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Optional;

import static org.jboss.shamrock.maven.CreateProjectMojo.PLUGIN_GROUPID;

/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
@Component(role = Extensions.class, instantiationStrategy = "singleton")
public class Extensions {

private Extensions() {
// avoid direct instantiation
}
/**
* Maven Project Builder component.
*/
@Requirement
protected ProjectBuilder projectBuilder;

/**
* Component used to resolve artifacts and download their files from remote repositories.
*/
@Requirement
protected ArtifactResolver artifactResolver;

public static List<Extension> get() {

public List<Extension> get() {
ObjectMapper mapper = new ObjectMapper()
.enable(JsonParser.Feature.ALLOW_COMMENTS)
.enable(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS);
Expand All @@ -52,7 +78,7 @@ public static List<Extension> get() {
}
}

public static Dependency parse(String dependency, Log log) {
public Dependency parse(String dependency, Log log) {
Dependency res = new Dependency();
String[] segments = dependency.split(":");
if (segments.length >= 2) {
Expand All @@ -71,13 +97,22 @@ public static Dependency parse(String dependency, Log log) {
}
}

public static boolean addExtensions(Model model, List<String> extensions, Log log) {
private List<Dependency> getDependenciesFromBom(MavenSession session, List<ArtifactRepository> repositories) throws MojoExecutionException {
String bomCoordinates = PLUGIN_GROUPID + ":" + MojoUtils.get("bom-artifactId") + ":"
+ MojoUtils.get("shamrock-version");
MavenProject bom = getMavenProject(bomCoordinates, session, repositories);
return bom.getDependencyManagement().getDependencies();
}

public boolean addExtensions(Model model, List<String> extensions,
MavenSession session, List<ArtifactRepository> repositories, Log log) throws MojoExecutionException {
if (extensions == null || extensions.isEmpty()) {
return false;
}

boolean updated = false;
List<Extension> exts = Extensions.get();
List<Extension> exts = get();
List<Dependency> dependenciesFromBom = getDependenciesFromBom(session, repositories);
for (String dependency : extensions) {
Optional<Extension> optional = exts.stream()
.filter(d -> {
Expand All @@ -91,7 +126,7 @@ public static boolean addExtensions(Model model, List<String> extensions, Log lo
if (!MojoUtils.hasDependency(model, optional.get().getGroupId(), optional.get().getArtifactId())) {
log.info("Adding extension " + optional.get().toCoordinates());

if (containsBOM(model)) {
if (containsBOM(model) && isDefinedInBom(dependenciesFromBom, optional.get())) {
model.addDependency(optional.get().toDependency(true));
} else {
model.addDependency(optional.get().toDependency(false));
Expand All @@ -105,7 +140,7 @@ public static boolean addExtensions(Model model, List<String> extensions, Log lo
} else if (dependency.contains(":")) {
// Add it as a dependency
// groupId:artifactId:version:classifier
Dependency parsed = Extensions.parse(dependency, log);
Dependency parsed = parse(dependency, log);
if (parsed != null) {
log.info("Adding dependency " + parsed.getManagementKey());
model.addDependency(parsed);
Expand All @@ -119,7 +154,13 @@ public static boolean addExtensions(Model model, List<String> extensions, Log lo
return updated;
}

private static boolean containsBOM(Model model) {
private boolean isDefinedInBom(List<Dependency> dependencies, Extension extension) {
return dependencies.stream().anyMatch(dependency ->
dependency.getGroupId().equalsIgnoreCase(extension.getGroupId())
&& dependency.getArtifactId().equalsIgnoreCase(extension.getArtifactId()));
}

private boolean containsBOM(Model model) {
List<Dependency> dependencies = model.getDependencyManagement().getDependencies();
return dependencies.stream()
// Find bom
Expand All @@ -129,4 +170,74 @@ private static boolean containsBOM(Model model) {
.anyMatch(dependency -> dependency.getArtifactId().equalsIgnoreCase(MojoUtils.get("bom-artifactId")));
}

/**
* Retrieves the Maven Project associated with the given artifact String, in the form of
* <code>groupId:artifactId[:version]</code>. This resolves the POM artifact at those coordinates and then builds
* the Maven project from it.
*
* @param artifactString Coordinates of the Maven project to get.
* @param session the maven session
* @param repositories the repositories
* @return New Maven project.
* @throws MojoExecutionException If there was an error while getting the Maven project.
*/
protected MavenProject getMavenProject(String artifactString, MavenSession session, List<ArtifactRepository> repositories)
throws MojoExecutionException {
ArtifactCoordinate coordinate = getArtifactCoordinate(artifactString, "pom");
try {
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setRemoteRepositories(repositories);
pbr.setProject(null);
pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
pbr.setResolveDependencies(true);
Artifact artifact = artifactResolver.resolveArtifact(pbr, coordinate).getArtifact();
return projectBuilder.build(artifact.getFile(), pbr).getProject();
} catch (Exception e) {
throw new MojoExecutionException("Unable to get the POM for the artifact '" + artifactString
+ "'. Verify the artifact parameter.", e);
}
}

/**
* Parses the given String into GAV artifact coordinate information, adding the given type.
*
* @param artifactString should respect the format <code>groupId:artifactId[:version]</code>
* @param type The extension for the artifact, must not be <code>null</code>.
* @return the <code>Artifact</code> object for the <code>artifactString</code> parameter.
* @throws MojoExecutionException if the <code>artifactString</code> doesn't respect the format.
*/
protected ArtifactCoordinate getArtifactCoordinate(String artifactString, String type)
throws MojoExecutionException {
String groupId; // required
String artifactId; // required
String version; // optional

String[] artifactParts = artifactString.split(":");
switch (artifactParts.length) {
case 2:
groupId = artifactParts[0];
artifactId = artifactParts[1];
version = Artifact.LATEST_VERSION;
break;
case 3:
groupId = artifactParts[0];
artifactId = artifactParts[1];
version = artifactParts[2];
break;
default:
throw new MojoExecutionException("The artifact parameter '" + artifactString
+ "' should be conform to: " + "'groupId:artifactId[:version]'.");
}
return getArtifactCoordinate(groupId, artifactId, version, type);
}

protected ArtifactCoordinate getArtifactCoordinate(String groupId, String artifactId, String version, String type) {
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(groupId);
coordinate.setArtifactId(artifactId);
coordinate.setVersion(version);
coordinate.setExtension(type);
return coordinate;
}

}