Skip to content

Commit

Permalink
Pull in the corresponding upstream platform catalog when resolving th…
Browse files Browse the repository at this point in the history
…e primary catalog directly from Maven

(cherry picked from commit 0824167)
  • Loading branch information
aloubyansky authored and gsmet committed Aug 3, 2022
1 parent cea4afc commit 7b59d0f
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import io.quarkus.deployment.builditem.QuarkusBuildCloseablesBuildItem;
import io.quarkus.deployment.configuration.ClassLoadingConfig;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.GACT;
import io.quarkus.maven.dependency.GACTV;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.paths.DirectoryPathTree;
import io.quarkus.paths.MultiRootPathTree;
Expand Down Expand Up @@ -176,10 +176,10 @@ private void addIndexDependencyPaths(List<IndexDependencyBuildItem> indexDepende
}
try {
for (IndexDependencyBuildItem indexDependencyBuildItem : indexDependencyBuildItems) {
final ArtifactKey key = new GACT(indexDependencyBuildItem.getGroupId(),
final ArtifactKey key = ArtifactKey.of(indexDependencyBuildItem.getGroupId(),
indexDependencyBuildItem.getArtifactId(),
indexDependencyBuildItem.getClassifier(),
GACTV.TYPE_JAR);
ArtifactCoords.TYPE_JAR);
final ResolvedDependency artifact = userMap.get(key);
if (artifact == null) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package io.quarkus.cli;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Path;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.quarkus.bootstrap.resolver.maven.workspace.ModelUtils;
import io.quarkus.devtools.testing.registry.client.TestRegistryClientBuilder;
import io.quarkus.maven.dependency.ArtifactCoords;
import picocli.CommandLine;

public class MavenProjectCreateForPlatformTest extends RegistryClientBuilderTestBase {

@BeforeAll
static void configureRegistryAndMavenRepo() {
TestRegistryClientBuilder.newInstance()
.baseDir(registryConfigDir())
.persistPlatformDescriptorsAsMavenArtifacts()
.newRegistry("registry.acme.com")
.newPlatform("com.acme.quarkus.platform")
.newStream("9.0")
.newRelease("9.0.0")
.quarkusVersion("9.0.0.acme-1")
.upstreamQuarkusVersion("9.0.0")
.addCoreMember()
.alignPluginsOnQuarkusVersion()
.release()
.newMember("acme-bom")
.addExtension("io.quarkus.platform", "acme-quarkus-supersonic", "9.0.0.acme-1")
.addExtension("io.quarkus.platform", "acme-quarkus-subatomic", "9.0.0.acme-1")
.release().stream().platform()
.newStream("8.0")
.newRelease("8.0.0")
.quarkusVersion("8.0.0.acme-1")
.upstreamQuarkusVersion("8.0.0")
.addCoreMember()
.alignPluginsOnQuarkusVersion()
.release()
.newMember("acme-supersonic-bom")
.addExtension("io.quarkus.platform", "acme-quarkus-supersonic", "8.0.0.acme-1")
.registry()
.clientBuilder()
.newRegistry("registry.acme.org")
.newPlatform("io.quarkus.platform")
.newStream("9.0")
.newRelease("9.0.0")
.quarkusVersion("9.0.0")
.addCoreMember()
.alignPluginsOnQuarkusVersion()
.release()
.newMember("acme-bom").addExtension("acme-quarkus-supersonic").addExtension("acme-quarkus-subatomic")
.release().stream().platform()
.newStream("8.0")
.newRelease("8.0.0")
.quarkusVersion("8.0.0")
.addCoreMember()
.alignPluginsOnQuarkusVersion()
.release()
.newMember("acme-supersonic-bom").addExtension("acme-quarkus-supersonic").release()
.newMember("acme-subatomic-bom").addExtension("acme-quarkus-subatomic")
.registry()
.newNonPlatformCatalog(getCurrentQuarkusVersion())
.addExtension("org.acme", "acme-quarkiverse-extension", "1.0")
.registry()
.clientBuilder()
.build();
}

@Test
void testCreateForPlatformWithUpstream() throws Exception {

final CliDriver.Result createResult = run(workDir(), "create", "create-for-platform",
"-P com.acme.quarkus.platform:acme-supersonic-bom:8.0.0", "-x supersonic,subatomic");
createResult.echoSystemOut();
createResult.echoSystemErr();
assertThat(createResult.exitCode).isEqualTo(CommandLine.ExitCode.OK)
.as(() -> "Expected OK return code." + createResult);
assertThat(createResult.stdout).contains("SUCCESS")
.as(() -> "Expected confirmation that the project has been created." + createResult);

final Path projectDir = workDir().resolve("create-for-platform");
assertThat(projectDir).exists();
final Path pomXml = projectDir.resolve("pom.xml");
assertThat(pomXml).exists();

final Model model = ModelUtils.readModel(pomXml);
final Properties pomProps = model.getProperties();
assertThat(pomProps).containsEntry("quarkus-plugin.version", "8.0.0.acme-1");
assertThat(pomProps).containsEntry("quarkus.platform.artifact-id", "quarkus-bom");
assertThat(pomProps).containsEntry("quarkus.platform.group-id", "com.acme.quarkus.platform");
assertThat(pomProps).containsEntry("quarkus.platform.version", "8.0.0");

final DependencyManagement dm = model.getDependencyManagement();
assertThat(dm).isNotNull();
final List<Dependency> managed = dm.getDependencies();
assertThat(managed).hasSize(3);

Dependency bomImport = managed.get(0);
assertThat(bomImport.getGroupId()).isEqualTo("${quarkus.platform.group-id}");
assertThat(bomImport.getArtifactId()).isEqualTo("${quarkus.platform.artifact-id}");
assertThat(bomImport.getVersion()).isEqualTo("${quarkus.platform.version}");
assertThat(bomImport.getType()).isEqualTo("pom");
assertThat(bomImport.getScope()).isEqualTo("import");

bomImport = managed.get(1);
assertThat(bomImport.getGroupId()).isEqualTo("${quarkus.platform.group-id}");
assertThat(bomImport.getArtifactId()).isEqualTo("acme-supersonic-bom");
assertThat(bomImport.getVersion()).isEqualTo("${quarkus.platform.version}");
assertThat(bomImport.getType()).isEqualTo("pom");
assertThat(bomImport.getScope()).isEqualTo("import");

bomImport = managed.get(2);
assertThat(bomImport.getGroupId()).isEqualTo("io.quarkus.platform");
assertThat(bomImport.getArtifactId()).isEqualTo("acme-subatomic-bom");
assertThat(bomImport.getVersion()).isEqualTo("8.0.0");
assertThat(bomImport.getType()).isEqualTo("pom");
assertThat(bomImport.getScope()).isEqualTo("import");

final Set<ArtifactCoords> pomDeps = model.getDependencies().stream()
.map(d -> ArtifactCoords.of(d.getGroupId(), d.getArtifactId(), d.getClassifier(), d.getType(), d.getVersion()))
.collect(Collectors.toSet());
assertThat(pomDeps).contains(ArtifactCoords.of("io.quarkus.platform", "acme-quarkus-subatomic",
ArtifactCoords.DEFAULT_CLASSIFIER, ArtifactCoords.TYPE_JAR, null));
assertThat(pomDeps).contains(ArtifactCoords.of("io.quarkus.platform", "acme-quarkus-supersonic",
ArtifactCoords.DEFAULT_CLASSIFIER, ArtifactCoords.TYPE_JAR, null));
}
}
2 changes: 1 addition & 1 deletion devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ private String getSourceEncoding() {

private void addProject(MavenDevModeLauncher.Builder builder, ResolvedDependency module, boolean root) throws Exception {

if (!ArtifactCoords.TYPE_JAR.equals(module.getType())) {
if (!module.isJar()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private ArtifactCoords appArtifact(QuarkusBootstrapMojo mojo)
if (coordsArr.length == 3) {
version = coordsArr[2];
} else if (coordsArr.length > 3) {
classifier = coordsArr[2] == null ? "" : coordsArr[2];
classifier = coordsArr[2] == null ? ArtifactCoords.DEFAULT_CLASSIFIER : coordsArr[2];
type = coordsArr[3] == null ? ArtifactCoords.TYPE_JAR : coordsArr[3];
if (coordsArr.length > 4) {
version = coordsArr[4];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ abstract class AbstractDependencyBuilder<B extends AbstractDependencyBuilder<B,
String groupId;
String artifactId;
String classifier = ArtifactCoords.DEFAULT_CLASSIFIER;
String type = GACTV.TYPE_JAR;
String type = ArtifactCoords.TYPE_JAR;
String version;
String scope = "compile";
int flags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,9 @@ private void ensureArtifactCoords(ObjectNode extObject) {
}
}
if (artifactNode == null || groupId == null || artifactId == null || version == null) {
final ArtifactCoords coords = new GACTV(
final ArtifactCoords coords = ArtifactCoords.jar(
groupId == null ? project.getGroupId() : groupId,
artifactId == null ? project.getArtifactId() : artifactId,
null,
ArtifactCoords.TYPE_JAR,
version == null ? project.getVersion() : version);
extObject.put("artifact", coords.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,9 @@ private void ensureArtifactCoords(ObjectNode extObject) {
}
}
if (artifactNode == null || groupId == null || artifactId == null || version == null) {
final ArtifactCoords coords = new GACTV(
final ArtifactCoords coords = ArtifactCoords.jar(
groupId == null ? project.getGroupId() : groupId,
artifactId == null ? project.getArtifactId() : artifactId,
null,
ArtifactCoords.TYPE_JAR,
version == null ? project.getVersion() : version);
extObject.put("artifact", coords.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ public static String dotJoin(String... parts) {

public static ExtensionCatalog resolvePlatformDescriptorDirectly(String bomGroupId, String bomArtifactId, String bomVersion,
MavenArtifactResolver artifactResolver, MessageWriter log) {
// TODO remove this method once we have the registry service available
return resolvePlatformDescriptorDirectly(bomGroupId, bomArtifactId, bomVersion, artifactResolver, log, 1);
}

private static ExtensionCatalog resolvePlatformDescriptorDirectly(String bomGroupId, String bomArtifactId,
String bomVersion,
MavenArtifactResolver artifactResolver, MessageWriter log, int registryPreference) {
if (bomVersion == null) {
throw new IllegalArgumentException("BOM version was not provided");
}
Expand All @@ -96,6 +101,7 @@ public static ExtensionCatalog resolvePlatformDescriptorDirectly(String bomGroup
(bomArtifactId == null ? ToolsConstants.DEFAULT_PLATFORM_BOM_ARTIFACT_ID : bomArtifactId)
+ BootstrapConstants.PLATFORM_DESCRIPTOR_ARTIFACT_ID_SUFFIX,
bomVersion, "json", bomVersion);

Path platformJson = null;
try {
log.debug("Resolving platform descriptor %s", catalogCoords);
Expand Down Expand Up @@ -160,7 +166,8 @@ public static ExtensionCatalog resolvePlatformDescriptorDirectly(String bomGroup
}
}

final OriginPreference originPreference = new OriginPreference(1, 1, 1, ++memberIndex, 1);
final OriginPreference originPreference = new OriginPreference(registryPreference, 1, 1, ++memberIndex,
1);
Map<String, Object> metadata = new HashMap<>(memberCatalog.getMetadata());
metadata.put("origin-preference", originPreference);
ExtensionCatalog.Mutable mutableMemberCatalog = memberCatalog.mutable();
Expand All @@ -171,6 +178,20 @@ public static ExtensionCatalog resolvePlatformDescriptorDirectly(String bomGroup
}
}
}
if (catalog.getUpstreamQuarkusCoreVersion() != null
&& !catalog.getUpstreamQuarkusCoreVersion().isBlank()
&& !(bomVersion.equals(catalog.getUpstreamQuarkusCoreVersion())
&& catalogCoords.getGroupId().equals(ToolsConstants.DEFAULT_PLATFORM_BOM_GROUP_ID)
&& catalogCoords.getArtifactId().equals(ToolsConstants.DEFAULT_PLATFORM_BOM_ARTIFACT_ID
+ BootstrapConstants.PLATFORM_DESCRIPTOR_ARTIFACT_ID_SUFFIX))) {
try {
final ExtensionCatalog upstreamCatalog = resolvePlatformDescriptorDirectly(null, null,
catalog.getUpstreamQuarkusCoreVersion(), artifactResolver, log, registryPreference + 1);
catalog = CatalogMergeUtility.merge(List.of(catalog, upstreamCatalog));
} catch (Exception e) {
log.warn(e.getLocalizedMessage());
}
}
return catalog;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class TestRegistryClientBuilder {
private List<Extension> externalExtensions = List.of();
private List<TestCodestartBuilder> externalCodestartBuilders = List.of();
private MavenArtifactResolver resolver;
private boolean persistPlatformDescriptorsAsMavenArtifacts;

public static TestRegistryClientBuilder newInstance() {
return new TestRegistryClientBuilder();
Expand All @@ -74,6 +75,11 @@ public TestRegistryClientBuilder debug() {
return this;
}

public TestRegistryClientBuilder persistPlatformDescriptorsAsMavenArtifacts() {
this.persistPlatformDescriptorsAsMavenArtifacts = true;
return this;
}

public TestRegistryBuilder newRegistry(String id) {
return registries.computeIfAbsent(id, i -> new TestRegistryBuilder(this, id));
}
Expand Down Expand Up @@ -766,7 +772,7 @@ private void install(ArtifactCoords coords, Path path) {
registry().clientBuilder().getResolver().install(new DefaultArtifact(
coords.getGroupId(), coords.getArtifactId(), coords.getClassifier(), coords.getType(),
coords.getVersion(),
Collections.emptyMap(), path.toFile()));
Map.of(), path.toFile()));
} catch (BootstrapMavenException e) {
throw new IllegalStateException("Failed to install " + path + " as " + coords, e);
}
Expand All @@ -782,6 +788,10 @@ private void persist(Path memberDir) {
throw new IllegalStateException("Failed to persist extension catalog " + json, e);
}

if (registry().clientBuilder().persistPlatformDescriptorsAsMavenArtifacts) {
install(PlatformArtifacts.ensureCatalogArtifact(bom), json);
}

Path artifactPath = registry().clientBuilder().getTmpPath(bom);
try {
ModelUtils.persistModel(artifactPath, pom);
Expand Down

0 comments on commit 7b59d0f

Please sign in to comment.