Fix Maven 4 extensions (#1601)

* Add rootDirectory to XmlReaderRequest and fix maven-core exported artifacts
* Set the thread context classloader to the container realm to fix class loading from extensions
This commit is contained in:
Guillaume Nodet 2024-07-09 14:10:26 +02:00 committed by GitHub
parent 58e1a7b6a1
commit fd8f99ed90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 1 deletions

View File

@ -41,6 +41,9 @@ public interface XmlReaderRequest {
@Nullable
Path getPath();
@Nullable
Path getRootDirectory();
@Nullable
URL getURL();
@ -83,6 +86,7 @@ public interface XmlReaderRequest {
@NotThreadSafe
class XmlReaderRequestBuilder {
Path path;
Path rootDirectory;
URL url;
InputStream inputStream;
Reader reader;
@ -97,6 +101,11 @@ public interface XmlReaderRequest {
return this;
}
public XmlReaderRequestBuilder rootDirectory(Path rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}
public XmlReaderRequestBuilder url(URL url) {
this.url = url;
return this;
@ -139,11 +148,21 @@ public interface XmlReaderRequest {
public XmlReaderRequest build() {
return new DefaultXmlReaderRequest(
path, url, inputStream, reader, transformer, strict, modelId, location, addDefaultEntities);
path,
rootDirectory,
url,
inputStream,
reader,
transformer,
strict,
modelId,
location,
addDefaultEntities);
}
private static class DefaultXmlReaderRequest implements XmlReaderRequest {
final Path path;
final Path rootDirectory;
final URL url;
final InputStream inputStream;
final Reader reader;
@ -156,6 +175,7 @@ public interface XmlReaderRequest {
@SuppressWarnings("checkstyle:ParameterNumber")
DefaultXmlReaderRequest(
Path path,
Path rootDirectory,
URL url,
InputStream inputStream,
Reader reader,
@ -165,6 +185,7 @@ public interface XmlReaderRequest {
String location,
boolean addDefaultEntities) {
this.path = path;
this.rootDirectory = rootDirectory;
this.url = url;
this.inputStream = inputStream;
this.reader = reader;
@ -180,6 +201,11 @@ public interface XmlReaderRequest {
return path;
}
@Override
public Path getRootDirectory() {
return rootDirectory;
}
@Override
public URL getURL() {
return null;

View File

@ -641,11 +641,18 @@ public class DefaultModelBuilder implements ModelBuilder {
try {
boolean strict = request.getValidationLevel() >= ModelBuilderRequest.VALIDATION_LEVEL_MAVEN_2_0;
Path rootDirectory;
try {
rootDirectory = request.getSession().getRootDirectory();
} catch (IllegalStateException ignore) {
rootDirectory = modelSource.getPath();
}
try (InputStream is = modelSource.openStream()) {
model = modelProcessor.read(XmlReaderRequest.builder()
.strict(strict)
.location(modelSource.getLocation())
.path(modelSource.getPath())
.rootDirectory(rootDirectory)
.inputStream(is)
.build());
} catch (XmlReaderException e) {
@ -657,6 +664,7 @@ public class DefaultModelBuilder implements ModelBuilder {
.strict(false)
.location(modelSource.getLocation())
.path(modelSource.getPath())
.rootDirectory(rootDirectory)
.inputStream(is)
.build());
} catch (XmlReaderException ne) {

View File

@ -145,9 +145,13 @@ under the License.
<exportedArtifacts>
<!-- maven 4 api -->
<exportedArtifact>org.apache.maven:maven-api-core</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-di</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-meta</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-metadata</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-model</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-plugin</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-settings</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-spi</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-toolchain</exportedArtifact>
<exportedArtifact>org.apache.maven:maven-api-xml</exportedArtifact>

View File

@ -677,6 +677,8 @@ public class MavenCli {
final CoreExports exports = new CoreExports(containerRealm, exportedArtifacts, exportedPackages);
Thread.currentThread().setContextClassLoader(containerRealm);
DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {
@Override
protected void configure() {