Skip to content

Commit

Permalink
Fixed Maven artifact resolver initialization in QuarkusProjectMojoBase
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky committed Sep 12, 2024
1 parent 62039a3 commit 4e2a003
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.quarkus.maven;

import static io.quarkus.devtools.project.CodestartResourceLoadersBuilder.getCodestartResourceLoaders;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.maven.execution.MavenSession;
Expand All @@ -18,24 +15,23 @@
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.impl.RemoteRepositoryManager;
import org.eclipse.aether.repository.RemoteRepository;

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.CodestartResourceLoadersBuilder;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.devtools.project.buildfile.MavenProjectBuildFile;
import io.quarkus.maven.components.QuarkusWorkspaceProvider;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.utilities.MojoUtils;
import io.quarkus.platform.descriptor.loader.json.ResourceLoader;
import io.quarkus.platform.tools.ToolsConstants;
import io.quarkus.platform.tools.ToolsUtils;
Expand All @@ -56,9 +52,6 @@ public abstract class QuarkusProjectMojoBase extends AbstractMojo {
@Parameter(defaultValue = "${session}", readonly = true)
MavenSession session;

@Component
protected RepositorySystem repoSystem;

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

Expand All @@ -75,7 +68,7 @@ public abstract class QuarkusProjectMojoBase extends AbstractMojo {
private String bomVersion;

@Component
RemoteRepositoryManager remoteRepositoryManager;
QuarkusWorkspaceProvider workspaceProvider;

private List<ArtifactCoords> importedPlatforms;

Expand Down Expand Up @@ -108,10 +101,14 @@ public void execute() throws MojoExecutionException {
throw new MojoExecutionException("Failed to initialize Quarkus Maven extension manager", e);
}
} else {
final List<ResourceLoader> codestartsResourceLoader = getCodestartResourceLoaders(resolveExtensionCatalog());
quarkusProject = QuarkusProject.of(baseDir(), resolveExtensionCatalog(),
codestartsResourceLoader,
log, buildTool, JavaVersion.NA);
final ExtensionCatalog extensionCatalog = resolveExtensionCatalog();
final List<ResourceLoader> codestartsResourceLoader = CodestartResourceLoadersBuilder
.codestartLoadersBuilder()
.artifactResolver(artifactResolver())
.catalog(extensionCatalog)
.build();
quarkusProject = QuarkusProject.of(baseDir(), extensionCatalog,
codestartsResourceLoader, log, buildTool, JavaVersion.NA);
}

doExecute(quarkusProject, getMessageWriter());
Expand Down Expand Up @@ -155,7 +152,7 @@ protected List<ArtifactCoords> getImportedPlatforms() throws MojoExecutionExcept
if (importedPlatforms == null) {
if (project.getFile() == null) {
if (bomGroupId == null && bomArtifactId == null && bomVersion == null) {
return Collections.emptyList();
return List.of();
}
final ExtensionCatalogResolver catalogResolver = getExtensionCatalogResolver();
if (!catalogResolver.hasRegistries()) {
Expand All @@ -172,39 +169,35 @@ protected List<ArtifactCoords> getImportedPlatforms() throws MojoExecutionExcept
} catch (RegistryResolutionException e) {
throw new MojoExecutionException("Failed to resolve the catalog of Quarkus platforms", e);
}
return importedPlatforms = Collections.singletonList(platformBom);
return importedPlatforms = List.of(platformBom);
}
importedPlatforms = collectImportedPlatforms();
}
return importedPlatforms;
}

protected MavenArtifactResolver catalogArtifactResolver() throws MojoExecutionException {
return artifactResolver;
return artifactResolver();
}

protected MavenArtifactResolver artifactResolver() throws MojoExecutionException {
return artifactResolver == null ? artifactResolver = initArtifactResolver() : artifactResolver;
}

protected MavenArtifactResolver initArtifactResolver() throws MojoExecutionException {
try {
return MavenArtifactResolver.builder()
.setRepositorySystem(repoSystem)
.setRepositorySystemSession(
getLog().isDebugEnabled() ? repoSession : MojoUtils.muteTransferListener(repoSession))
.setRemoteRepositories(repos)
.setRemoteRepositoryManager(remoteRepositoryManager)
.build();
} catch (BootstrapMavenException e) {
throw new MojoExecutionException("Failed to initialize Maven artifact resolver", e);
}
var config = BootstrapMavenContext.config()
.setArtifactTransferLogging(getLog().isDebugEnabled())
.setRemoteRepositoryManager(workspaceProvider.getRemoteRepositoryManager())
.setRepositorySystem(workspaceProvider.getRepositorySystem())
.setRemoteRepositories(repos)
.setWorkspaceDiscovery(false)
.setRepositorySystemSession(repoSession);
return workspaceProvider.createArtifactResolver(config);
}

