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

Fix MavenArtifactResolver issues with maven 3.9+ #519

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
66 changes: 21 additions & 45 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright (c) 2019, 2023, Gluon
~ Copyright (c) 2019, 2024, Gluon
~ All rights reserved.
~
~ Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -42,12 +42,11 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.resource>21</maven.compiler.resource>
<source.plugin.version>3.3.0</source.plugin.version>
<javadoc.plugin.version>3.5.0</javadoc.plugin.version>
<gpg.plugin.version>3.1.0</gpg.plugin.version>
<maven.resolver.version>1.7.3</maven.resolver.version>
<maven.plugin.version>3.9.9</maven.plugin.version>
<substrate.version>0.0.65-SNAPSHOT</substrate.version>
</properties>

Expand All @@ -60,57 +59,30 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.8</version>
<version>${maven.plugin.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-java</artifactId>
<version>0.9.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.8.8</version>
<version>${maven.plugin.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.2.0</version>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>3.3.9</version>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.15.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-http</artifactId>
<version>${maven.resolver.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-file</artifactId>
<version>${maven.resolver.version}</version>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-connector-basic</artifactId>
<version>${maven.resolver.version}</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>

Expand All @@ -134,12 +106,16 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.13.0</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.9.0</version>
<version>3.15.0</version>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
Expand All @@ -155,7 +131,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.0</version>
<executions>
<execution>
<id>enforce-no-snapshots</id>
Expand Down
31 changes: 23 additions & 8 deletions src/main/java/com/gluonhq/NativeBaseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.repository.RemoteRepository;

import java.io.File;
import java.io.IOException;
Expand All @@ -74,14 +77,31 @@ public abstract class NativeBaseMojo extends AbstractMojo {

private static final List<String> ALLOWED_DEPENDENCY_TYPES = Collections.singletonList("jar");

// TODO: Remove this restriction when MavenArtifactResolver works with Maven 3.9.0+
private static final Version MAX_SUPPORTED_MAVEN_VERSION = new Version(3, 8, 8);
private static final Version MAX_SUPPORTED_MAVEN_VERSION = new Version(3, 9, 9);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we expect this not to work on 4?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't tested it yet, honestly. Many parts are moving and being deprecated/removed for Maven 4, so I'd say we'll need to revisited when people start to use it (beta for now), or it gets finally released.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we need to change at least some logic here, because at this point, the plugin won't work starting with maven 3.9.10. We can perhaps invert the logic and say that we support all versions up to 4.0.0 (but not inclusive)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maven 4.0.0 is not released yet, but I'm trying to test Maven 4.0.0-alpha-13, and new Version() fails to parse it.

Removing the whole version check works (I've tested 3.6.3, 3.8.1, 3.8.8, 3.9.0, 3.9.9, 4.0.0-alpha-13), so we don't need it anymore.


Path outputDir;

@Parameter(defaultValue = "${project}", readonly = true)
MavenProject project;

/**
* The entry point to Maven Artifact Resolver
*/
@Component
private RepositorySystem repoSystem;

/**
* The current repository/network configuration of Maven.
*/
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
private RepositorySystemSession repoSession;

/**
* The project's remote repositories to use for the resolution.
*/
@Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
private List<RemoteRepository> remoteRepos;

@Parameter(defaultValue = "${session}", readonly = true)
MavenSession session;

Expand Down Expand Up @@ -242,12 +262,7 @@ private String getProjectClasspath() {
}

private List<File> getClasspathElements(MavenProject project) {
List<Repository> repositories = project.getRepositories();
Repository gluonRepository = new Repository();
gluonRepository.setId("Gluon");
gluonRepository.setUrl("https://nexus.gluonhq.com/nexus/content/repositories/releases");
repositories.add(gluonRepository);
MavenArtifactResolver.initRepositories(repositories);
MavenArtifactResolver.initRepositories(repoSystem, repoSession, remoteRepos);

List<Artifact> attachDependencies = getAttachDependencies();
List<File> list = Stream.concat(project.getArtifacts().stream(), attachDependencies.stream())
Expand Down
90 changes: 10 additions & 80 deletions src/main/java/com/gluonhq/utils/MavenArtifactResolver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Gluon
* Copyright (c) 2019, 2024, Gluon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -31,46 +31,29 @@
package com.gluonhq.utils;

import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.model.Repository;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
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.collection.CollectRequest;
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.transport.file.FileTransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;
import org.eclipse.aether.util.artifact.JavaScopes;
import org.eclipse.aether.util.filter.DependencyFilterUtils;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static org.eclipse.aether.repository.RepositoryPolicy.CHECKSUM_POLICY_WARN;
import static org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_DAILY;
import static org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_NEVER;

public class MavenArtifactResolver {

private static final String DEFAULT_LOCAL_REPO = org.apache.maven.repository.RepositorySystem.
Expand All @@ -87,7 +70,7 @@ public class MavenArtifactResolver {
*
* If the instance hasn't been created yet, it will throw an
* {@code IllegalStateException}. To prevent this,
* {@link #initRepositories(List)} has to be called first.
* {@link #initRepositories(RepositorySystem, RepositorySystemSession, List)} has to be called first.
*
* @return an instance of MavenArtifactResolver
*/
Expand All @@ -98,77 +81,24 @@ public static MavenArtifactResolver getInstance() {
return instance;
}

private MavenArtifactResolver(List<Repository> repositories) {
repositorySystem = createRepositorySystem();
systemSession = createRepositorySystemSession(repositorySystem, DEFAULT_LOCAL_REPO);
remoteRepositories = new LinkedList<>();
repositories.forEach(r -> {
org.apache.maven.model.RepositoryPolicy releases = r.getReleases();
RepositoryPolicy releasesRepositoryPolicy = null;
if (releases != null) {
releasesRepositoryPolicy = new RepositoryPolicy(releases.isEnabled(),
releases.getUpdatePolicy() == null ? UPDATE_POLICY_NEVER : releases.getUpdatePolicy(),
releases.getChecksumPolicy());
}
RepositoryPolicy snapshotsRepositoryPolicy = null;
org.apache.maven.model.RepositoryPolicy snapshots = r.getSnapshots();
if (snapshots != null) {
snapshotsRepositoryPolicy = new RepositoryPolicy(snapshots.isEnabled(),
snapshots.getUpdatePolicy() == null ? UPDATE_POLICY_NEVER : snapshots.getUpdatePolicy(),
snapshots.getChecksumPolicy());
}
if (releasesRepositoryPolicy == null && snapshotsRepositoryPolicy == null) {
if (r.getUrl().toLowerCase(Locale.ROOT).contains("/release")) {
releasesRepositoryPolicy = new RepositoryPolicy(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN);
snapshotsRepositoryPolicy = new RepositoryPolicy(false, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN);
} else if (r.getUrl().toLowerCase(Locale.ROOT).contains("/snapshot")) {
releasesRepositoryPolicy = new RepositoryPolicy(false, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN);
snapshotsRepositoryPolicy = new RepositoryPolicy(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN);
}
}

RemoteRepository repository = new RemoteRepository
.Builder(r.getId(), "default", r.getUrl())
.setReleasePolicy(releasesRepositoryPolicy)
.setSnapshotPolicy(snapshotsRepositoryPolicy)
.build();
remoteRepositories.add(repository);
});
private MavenArtifactResolver(RepositorySystem repositorySystem, RepositorySystemSession systemSession, List<RemoteRepository> repositories) {
this.repositorySystem = repositorySystem;
this.systemSession = systemSession;
this.remoteRepositories = repositories;
}

/**
* Creates and initializes a new instance with a list of remote
* repositories, only if such instance doesn't already exist
*
* @param repositories a list of remote repositories
* @param repositorySystem The entry point to Maven Artifact Resolver
* @param systemSession The current repository/network configuration of Maven
jperedadnr marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void initRepositories(List<Repository> repositories) {
public static void initRepositories(RepositorySystem repositorySystem, RepositorySystemSession systemSession, List<RemoteRepository> repositories) {
if (instance != null) {
return;
}
instance = new MavenArtifactResolver(repositories);
}

// TODO: Find an alternative, as this only works up until Maven 3.8.7.
private RepositorySystem createRepositorySystem() {
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
serviceLocator.addService(TransporterFactory.class, FileTransporterFactory.class);
serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
serviceLocator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() {
@Override
public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception) {
throw new RuntimeException(exception);
}
});
return serviceLocator.getService(RepositorySystem.class);
}

private DefaultRepositorySystemSession createRepositorySystemSession(RepositorySystem system, String localRepoPath) {
DefaultRepositorySystemSession systemSession = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository(localRepoPath);
systemSession.setLocalRepositoryManager(system.newLocalRepositoryManager(systemSession, localRepo));
return systemSession;
instance = new MavenArtifactResolver(repositorySystem, systemSession, repositories);
}

/**
Expand Down
Loading