Skip to content

Commit

Permalink
Core: make it possible to index dependencies with a specific groupId
Browse files Browse the repository at this point in the history
- fixes #33316
  • Loading branch information
mkouba authored and carlesarnal committed Dec 18, 2023
1 parent f93238b commit d731567
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.deployment.builditem;

import java.util.Objects;
import java.util.Optional;

import io.quarkus.builder.item.MultiBuildItem;
Expand All @@ -18,9 +19,9 @@ public ExcludeDependencyBuildItem(String groupId, String artifactId) {
}

public ExcludeDependencyBuildItem(String groupId, String artifactId, Optional<String> classifier) {
this.groupId = groupId;
this.groupId = Objects.requireNonNull(groupId);
this.artifactId = artifactId;
this.classifier = classifier;
this.classifier = Objects.requireNonNull(classifier);
}

public String getGroupId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.deployment.builditem;

import java.util.Objects;

import io.quarkus.builder.item.MultiBuildItem;

/**
Expand All @@ -16,19 +18,31 @@ public IndexDependencyBuildItem(String groupId, String artifactId) {
}

public IndexDependencyBuildItem(String groupId, String artifactId, String classifier) {
this.groupId = groupId;
this.groupId = Objects.requireNonNull(groupId);
this.artifactId = artifactId;
this.classifier = classifier;
}

/**
*
* @return the groupId, never {@code null}
*/
public String getGroupId() {
return groupId;
}

/**
*
* @return the artifactId, or {@code null}
*/
public String getArtifactId() {
return artifactId;
}

/**
*
* @return the classifier, or {@code null}
*/
public String getClassifier() {
return classifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static final class IndexDependencyConfiguration {
void addConfiguredIndexedDependencies(BuildProducer<IndexDependencyBuildItem> indexDependencyBuildItemBuildProducer) {
for (IndexDependencyConfig indexDependencyConfig : config.indexDependency.values()) {
indexDependencyBuildItemBuildProducer.produce(new IndexDependencyBuildItem(indexDependencyConfig.groupId,
indexDependencyConfig.artifactId, indexDependencyConfig.classifier.orElse(null)));
indexDependencyConfig.artifactId.orElse(null), indexDependencyConfig.classifier.orElse(null)));
}
}

