Skip to content

Commit

Permalink
Platform JSON: support reading BOM's managed deps from a generated POM
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky authored and gsmet committed Aug 30, 2020
1 parent af6dd4b commit 6c48842
Showing 1 changed file with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;

import org.apache.maven.model.Model;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -56,6 +57,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.bootstrap.resolver.maven.workspace.ModelUtils;
import io.quarkus.bootstrap.util.ZipUtils;
import io.quarkus.dependencies.Extension;
import io.quarkus.platform.tools.ToolsConstants;
Expand Down Expand Up @@ -109,22 +111,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
final DefaultArtifact bomArtifact = new DefaultArtifact(bomGroupId, bomArtifactId, "", "pom", bomVersion);
info("Generating catalog of extensions for %s", bomArtifact);

// And get all its dependencies (which are extensions)
final List<Dependency> deps;
try {
deps = repoSystem
.readArtifactDescriptor(repoSession,
new ArtifactDescriptorRequest().setRepositories(repos).setArtifact(bomArtifact))
.getManagedDependencies();
} catch (ArtifactDescriptorException e) {
throw new MojoExecutionException("Failed to read descriptor of " + bomArtifact, e);
// if the BOM is generated and has replaced the original one, to pick up the generated content
// we are first trying to read the dependencies from the project's POM file
List<Dependency> deps = readManagedDepsFromPom();
if (deps == null) {
deps = resolveManagedDeps(bomArtifact);
}
if (deps.isEmpty()) {
getLog().warn("BOM " + bomArtifact + " does not include any dependency");
return;
}

List<OverrideInfo> allOverrides = new ArrayList();
List<OverrideInfo> allOverrides = new ArrayList<>();
for (String path : overridesFile.split(",")) {
OverrideInfo overrideInfo = getOverrideInfo(new File(path.trim()));
if (overrideInfo != null) {
Expand Down Expand Up @@ -221,6 +219,57 @@ public void execute() throws MojoExecutionException, MojoFailureException {
projectHelper.attachArtifact(project, "json", null, outputFile);
}

private List<Dependency> resolveManagedDeps(Artifact bomArtifact) throws MojoExecutionException {
try {
return repoSystem
.readArtifactDescriptor(repoSession,
new ArtifactDescriptorRequest().setRepositories(repos).setArtifact(bomArtifact))
.getManagedDependencies();
} catch (ArtifactDescriptorException e) {
throw new MojoExecutionException("Failed to read descriptor of " + bomArtifact, e);
}
}

private List<Dependency> readManagedDepsFromPom() throws MojoExecutionException {
// if the configured BOM coordinates are not matching the current project
// the current project's POM isn't the right source
if (!project.getArtifact().getArtifactId().equals(bomArtifactId)
|| !project.getArtifact().getVersion().equals(bomVersion)
|| !project.getArtifact().getGroupId().equals(bomGroupId)
|| !project.getFile().exists()) {
return null;
}

final Model bomModel;
try {
bomModel = ModelUtils.readModel(project.getFile().toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to parse " + project.getFile(), e);
}

// if the POM has a parent then we better resolve the descriptor
if (bomModel.getParent() != null) {
return null;
}

if (bomModel.getDependencyManagement() == null) {
return Collections.emptyList();
}
final List<org.apache.maven.model.Dependency> modelDeps = bomModel.getDependencyManagement().getDependencies();
if (modelDeps.isEmpty()) {
return Collections.emptyList();
}

final List<Dependency> deps = new ArrayList<>(modelDeps.size());
for (org.apache.maven.model.Dependency modelDep : modelDeps) {
final Artifact artifact = new DefaultArtifact(modelDep.getGroupId(), modelDep.getArtifactId(),
modelDep.getClassifier(), modelDep.getType(), modelDep.getVersion());
// exclusions aren't relevant in this context
deps.add(new Dependency(artifact, modelDep.getScope(), modelDep.isOptional(), Collections.emptyList()));
}
return deps;
}

private JsonObject processDependency(Artifact artifact) throws IOException {
JsonNode onode = processDependencyToObjectNode(artifact);

Expand Down

0 comments on commit 6c48842

Please sign in to comment.