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

Make sure quarkus:go-offline properly supports test scoped dependencies #31092

Merged
merged 1 commit into from
Feb 11, 2023
Merged
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
75 changes: 47 additions & 28 deletions devtools/maven/src/main/java/io/quarkus/maven/GoOfflineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -28,6 +29,7 @@
import io.quarkus.bootstrap.resolver.maven.workspace.LocalProject;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalWorkspace;
import io.quarkus.bootstrap.util.IoUtils;
import io.quarkus.bootstrap.workspace.ArtifactSources;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.runtime.LaunchMode;

Expand All @@ -39,19 +41,27 @@
public class GoOfflineMojo extends AbstractMojo {

@Parameter(defaultValue = "${project}", readonly = true, required = true)
protected MavenProject project;
MavenProject project;

@Component
private RepositorySystem repoSystem;
RepositorySystem repoSystem;

@Component
RemoteRepositoryManager remoteRepositoryManager;

@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
private RepositorySystemSession repoSession;
RepositorySystemSession repoSession;

@Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
private List<RemoteRepository> repos;
List<RemoteRepository> repos;

/**
* Target launch mode corresponding to {@link io.quarkus.runtime.LaunchMode} for which the dependencies should be resolved.
* {@code io.quarkus.runtime.LaunchMode.TEST} is the default, since it includes both {@code provided} and {@code test}
* dependency scopes.
*/
@Parameter(property = "mode", required = false, defaultValue = "test")
String mode;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand All @@ -62,43 +72,47 @@ public void execute() throws MojoExecutionException, MojoFailureException {
project.getVersion());

final MavenArtifactResolver resolver = getResolver();
final BootstrapAppModelResolver appModelResolver = new BootstrapAppModelResolver(resolver);

final Set<String> excludedScopes;
if (mode.equalsIgnoreCase("test")) {
appModelResolver.setTest(true);
excludedScopes = Set.of();
} else if (mode.equalsIgnoreCase("dev") || mode.equalsIgnoreCase("development")) {
appModelResolver.setDevMode(true);
excludedScopes = Set.of("test");
} else if (mode.equalsIgnoreCase("prod") || mode.isEmpty()) {
excludedScopes = Set.of("test", "provided");
} else {
throw new IllegalArgumentException(
"Unrecognized mode '" + mode + "', supported values are test, dev, development, prod");
}

final DependencyNode root;
try {
root = resolver.collectDependencies(pom, List.of()).getRoot();
root = resolver.getSystem().collectDependencies(
resolver.getSession(),
resolver.newCollectManagedRequest(pom, List.of(), List.of(), List.of(), List.of(), excludedScopes))
.getRoot();
} catch (Exception e) {
throw new MojoExecutionException("Failed to collect dependencies of " + pom, e);
}

final List<Path> createdDirs = new ArrayList<>();
final LocalWorkspace workspace = resolver.getMavenContext().getWorkspace();
final List<Path> createdDirs = new ArrayList<>(workspace.getProjects().size());
try {
ensureResolvableModule(root, resolver.getMavenContext().getWorkspace(), createdDirs);
final ArtifactCoords appArtifact = ArtifactCoords.of(pom.getGroupId(), pom.getArtifactId(), pom.getClassifier(),
pom.getExtension(), pom.getVersion());
resolveAppModel(resolver, appArtifact, LaunchMode.NORMAL);
resolveAppModel(resolver, appArtifact, LaunchMode.DEVELOPMENT);
resolveAppModel(resolver, appArtifact, LaunchMode.TEST);
ensureResolvableModule(root, workspace, createdDirs);
appModelResolver.resolveModel(ArtifactCoords.of(pom.getGroupId(), pom.getArtifactId(), pom.getClassifier(),
pom.getExtension(), pom.getVersion()));
} catch (AppModelResolverException e) {
throw new MojoExecutionException("Failed to resolve Quarkus application model for " + project.getArtifact(), e);
} finally {
for (Path d : createdDirs) {
IoUtils.recursiveDelete(d);
}
}
}

private void resolveAppModel(final MavenArtifactResolver resolver, final ArtifactCoords appArtifact, LaunchMode mode)
throws MojoExecutionException {
final BootstrapAppModelResolver appModelResolver = new BootstrapAppModelResolver(resolver);
if (mode == LaunchMode.DEVELOPMENT) {
appModelResolver.setDevMode(true);
} else if (mode == LaunchMode.TEST) {
appModelResolver.setTest(true);
}
try {
appModelResolver.resolveModel(appArtifact);
} catch (AppModelResolverException e) {
throw new MojoExecutionException("Failed to resolve Quarkus application model for " + project.getArtifact(), e);
}
}

private MavenArtifactResolver getResolver() throws MojoExecutionException {
try {
return MavenArtifactResolver.builder()
Expand All @@ -118,7 +132,12 @@ private static void ensureResolvableModule(DependencyNode node, LocalWorkspace w
if (artifact != null) {
final LocalProject module = workspace.getProject(artifact.getGroupId(), artifact.getArtifactId());
if (module != null && !module.getRawModel().getPackaging().equals(ArtifactCoords.TYPE_POM)) {
final Path classesDir = module.getClassesDir();
final Path classesDir;
if (artifact.getClassifier().equals(ArtifactSources.TEST)) {
classesDir = module.getTestClassesDir();
} else {
classesDir = module.getClassesDir();
}
if (!Files.exists(classesDir)) {
Path topDirToCreate = classesDir;
while (!Files.exists(topDirToCreate.getParent())) {
Expand Down