Skip to content

Commit

Permalink
Important bugfix and bring over MIMA CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 13, 2024
1 parent 4306776 commit 621799f
Show file tree
Hide file tree
Showing 25 changed files with 1,987 additions and 36 deletions.
132 changes: 118 additions & 14 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

<name>${project.groupId}:${project.artifactId}</name>

<properties>
<mainClass>eu.maveniverse.maven.toolbox.cli.Main</mainClass>
</properties>

<dependencies>
<!-- CLI -->
<dependency>
Expand All @@ -42,28 +46,30 @@
<artifactId>shared</artifactId>
</dependency>

<!-- Provided (by runtime) -->
<!-- Search -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>${version.maven}</version>
<scope>provided</scope>
<groupId>org.apache.maven.indexer</groupId>
<artifactId>search-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<scope>provided</scope>
<groupId>org.apache.maven.indexer</groupId>
<artifactId>search-backend-remoterepository</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-util</artifactId>
<scope>provided</scope>
<groupId>org.apache.maven.indexer</groupId>
<artifactId>search-backend-smo</artifactId>
</dependency>

<!-- MIMA Context -->
<dependency>
<groupId>eu.maveniverse.maven.mima</groupId>
<artifactId>context</artifactId>
</dependency>

<!-- Runtime -->
<dependency>
<groupId>eu.maveniverse.maven.mima.runtime</groupId>
<artifactId>standalone-static</artifactId>
<artifactId>standalone-static-uber</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
Expand All @@ -77,6 +83,24 @@
<scope>runtime</scope>
</dependency>

<!-- Provided (by runtime) -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>${version.maven}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-util</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -88,8 +112,88 @@
<build>
<plugins>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-proc</id>
<goals>
<goal>compile</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>info.picocli</groupId>
<artifactId>picocli-codegen</artifactId>
<version>${version.picocli}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Aproject=${project.groupId}/${project.artifactId}</arg>
</compilerArgs>
<proc>only</proc>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>cli</id>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>uber</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${mainClass}</Main-Class>
<Specification-Title>${project.artifactId}</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Implementation-Title>${project.artifactId}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/LICENSE</exclude>
<exclude>META-INF/DEPENDENCIES</exclude>
<exclude>META-INF/NOTICE</exclude>
<exclude>**/module-info.class</exclude>
</excludes>
</filter>
</filters>
<entryPoints>
<entryPoint>${mainClass}</entryPoint>
</entryPoints>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
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 cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Classpath.java
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;
}
}
Loading

0 comments on commit 621799f

Please sign in to comment.