Skip to content

Commit

Permalink
Fix Maven 4 extensions (#1601)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gnodet committed Jul 9, 2024
1 parent 58e1a7b commit fd8f99e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 41,9 @@ public interface XmlReaderRequest {
@Nullable
Path getPath();

@Nullable
Path getRootDirectory();

@Nullable
URL getURL();

Expand Down Expand Up @@ -83,6 86,7 @@ static XmlReaderRequestBuilder builder() {
@NotThreadSafe
class XmlReaderRequestBuilder {
Path path;
Path rootDirectory;
URL url;
InputStream inputStream;
Reader reader;
Expand All @@ -97,6 101,11 @@ public XmlReaderRequestBuilder path(Path path) {
return this;
}

public XmlReaderRequestBuilder rootDirectory(Path rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}

public XmlReaderRequestBuilder url(URL url) {
this.url = url;
return this;
Expand Down Expand Up @@ -139,11 148,21 @@ public XmlReaderRequestBuilder addDefaultEntities(boolean addDefaultEntities) {

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;
Expand All @@ -156,6 175,7 @@ private static class DefaultXmlReaderRequest implements XmlReaderRequest {
@SuppressWarnings("checkstyle:ParameterNumber")
DefaultXmlReaderRequest(
Path path,
Path rootDirectory,
URL url,
InputStream inputStream,
Reader reader,
Expand All @@ -165,6 185,7 @@ private static class DefaultXmlReaderRequest implements XmlReaderRequest {
String location,
boolean addDefaultEntities) {
this.path = path;
this.rootDirectory = rootDirectory;
this.url = url;
this.inputStream = inputStream;
this.reader = reader;
Expand All @@ -180,6 201,11 @@ public Path getPath() {
return path;
}

@Override
public Path getRootDirectory() {
return rootDirectory;
}

@Override
public URL getURL() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 641,18 @@ private Model doReadFileModel(
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) {
Expand All @@ -657,6 664,7 @@ private Model doReadFileModel(
.strict(false)
.location(modelSource.getLocation())
.path(modelSource.getPath())
.rootDirectory(rootDirectory)
.inputStream(is)
.build());
} catch (XmlReaderException ne) {
Expand Down
4 changes: 4 additions & 0 deletions maven-core/src/main/resources/META-INF/maven/extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 677,8 @@ PlexusContainer container(CliRequest cliRequest) throws Exception {

final CoreExports exports = new CoreExports(containerRealm, exportedArtifacts, exportedPackages);

Thread.currentThread().setContextClassLoader(containerRealm);

DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {
@Override
protected void configure() {
Expand Down

0 comments on commit fd8f99e

Please sign in to comment.