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

[MNG-8451] Core inject depMgt for plugin resolution #2000

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions compat/maven-settings-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ under the License.
<artifactId>japicmp-maven-plugin</artifactId>
<configuration>
<parameter>
<ignoreMissingClasses>true</ignoreMissingClasses>
<excludes>
<!-- <exclude>org.apache.maven.settings.validation.SettingsValidator#validate(org.apache.maven.settings.Settings,boolean,org.apache.maven.settings.building.SettingsProblemCollector):METHOD_NEW_DEFAULT</exclude>-->
<!-- <exclude>org.apache.maven.settings.building.DefaultSettingsBuilder#setSettingsReader(org.apache.maven.settings.io.SettingsReader):METHOD_REMOVED</exclude>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public CoreExportsProvider(CoreExports exports) {
this.exports = Objects.requireNonNull(exports);
}

@Override
public CoreExports get() {
return exports;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@
import javax.inject.Named;
import javax.inject.Singleton;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.DependencyScope;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.extension.internal.CoreExports;
import org.apache.maven.impl.InternalSession;
import org.apache.maven.impl.RequestTraceHelper;
import org.apache.maven.impl.resolver.RelocatedArtifact;
Expand Down Expand Up @@ -80,11 +88,16 @@ public class DefaultPluginDependenciesResolver implements PluginDependenciesReso

private final List<MavenPluginDependenciesValidator> dependenciesValidators;

private final CoreExports coreExports;

@Inject
public DefaultPluginDependenciesResolver(
RepositorySystem repoSystem, List<MavenPluginDependenciesValidator> dependenciesValidators) {
RepositorySystem repoSystem,
List<MavenPluginDependenciesValidator> dependenciesValidators,
CoreExports coreExports) {
this.repoSystem = repoSystem;
this.dependenciesValidators = dependenciesValidators;
this.coreExports = coreExports;
}

private Artifact toArtifact(Plugin plugin, RepositorySystemSession session) {
Expand Down Expand Up @@ -210,12 +223,29 @@ private DependencyResult resolveInternal(
request.setRequestContext(REPOSITORY_CONTEXT);
request.setRepositories(repositories);
request.setRoot(new org.eclipse.aether.graph.Dependency(pluginArtifact, null));
Map<String, org.eclipse.aether.graph.Dependency> core = getCoreExportsAsDependencies(session);
request.setManagedDependencies(core.values().stream().toList());
for (Dependency dependency : plugin.getDependencies()) {
org.eclipse.aether.graph.Dependency pluginDep =
RepositoryUtils.toDependency(dependency, session.getArtifactTypeRegistry());
if (!DependencyScope.SYSTEM.is(pluginDep.getScope())) {
pluginDep = pluginDep.setScope(DependencyScope.RUNTIME.id());
}
org.eclipse.aether.graph.Dependency managedDep =
core.get(pluginDep.getArtifact().getGroupId() + ":"
+ pluginDep.getArtifact().getArtifactId());
if (managedDep != null) {
// align version if needed
if (!Objects.equals(
pluginDep.getArtifact().getVersion(),
managedDep.getArtifact().getVersion())) {
pluginDep = pluginDep.setArtifact(pluginDep
.getArtifact()
.setVersion(managedDep.getArtifact().getVersion()));
}
// align scope
pluginDep = pluginDep.setScope(managedDep.getScope());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we flag them as system scope ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... to avoid downloading the jars ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but there is more to this PR, it breaks https://issues.apache.org/jira/browse/MNG-5783 We need to implement the proposed fix in refd issue (real classpath vs ${plugin.artifacts}).

Copy link
Member Author

@cstamas cstamas Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Former contains plugin stuff + maven core imported (as one), while latter should contain all JARs

}
request.addDependency(pluginDep);
}

Expand Down Expand Up @@ -246,4 +276,55 @@ private DependencyResult resolveInternal(
RequestTraceHelper.exit(trace);
}
}

private static final String CACHE_KEY =
DefaultPluginDependenciesResolver.class.getName() + "#getCoreExportsAsDependencies";

@SuppressWarnings("unchecked")
private Map<String, org.eclipse.aether.graph.Dependency> getCoreExportsAsDependencies(
RepositorySystemSession session) {
return (Map<String, org.eclipse.aether.graph.Dependency>)
session.getData().computeIfAbsent(CACHE_KEY, () -> {
HashMap<String, org.eclipse.aether.graph.Dependency> core = new HashMap<>();
ClassLoader classLoader = coreExports.getExportedPackages().get("org.apache.maven.*");
for (String coreArtifact : coreExports.getExportedArtifacts()) {
String[] split = coreArtifact.split(":");
if (split.length == 2) {
String groupId = split[0];
String artifactId = split[1];
String version = discoverArtifactVersion(classLoader, groupId, artifactId, null);
if (version != null) {
core.put(
groupId + ":" + artifactId,
new org.eclipse.aether.graph.Dependency(
new DefaultArtifact(groupId + ":" + artifactId + ":" + version),
DependencyScope.PROVIDED.id()));
}
}
}
return Collections.unmodifiableMap(core);
});
}

private static String discoverArtifactVersion(
ClassLoader classLoader, String groupId, String artifactId, @Nullable String defVal) {
String version = defVal;
String resource = "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties";
final Properties props = new Properties();
try (InputStream is = classLoader.getResourceAsStream(resource)) {
if (is != null) {
props.load(is);
}
version = props.getProperty("version");
} catch (IOException e) {
// fall through
}
if (version != null) {
version = version.trim();
if (version.startsWith("${")) {
version = defVal;
}
}
return version;
}
}
Loading