private List<ArtifactCoords> collectImportedPlatforms()
throws MojoExecutionException {
private List<ArtifactCoords> collectImportedPlatforms() {
final List<ArtifactCoords> descriptors = new ArrayList<>(4);
final List<Dependency> constraints = project.getDependencyManagement() == null ? Collections.emptyList()
final List<Dependency> constraints = project.getDependencyManagement() == null ? List.of()
: project.getDependencyManagement().getDependencies();
if (!constraints.isEmpty()) {
final MessageWriter log = getMessageWriter();
Expand All @@ -222,17 +215,6 @@ private List<ArtifactCoords> collectImportedPlatforms()
return descriptors;
}

private String getQuarkusCoreVersion() {
final List<Dependency> constraints = project.getDependencyManagement() == null ? Collections.emptyList()
: project.getDependencyManagement().getDependencies();
for (Dependency d : constraints) {
if (d.getArtifactId().endsWith("quarkus-core") && d.getGroupId().equals("io.quarkus")) {
return d.getVersion();
}
}
return null;
}

protected void validateParameters() throws MojoExecutionException {
}

Expand All @@ -241,8 +223,8 @@ protected abstract void doExecute(QuarkusProject quarkusProject, MessageWriter l

private Artifact projectArtifact() {
return projectArtifact == null
? projectArtifact = new DefaultArtifact(project.getGroupId(), project.getArtifactId(), null, "pom",
project.getVersion())
? projectArtifact = new DefaultArtifact(project.getGroupId(), project.getArtifactId(), null,
ArtifactCoords.TYPE_POM, project.getVersion())
: projectArtifact;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected MavenArtifactResolver catalogArtifactResolver() throws MojoExecutionEx
protected MavenArtifactResolver initArtifactResolver() throws MojoExecutionException {
return workspaceProvider.createArtifactResolver(BootstrapMavenContext.config()
.setUserSettings(session.getRequest().getUserSettingsFile())
.setRemoteRepositoryManager(remoteRepositoryManager)
.setRemoteRepositoryManager(workspaceProvider.getRemoteRepositoryManager())
// The system needs to be initialized with the bootstrap model builder to properly interpolate system properties set on the command line
// e.g. -Dquarkus.platform.version=xxx
//.setRepositorySystem(workspaceProvider.getRepositorySystem())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static QuarkusProject getProject(Artifact projectPom, Model projectModel,
final List<ArtifactCoords> importedPlatforms;
final String quarkusVersion;
if (projectPom == null) {
managedDeps = Collections.emptyList();
deps = () -> Collections.emptyList();
managedDeps = List.of();
deps = List::of;
importedPlatforms = Collections.emptyList();
// TODO allow multiple streams in the same catalog for now
quarkusVersion = null;// defaultQuarkusVersion.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import org.junit.jupiter.api.Test;

import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContextConfig;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.bootstrap.resolver.maven.workspace.ModelUtils;
import io.quarkus.devtools.testing.RegistryClientTestHelper;
import io.quarkus.maven.components.QuarkusWorkspaceProvider;

class AddExtensionMojoTest {

Expand Down Expand Up @@ -64,10 +66,21 @@ void init() throws Exception {
new BootstrapMavenContext(BootstrapMavenContext.config()
.setCurrentProject(OUTPUT_POM.getAbsolutePath())
.setOffline(true)));
mojo.repoSystem = mvn.getSystem();
mojo.repoSession = mvn.getSession();
mojo.repos = mvn.getRepositories();
mojo.remoteRepositoryManager = mvn.getRemoteRepositoryManager();
mojo.workspaceProvider = new QuarkusWorkspaceProvider(null, null, null, null, null,
mvn.getRemoteRepositoryManager(),
mvn.getMavenContext().getSettingsDecrypter()) {
@Override
public BootstrapMavenContext createMavenContext(BootstrapMavenContextConfig<?> config) {
return mvn.getMavenContext();
}

@Override
public MavenArtifactResolver createArtifactResolver(BootstrapMavenContextConfig<?> config) {
return mvn;
}
};

final Model effectiveModel = model.clone();
final DependencyManagement dm = new DependencyManagement();
Expand Down

0 comments on commit 4e2a003

Please sign in to comment.