Expand Down Expand Up @@ -167,15 +167,21 @@ private void addIndexDependencyPaths(List<IndexDependencyBuildItem> indexDepende
if (indexDependencyBuildItems.isEmpty()) {
return;
}
final Set<ArtifactKey> indexDependencyKeys = new HashSet<>(indexDependencyBuildItems.size());
final Set<ArtifactKey> indexDependencyKeys = new HashSet<>();
final Set<String> indexGroupIds = new HashSet<>();
for (IndexDependencyBuildItem indexDependencyBuildItem : indexDependencyBuildItems) {
indexDependencyKeys.add(ArtifactKey.of(indexDependencyBuildItem.getGroupId(),
indexDependencyBuildItem.getArtifactId(),
indexDependencyBuildItem.getClassifier(),
ArtifactCoords.TYPE_JAR));
if (indexDependencyBuildItem.getArtifactId() != null) {
indexDependencyKeys.add(ArtifactKey.of(indexDependencyBuildItem.getGroupId(),
indexDependencyBuildItem.getArtifactId(),
indexDependencyBuildItem.getClassifier(),
ArtifactCoords.TYPE_JAR));
} else {
indexGroupIds.add(indexDependencyBuildItem.getGroupId());
}
}
for (ResolvedDependency dep : curateOutcomeBuildItem.getApplicationModel().getDependencies()) {
if (dep.isRuntimeCp() && indexDependencyKeys.contains(dep.getKey())) {
if (dep.isRuntimeCp()
&& (indexDependencyKeys.contains(dep.getKey()) || indexGroupIds.contains(dep.getGroupId()))) {
for (Path path : dep.getContentTree().getRoots()) {
if (!root.isExcludedFromIndexing(path)
&& !root.getResolvedPaths().contains(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class IndexDependencyConfig {
public String groupId;

/**
* The maven artifactId of the artifact.
* The maven artifactId of the artifact (optional).
*/
@ConfigItem
public String artifactId;
public Optional<String> artifactId;

/**
* The maven classifier of the artifact.
* The maven classifier of the artifact (optional).
*/
@ConfigItem
public Optional<String> classifier;
Expand Down
4 changes: 3 additions & 1 deletion docs/src/main/asciidoc/cdi-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ If you can't modify the dependency, you can still index it by adding `quarkus.in
[source,properties]
----
quarkus.index-dependency.<name>.group-id=
quarkus.index-dependency.<name>.artifact-id=
quarkus.index-dependency.<name>.artifact-id=(this one is optional)
quarkus.index-dependency.<name>.classifier=(this one is optional)
----
TIP: If no `artifact-id` is specified then all dependencies with the specificed `group-id` are indexed.
For example, the following entries ensure that the `org.acme:acme-api` dependency is indexed:
.Example application.properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ private boolean isApplicationArchiveExcluded(ArcConfig config, List<ExcludeDepen
}

for (ExcludeDependencyBuildItem excludeDependencyBuildItem : excludeDependencyBuildItems) {
if (archiveMatches(key, excludeDependencyBuildItem.getGroupId(), excludeDependencyBuildItem.getArtifactId(),
if (archiveMatches(key, excludeDependencyBuildItem.getGroupId(),
Optional.ofNullable(excludeDependencyBuildItem.getArtifactId()),
excludeDependencyBuildItem.getClassifier())) {
return true;
}
Expand All @@ -266,17 +267,16 @@ private boolean isApplicationArchiveExcluded(ArcConfig config, List<ExcludeDepen
return false;
}

public static boolean archiveMatches(ArtifactKey key, String groupId, String artifactId, Optional<String> classifier) {

if (Objects.equals(key.getArtifactId(), artifactId)
&& Objects.equals(key.getGroupId(), groupId)) {
public static boolean archiveMatches(ArtifactKey key, String groupId, Optional<String> artifactId,
Optional<String> classifier) {
if (Objects.equals(key.getGroupId(), groupId)
&& artifactId.isEmpty() || Objects.equals(key.getArtifactId(), artifactId.get())) {
if (classifier.isPresent() && Objects.equals(key.getClassifier(), classifier.get())) {
return true;
} else if (!classifier.isPresent() && ArtifactCoords.DEFAULT_CLASSIFIER.equals(key.getClassifier())) {
return true;
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public void testMatch() {
ArtifactKey key = GACT.fromString(GROUP_ID + ":" + ARTIFACT_ID);
ArtifactKey keyWithClassifier = GACT.fromString(GROUP_ID + ":" + ARTIFACT_ID + ":" + CLASSIFIER);

assertTrue(archiveMatches(key, GROUP_ID, ARTIFACT_ID, Optional.empty()));
assertFalse(archiveMatches(key, GROUP_ID, ARTIFACT_ID, Optional.of(CLASSIFIER)));
assertFalse(archiveMatches(key, GROUP_ID, "test1", Optional.empty()));
assertTrue(archiveMatches(key, GROUP_ID, Optional.of(ARTIFACT_ID), Optional.empty()));
assertFalse(archiveMatches(key, GROUP_ID, Optional.of(ARTIFACT_ID), Optional.of(CLASSIFIER)));
assertFalse(archiveMatches(key, GROUP_ID, Optional.of("test1"), Optional.empty()));

assertTrue(archiveMatches(keyWithClassifier, GROUP_ID, ARTIFACT_ID, Optional.of(CLASSIFIER)));
assertFalse(archiveMatches(keyWithClassifier, GROUP_ID, "test1", Optional.of(CLASSIFIER)));
assertFalse(archiveMatches(keyWithClassifier, GROUP_ID, ARTIFACT_ID, Optional.empty()));
assertTrue(archiveMatches(keyWithClassifier, GROUP_ID, Optional.of(ARTIFACT_ID), Optional.of(CLASSIFIER)));
assertFalse(archiveMatches(keyWithClassifier, GROUP_ID, Optional.of("test1"), Optional.of(CLASSIFIER)));
assertFalse(archiveMatches(keyWithClassifier, GROUP_ID, Optional.of(ARTIFACT_ID), Optional.empty()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,26 @@ public void testQuarkusIndexDependencyOnLocalModule() throws Exception {
assertZipEntriesCanBeOpenedAndClosed(runnerJar);
}

/**
* Tests that quarkus.index-dependency.* can be used for modules in a multimodule project. artifact-id is optional.
*/
@Test
public void testQuarkusIndexDependencyGroupIdOnLocalModule() throws Exception {
testDir = initProject("projects/quarkus-index-dependencies-groupid");

running = new RunningInvoker(testDir, false);
final MavenProcessInvocationResult result = running.execute(Collections.singletonList("package"),
Collections.emptyMap());

assertThat(result.getProcess().waitFor()).isEqualTo(0);

final File targetDir = new File(testDir.getAbsoluteFile(), "runner" + File.separator + "target");

final Path runnerJar = targetDir.toPath().resolve("quarkus-app").resolve("quarkus-run.jar");
Assertions.assertTrue(Files.exists(runnerJar), "Runner jar " + runnerJar + " is missing");
assertZipEntriesCanBeOpenedAndClosed(runnerJar);
}

@Test
public void testNativeSourcesPackage() throws Exception {
testDir = initProject("projects/uberjar-check", "projects/project-native-sources");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.acme</groupId>
<artifactId>quarkus-quickstart-multimodule-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>org.acme</groupId>
<artifactId>quarkus-quickstart-multimodule-lib</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.acme;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class SomeBean {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>org.acme</groupId>
<artifactId>quarkus-quickstart-multimodule-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<compiler-plugin.version>${compiler-plugin.version}</compiler-plugin.version>
<surefire-plugin.version>${version.surefire.plugin}</surefire-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>${maven.compiler.source}</maven.compiler.source>
<maven.compiler.target>${maven.compiler.target}</maven.compiler.target>
</properties>
<modules>
<module>library</module>
<module>runner</module>
</modules>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>\${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>\${quarkus-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>\${quarkus.platform.group-id}</groupId>
<artifactId>\${quarkus.platform.artifact-id}</artifactId>
<version>\${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.acme</groupId>
<artifactId>quarkus-quickstart-multimodule-lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Loading

0 comments on commit d731567

Please sign in to comment.