Skip to content

Commit

Permalink
Handle loaded/notloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 22, 2024
1 parent 522045c commit 8267512
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.plugin.gav;

import eu.maveniverse.maven.toolbox.plugin.GavMojoSupport;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.util.stream.Collectors;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.artifact.DefaultArtifact;

/**
* Resolves given artifact.
*/
@Mojo(name = "gav-resolve", requiresProject = false, threadSafe = true)
public class GavResolveMojo extends GavMojoSupport {
/**
* The comma separated artifact coordinates in the format
* {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>} to resolve.
*/
@Parameter(property = "gav", required = true)
private String gav;

/**
* Apply BOMs, if needed. Comma separated GAVs.
*/
@Parameter(property = "boms")
private String boms;

@Parameter(property = "sources", defaultValue = "false")
private boolean sources;

@Parameter(property = "javadoc", defaultValue = "false")
private boolean javadoc;

@Parameter(property = "signature", defaultValue = "false")
private boolean signature;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.resolve(
csv(gav).stream().map(DefaultArtifact::new).collect(Collectors.toList()),
sources,
javadoc,
signature,
output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.plugin.gav;

import eu.maveniverse.maven.toolbox.plugin.GavMojoSupport;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Resolves transitively given artifact.
*/
@Mojo(name = "gav-resolve-transitive", requiresProject = false, threadSafe = true)
public class GavResolveTransitiveMojo extends GavMojoSupport {
/**
* The resolution scope to resolve, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

/**
* The comma separated artifact coordinates in the format
* {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>} to resolve.
*/
@Parameter(property = "gav", required = true)
private String gav;

/**
* Apply BOMs, if needed. Comma separated GAVs.
*/
@Parameter(property = "boms")
private String boms;

@Parameter(property = "sources", defaultValue = "false")
private boolean sources;

@Parameter(property = "javadoc", defaultValue = "false")
private boolean javadoc;

@Parameter(property = "signature", defaultValue = "false")
private boolean signature;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.resolveTransitive(
ResolutionScope.parse(scope),
toolboxCommando.loadGavs(csv(gav), csv(boms)),
sources,
javadoc,
signature,
output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.plugin.mp;

import eu.maveniverse.maven.toolbox.plugin.MPMojoSupport;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.util.Collections;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Resolves transitively given project.
*/
@Mojo(name = "resolve-transitive", requiresProject = false, threadSafe = true)
public class ResolveTransitiveMojo extends MPMojoSupport {
/**
* The resolution scope to resolve, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

@Parameter(property = "sources", defaultValue = "false")
private boolean sources;

@Parameter(property = "javadoc", defaultValue = "false")
private boolean javadoc;

@Parameter(property = "signature", defaultValue = "false")
private boolean signature;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.resolveTransitive(
ResolutionScope.parse(scope),
Collections.singleton(projectAsResolutionRoot()),
sources,
javadoc,
signature,
output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
public final class ResolutionRoot {
private final Artifact artifact;
private final boolean load;
private final boolean prepared;
private final List<Dependency> dependencies;
private final List<Dependency> managedDependencies;

private ResolutionRoot(
Artifact artifact, boolean load, List<Dependency> dependencies, List<Dependency> managedDependencies) {
Artifact artifact,
boolean load,
boolean prepared,
List<Dependency> dependencies,
List<Dependency> managedDependencies) {
this.artifact = artifact;
this.load = load;
this.prepared = prepared;
this.dependencies = dependencies;
this.managedDependencies = managedDependencies;
}
Expand All @@ -45,6 +51,22 @@ public boolean isLoad() {
return load;
}

/**
* Is this instance prepared (for processing)?
*/
public boolean isPrepared() {
return prepared;
}

/**
* To mark root as prepared (for processing).
* <p>
* Note: users should not invoke this method, or at least, should be aware of the consequences.
*/
public ResolutionRoot prepared() {
return new ResolutionRoot(artifact, load, true, dependencies, managedDependencies);
}

/**
* Explicitly specified direct dependencies.
* <p>
Expand Down Expand Up @@ -118,7 +140,7 @@ public ResolutionRoot build() {
if (!load && dependencies == null) {
throw new IllegalStateException("must specify dependencies for non-loaded artifacts");
}
return new ResolutionRoot(artifact, load, dependencies, managedDependencies);
return new ResolutionRoot(artifact, load, false, dependencies, managedDependencies);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package eu.maveniverse.maven.toolbox.shared.internal;

import static java.util.Objects.requireNonNull;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.graph.Exclusion;
import org.eclipse.aether.util.artifact.ArtifactIdUtils;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;

/**
* A dependency visitor that dumps the graph to any {@link Consumer}{@code <String>}. Meant for diagnostic and testing, as
* it may output the graph to standard output, error or even some logging interface.
* <p>
* Copy of the corresponding class from Resolver 1.9.18, to retain same output across Maven 3.6+
*/
public class DependencyGraphDumper implements DependencyVisitor {

private final Consumer<String> consumer;

private final Deque<DependencyNode> nodes = new ArrayDeque<>();

public DependencyGraphDumper(Consumer<String> consumer) {
this.consumer = requireNonNull(consumer);
}

@Override
public boolean visitEnter(DependencyNode node) {
nodes.push(node);
consumer.accept(formatLine(nodes));
return true;
}

@Override
public boolean visitLeave(DependencyNode node) {
if (!nodes.isEmpty()) {
nodes.pop();
}
return true;
}

protected String formatLine(Deque<DependencyNode> nodes) {
return formatIndentation(nodes) + formatNode(nodes);
}

protected String formatIndentation(Deque<DependencyNode> nodes) {
StringBuilder buffer = new StringBuilder(128);
Iterator<DependencyNode> iter = nodes.descendingIterator();
DependencyNode parent = iter.hasNext() ? iter.next() : null;
DependencyNode child = iter.hasNext() ? iter.next() : null;
while (parent != null && child != null) {
boolean lastChild = parent.getChildren().get(parent.getChildren().size() - 1) == child;
boolean end = child == nodes.peekFirst();
String indent;
if (end) {
indent = lastChild ? "\\- " : "+- ";
} else {
indent = lastChild ? " " : "| ";
}
buffer.append(indent);
parent = child;
child = iter.hasNext() ? iter.next() : null;
}
return buffer.toString();
}

protected String formatNode(Deque<DependencyNode> nodes) {
DependencyNode node = requireNonNull(nodes.peek(), "bug: should not happen");
StringBuilder buffer = new StringBuilder(128);
Artifact a = node.getArtifact();
buffer.append(a);
Dependency d = node.getDependency();
if (d != null && !d.getScope().isEmpty()) {
buffer.append(" [").append(d.getScope());
if (d.isOptional()) {
buffer.append(", optional");
}
buffer.append("]");
}
String premanaged = DependencyManagerUtils.getPremanagedVersion(node);
if (premanaged != null && !premanaged.equals(a.getBaseVersion())) {
buffer.append(" (version managed from ").append(premanaged).append(")");
}

premanaged = DependencyManagerUtils.getPremanagedScope(node);
if (premanaged != null && d != null && !premanaged.equals(d.getScope())) {
buffer.append(" (scope managed from ").append(premanaged).append(")");
}

Boolean premanagedOptional = DependencyManagerUtils.getPremanagedOptional(node);
if (premanagedOptional != null && d != null && !premanagedOptional.equals(d.getOptional())) {
buffer.append(" (optionality managed from ")
.append(premanagedOptional)
.append(")");
}

Collection<Exclusion> premanagedExclusions = DependencyManagerUtils.getPremanagedExclusions(node);
if (premanagedExclusions != null && d != null && !equals(premanagedExclusions, d.getExclusions())) {
buffer.append(" (exclusions managed from ")
.append(premanagedExclusions)
.append(")");
}

Map<String, String> premanagedProperties = DependencyManagerUtils.getPremanagedProperties(node);
if (premanagedProperties != null && !equals(premanagedProperties, a.getProperties())) {
buffer.append(" (properties managed from ")
.append(premanagedProperties)
.append(")");
}

DependencyNode winner = (DependencyNode) node.getData().get(ConflictResolver.NODE_DATA_WINNER);
if (winner != null) {
if (ArtifactIdUtils.equalsId(a, winner.getArtifact())) {
buffer.append(" (nearer exists)");
} else {
Artifact w = winner.getArtifact();
buffer.append(" (conflicts with ");
if (ArtifactIdUtils.toVersionlessId(a).equals(ArtifactIdUtils.toVersionlessId(w))) {
buffer.append(w.getVersion());
} else {
buffer.append(w);
}
buffer.append(")");
}
}
return buffer.toString();
}

private boolean equals(Collection<Exclusion> c1, Collection<Exclusion> c2) {
return c1 != null && c2 != null && c1.size() == c2.size() && c1.containsAll(c2);
}

private boolean equals(Map<String, String> m1, Map<String, String> m2) {
return m1 != null
&& m2 != null
&& m1.size() == m2.size()
&& m1.entrySet().stream().allMatch(entry -> Objects.equals(m2.get(entry.getKey()), entry.getValue()));
}
}
Loading

0 comments on commit 8267512

Please sign in to comment.