Skip to content

Commit

Permalink
Aggregate repos from user Maven settings
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky committed Aug 2, 2022
1 parent f2e4c80 commit 5657387
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.GACTV;

public abstract class BootstrapFromOriginalJarTestBase extends PackageAppTestBase {

Expand Down Expand Up @@ -145,8 +144,10 @@ protected QuarkusBootstrap.Builder initBootstrapBuilder() throws Exception {
modulePom.setParent(parent);
final Path moduleDir = IoUtils.mkdirs(ws.resolve(modulePom.getArtifactId()));
ModelUtils.persistModel(moduleDir.resolve("pom.xml"), modulePom);
final Path resolvedJar = resolver.resolve(new GACTV(modulePom.getGroupId(), modulePom.getArtifactId(),
moduleDep.getClassifier(), moduleDep.getType(), modulePom.getVersion())).getResolvedPaths()
final Path resolvedJar = resolver
.resolve(ArtifactCoords.of(modulePom.getGroupId(), modulePom.getArtifactId(),
moduleDep.getClassifier(), moduleDep.getType(), modulePom.getVersion()))
.getResolvedPaths()
.getSinglePath();
final Path moduleTargetDir = moduleDir.resolve("target");
ZipUtils.unzip(resolvedJar, moduleTargetDir.resolve("classes"));
Expand All @@ -160,8 +161,10 @@ protected QuarkusBootstrap.Builder initBootstrapBuilder() throws Exception {
modulePom.setParent(parent);
final Path moduleDir = IoUtils.mkdirs(ws.resolve(modulePom.getArtifactId()));
ModelUtils.persistModel(moduleDir.resolve("pom.xml"), modulePom);
final Path resolvedJar = resolver.resolve(new GACTV(modulePom.getGroupId(), modulePom.getArtifactId(),
module.getClassifier(), module.getType(), modulePom.getVersion())).getResolvedPaths()
final Path resolvedJar = resolver
.resolve(ArtifactCoords.of(modulePom.getGroupId(), modulePom.getArtifactId(),
module.getClassifier(), module.getType(), modulePom.getVersion()))
.getResolvedPaths()
.getSinglePath();
final Path moduleTargetDir = moduleDir.resolve("target");
ZipUtils.unzip(resolvedJar, moduleTargetDir.resolve("classes"));
Expand Down Expand Up @@ -201,8 +204,10 @@ protected QuarkusBootstrap.Builder initBootstrapBuilder() throws Exception {
modulePom.setParent(parent);
final Path moduleDir = IoUtils.mkdirs(ws.resolve(modulePom.getArtifactId()));
ModelUtils.persistModel(moduleDir.resolve("pom.xml"), modulePom);
final Path resolvedJar = resolver.resolve(new GACTV(modulePom.getGroupId(), modulePom.getArtifactId(),
a.getClassifier(), a.getType(), modulePom.getVersion())).getResolvedPaths()
final Path resolvedJar = resolver
.resolve(ArtifactCoords.of(modulePom.getGroupId(), modulePom.getArtifactId(),
a.getClassifier(), a.getType(), modulePom.getVersion()))
.getResolvedPaths()
.getSinglePath();
final Path moduleTargetDir = moduleDir.resolve("target");
ZipUtils.unzip(resolvedJar, moduleTargetDir.resolve("classes"));
Expand All @@ -222,6 +227,7 @@ protected QuarkusBootstrap.Builder initBootstrapBuilder() throws Exception {
.setWorkspaceDiscovery(true)
.setWorkspaceModuleParentHierarchy(workspaceModuleParentHierarchy())
.setRootProjectDir(ws)
.setUserSettings(getSettingsXml() == null ? null : getSettingsXml().toFile())
.setCurrentProject(appPomXml.toString()))
.getCurrentProject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

public class SimpleExtAndAppCompileDepsTest extends BootstrapFromOriginalJarTestBase {

@Override
protected boolean setupCustomMavenRepoInSettings() {
// this is to make sure repositories enabled in user settings are properly aggregated
return true;
}

@Override
protected TsArtifact composeApplication() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;

import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
Expand Down Expand Up @@ -78,7 +78,7 @@ static void setup() throws Exception {

settingsXml = workDir().resolve("settings.xml");
try (BufferedWriter writer = Files.newBufferedWriter(settingsXml)) {
new DefaultSettingsWriter().write(writer, Collections.emptyMap(), settings);
new DefaultSettingsWriter().write(writer, Map.of(), settings);
}
testRepo = registryConfigDir.resolve("test-repo");
}
Expand Down Expand Up @@ -109,7 +109,7 @@ protected static String getCurrentQuarkusVersion() {
private static Settings getBaseMavenSettings(Path mavenSettings) throws IOException {
if (Files.exists(mavenSettings)) {
try (BufferedReader reader = Files.newBufferedReader(mavenSettings)) {
return new DefaultSettingsReader().read(reader, Collections.emptyMap());
return new DefaultSettingsReader().read(reader, Map.of());
}
}
return new Settings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.quarkus.bootstrap.resolver.BootstrapAppModelResolver;
import io.quarkus.bootstrap.resolver.TsArtifact;
import io.quarkus.bootstrap.resolver.TsDependency;
import io.quarkus.bootstrap.resolver.TsQuarkusExt;
Expand Down Expand Up @@ -48,7 +47,7 @@ public void setup() throws Exception {
.setRemoteRepositories(Collections.emptyList())
.build();

repoBuilder = TsRepoBuilder.getInstance(new BootstrapAppModelResolver(mvnResolver), workDir);
repoBuilder = TsRepoBuilder.getInstance(mvnResolver, workDir);
initRepo();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import io.quarkus.maven.dependency.DependencyFlags;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.aether.util.artifact.JavaScopes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -21,8 +21,8 @@
public abstract class CollectDependenciesBase extends ResolverSetupCleanup {

protected TsArtifact root;
protected List<Dependency> expectedResult = Collections.emptyList();
protected List<Dependency> deploymentDeps = Collections.emptyList();
protected List<Dependency> expectedResult = List.of();
protected List<Dependency> deploymentDeps = List.of();

@Override
@BeforeEach
Expand Down Expand Up @@ -57,12 +57,12 @@ protected BootstrapAppModelResolver getTestResolver() throws Exception {
}

protected Path getInstallDir(TsArtifact artifact) {
return repoHome.resolve(artifact.getGroupId().replace('.', '/')).resolve(artifact.getArtifactId())
return getInstallDir().resolve(artifact.getGroupId().replace('.', '/')).resolve(artifact.getArtifactId())
.resolve(artifact.getVersion());
}

protected TsArtifact install(TsArtifact dep, boolean collected) {
return install(dep, collected ? "compile" : null);
return install(dep, collected ? JavaScopes.COMPILE : null);
}

protected TsArtifact install(TsArtifact dep, String collectedInScope) {
Expand All @@ -74,7 +74,7 @@ protected TsArtifact install(TsArtifact dep, String collectedInScope, boolean op
}

protected TsArtifact install(TsArtifact dep, Path p, boolean collected) {
return install(dep, p, collected ? "compile" : null, false);
return install(dep, p, collected ? JavaScopes.COMPILE : null, false);
}

protected TsArtifact install(TsArtifact dep, Path p, String collectedInScope, boolean optional) {
Expand All @@ -93,15 +93,15 @@ protected TsQuarkusExt install(TsQuarkusExt ext) {
protected void install(TsQuarkusExt ext, boolean collected) {
ext.install(repo);
if (collected) {
addCollectedDep(ext.getRuntime(), "compile", false, DependencyFlags.RUNTIME_EXTENSION_ARTIFACT);
addCollectedDep(ext.getRuntime(), JavaScopes.COMPILE, false, DependencyFlags.RUNTIME_EXTENSION_ARTIFACT);
addCollectedDeploymentDep(ext.getDeployment());
}
}

protected void installAsDep(TsQuarkusExt ext) {
ext.install(repo);
root.addDependency(ext);
addCollectedDep(ext.getRuntime(), "compile", false,
addCollectedDep(ext.getRuntime(), JavaScopes.COMPILE, false,
DependencyFlags.DIRECT | DependencyFlags.RUNTIME_EXTENSION_ARTIFACT
| DependencyFlags.TOP_LEVEL_RUNTIME_EXTENSION_ARTIFACT);
addCollectedDeploymentDep(ext.getDeployment());
Expand Down Expand Up @@ -142,11 +142,11 @@ protected void installAsDep(TsDependency dep, Path p, boolean collected, int...
for (int f : flags) {
allFlags |= f;
}
addCollectedDep(artifact, dep.scope == null ? "compile" : dep.scope, dep.optional, allFlags);
addCollectedDep(artifact, dep.scope == null ? JavaScopes.COMPILE : dep.scope, dep.optional, allFlags);
}

protected void addCollectedDep(final TsArtifact artifact, int... flags) {
addCollectedDep(artifact, "compile", false, flags);
addCollectedDep(artifact, JavaScopes.COMPILE, false, flags);
}

protected void addCollectedDep(final TsArtifact artifact, final String scope, boolean optional, int... flags) {
Expand All @@ -168,7 +168,7 @@ protected void addCollectedDeploymentDep(TsArtifact ext) {
deploymentDeps = new ArrayList<>();
}
deploymentDeps
.add(new ArtifactDependency(ext.toArtifact(), "compile",
.add(new ArtifactDependency(ext.toArtifact(), JavaScopes.COMPILE,
DependencyFlags.DEPLOYMENT_CP));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalProject;
import io.quarkus.bootstrap.util.IoUtils;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.maven.settings.Activation;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.RepositoryPolicy;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.DefaultSettingsWriter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

Expand All @@ -19,7 +28,9 @@
public class ResolverSetupCleanup {

protected Path workDir;
protected Path repoHome;
private Path repoHome;
private Path localRepoHome;
private Path settingsXml;
protected BootstrapAppModelResolver resolver;
protected TsRepoBuilder repo;

Expand All @@ -30,8 +41,45 @@ public void setup() throws Exception {
setSystemProperties();
workDir = initWorkDir();
repoHome = IoUtils.mkdirs(workDir.resolve("repo"));
if (setupCustomMavenRepoInSettings()) {
localRepoHome = IoUtils.mkdirs(workDir).resolve("local-repo");

final Settings settings = new Settings();
final Profile profile = new Profile();
final Activation activation = new Activation();
activation.setActiveByDefault(true);
profile.setActivation(activation);
final Repository repo = new Repository();
repo.setId("custom-repo");
repo.setName("Custom Test Repo");
repo.setLayout("default");
try {
repo.setUrl(repoHome.toUri().toURL().toExternalForm());
} catch (MalformedURLException e) {
throw new BootstrapMavenException("Failed to initialize Maven repo URL", e);
}
RepositoryPolicy policy = new RepositoryPolicy();
policy.setEnabled(true);
policy.setChecksumPolicy("ignore");
policy.setUpdatePolicy("never");
repo.setReleases(policy);
repo.setSnapshots(policy);
profile.setId("custom-repo");
profile.addRepository(repo);
settings.addProfile(profile);

settingsXml = workDir.resolve("settings.xml");
try (BufferedWriter writer = Files.newBufferedWriter(settingsXml)) {
new DefaultSettingsWriter().write(writer, Map.of(), settings);
} catch (IOException e) {
throw new BootstrapMavenException("Failed to persist settings.xml", e);
}
} else {
localRepoHome = repoHome;
}

resolver = newAppModelResolver(null);
repo = TsRepoBuilder.getInstance(resolver, workDir);
repo = TsRepoBuilder.getInstance(newArtifactResolver(null, true), workDir);
}

@AfterEach
Expand All @@ -51,6 +99,22 @@ public void cleanup() {
}
}

protected Path getInstallDir() {
return repoHome;
}

/**
* Enabling this option will install all the artifacts to a Maven repo that
* will be enabled in the Maven settings as a remote repo for the test.
* Otherwise, all the artifacts will be installed in a Maven repo that will
* be configured as a local repo for the test.
*
* @return whether to setup a custom remote Maven repo for the test
*/
protected boolean setupCustomMavenRepoInSettings() {
return false;
}

protected void setSystemProperties() {
}

Expand All @@ -68,6 +132,10 @@ protected Path initWorkDir() {
return IoUtils.createRandomTmpDir();
}

protected Path getSettingsXml() {
return settingsXml;
}

protected boolean cleanWorkDir() {
return true;
}
Expand All @@ -85,12 +153,24 @@ protected BootstrapAppModelResolver newAppModelResolver(LocalProject currentProj
}

protected MavenArtifactResolver newArtifactResolver(LocalProject currentProject) throws BootstrapMavenException {
return MavenArtifactResolver.builder()
.setLocalRepository(repoHome.toString())
return newArtifactResolver(currentProject, false);
}

private MavenArtifactResolver newArtifactResolver(LocalProject currentProject, boolean forInstalling)
throws BootstrapMavenException {
final MavenArtifactResolver.Builder builder = MavenArtifactResolver.builder()
.setOffline(true)
.setWorkspaceDiscovery(false)
.setCurrentProject(currentProject)
.build();
.setCurrentProject(currentProject);
if (forInstalling) {
builder.setLocalRepository(repoHome.toString());
} else {
builder.setLocalRepository(localRepoHome.toString());
if (settingsXml != null) {
builder.setUserSettings(settingsXml.toFile()).setOffline(false);
}
}
return builder.build();
}

protected TsJar newJar() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.quarkus.bootstrap.resolver;

import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.bootstrap.resolver.maven.workspace.ModelUtils;
import io.quarkus.maven.dependency.ArtifactCoords;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.UUID;
import org.eclipse.aether.artifact.DefaultArtifact;

/**
*
Expand All @@ -18,14 +22,14 @@ private static void error(String message, Throwable t) {
throw new IllegalStateException(message, t);
}

public static TsRepoBuilder getInstance(BootstrapAppModelResolver resolver, Path workDir) {
public static TsRepoBuilder getInstance(MavenArtifactResolver resolver, Path workDir) {
return new TsRepoBuilder(resolver, workDir);
}

protected final Path workDir;
private final BootstrapAppModelResolver resolver;
private final MavenArtifactResolver resolver;

private TsRepoBuilder(BootstrapAppModelResolver resolver, Path workDir) {
private TsRepoBuilder(MavenArtifactResolver resolver, Path workDir) {
this.resolver = resolver;
this.workDir = workDir;
}
Expand Down Expand Up @@ -77,8 +81,10 @@ public void install(TsArtifact artifact, Path p) {

protected void install(ArtifactCoords artifact, Path file) {
try {
resolver.install(artifact, file);
} catch (AppModelResolverException e) {
resolver.install(new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(),
artifact.getType(),
artifact.getVersion(), Map.of(), file.toFile()));
} catch (BootstrapMavenException e) {
error("Failed to install " + artifact, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private ApplicationModel doResolveModel(ArtifactCoords coords,

directMvnDeps = DependencyUtils.mergeDeps(directMvnDeps, appArtifactDescr.getDependencies(), managedVersions,
getExcludedScopes());
aggregatedRepos = mvn.aggregateRepositories(managedRepos,
aggregatedRepos = mvn.aggregateRepositories(aggregatedRepos,
mvn.newResolutionRepositories(appArtifactDescr.getRepositories()));

return buildAppModel(appArtifact,
Expand Down
Loading

0 comments on commit 5657387

Please sign in to comment.