-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Important bugfix and bring over MIMA CLI
- Loading branch information
Showing
25 changed files
with
1,987 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
cli/src/main/java/eu/maveniverse/maven/toolbox/cli/ArtifactRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.cli; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
import org.eclipse.aether.AbstractRepositoryListener; | ||
import org.eclipse.aether.RepositoryEvent; | ||
import org.eclipse.aether.artifact.Artifact; | ||
import org.eclipse.aether.repository.RemoteRepository; | ||
|
||
/** | ||
* Records in-memory all the resolved artifacts. | ||
*/ | ||
final class ArtifactRecorder extends AbstractRepositoryListener { | ||
private final RemoteRepository sentinel = new RemoteRepository.Builder("none", "default", "fake").build(); | ||
private final ConcurrentHashMap<RemoteRepository, ArrayList<Artifact>> artifactsMap = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void artifactResolved(RepositoryEvent event) { | ||
if (event.getException() == null) { | ||
RemoteRepository repository = event.getRepository() instanceof RemoteRepository | ||
? (RemoteRepository) event.getRepository() | ||
: sentinel; | ||
artifactsMap.computeIfAbsent(repository, k -> new ArrayList<>()).add(event.getArtifact()); | ||
} | ||
} | ||
|
||
public RemoteRepository getSentinel() { | ||
return sentinel; | ||
} | ||
|
||
public Map<RemoteRepository, ArrayList<Artifact>> getArtifactsMap() { | ||
return artifactsMap; | ||
} | ||
|
||
public List<Artifact> getAllArtifacts() { | ||
return artifactsMap.values().stream().flatMap(Collection::stream).collect(Collectors.toList()); | ||
} | ||
|
||
public Set<Artifact> getUniqueArtifacts() { | ||
return new HashSet<>(getAllArtifacts()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Classpath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.cli; | ||
|
||
import eu.maveniverse.maven.mima.context.Context; | ||
import java.io.File; | ||
import java.util.stream.Collectors; | ||
import org.eclipse.aether.artifact.Artifact; | ||
import org.eclipse.aether.collection.CollectRequest; | ||
import org.eclipse.aether.graph.Dependency; | ||
import org.eclipse.aether.resolution.DependencyRequest; | ||
import org.eclipse.aether.resolution.DependencyResult; | ||
import org.eclipse.aether.util.artifact.JavaScopes; | ||
import org.eclipse.aether.util.filter.DependencyFilterUtils; | ||
import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator; | ||
import picocli.CommandLine; | ||
|
||
/** | ||
* Resolves transitively a given GAV and outputs classpath path. | ||
*/ | ||
@CommandLine.Command(name = "classpath", description = "Resolves Maven Artifact and prints out the classpath") | ||
public final class Classpath extends ResolverCommandSupport { | ||
|
||
enum ClasspathScope { | ||
runtime, | ||
compile, | ||
test; | ||
} | ||
|
||
@CommandLine.Parameters(index = "0", description = "The GAV to print classpath for") | ||
private String gav; | ||
|
||
@CommandLine.Option(names = "--scope", defaultValue = "runtime") | ||
private ClasspathScope scope; | ||
|
||
@CommandLine.Option( | ||
names = {"--boms"}, | ||
defaultValue = "", | ||
split = ",", | ||
description = "Comma separated list of BOMs to apply") | ||
private String[] boms; | ||
|
||
@Override | ||
protected Integer doCall(Context context) throws Exception { | ||
java.util.List<Dependency> managedDependencies = importBoms(context, boms); | ||
Artifact artifact = parseGav(gav, managedDependencies); | ||
|
||
CollectRequest collectRequest = new CollectRequest(); | ||
collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE)); | ||
collectRequest.setRepositories(context.remoteRepositories()); | ||
collectRequest.setManagedDependencies(managedDependencies); | ||
DependencyRequest dependencyRequest = | ||
new DependencyRequest(collectRequest, DependencyFilterUtils.classpathFilter(scope.name())); | ||
|
||
verbose("Resolving {}", dependencyRequest); | ||
DependencyResult dependencyResult = | ||
context.repositorySystem().resolveDependencies(getRepositorySystemSession(), dependencyRequest); | ||
|
||
PreorderNodeListGenerator nlg = new PreorderNodeListGenerator(); | ||
dependencyResult.getRoot().accept(nlg); | ||
// TODO: Do not use PreorderNodeListGenerator#getClassPath() until MRESOLVER-483 is fixed/released | ||
info("{}", nlg.getFiles().stream().map(File::getAbsolutePath).collect(Collectors.joining(File.pathSeparator))); | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.