Skip to content

Commit fd8f99e

Browse files
authored
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
1 parent 58e1a7b commit fd8f99e

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlReaderRequest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public interface XmlReaderRequest {
4141
@Nullable
4242
Path getPath();
4343

44+
@Nullable
45+
Path getRootDirectory();
46+
4447
@Nullable
4548
URL getURL();
4649

@@ -83,6 +86,7 @@ static XmlReaderRequestBuilder builder() {
8386
@NotThreadSafe
8487
class XmlReaderRequestBuilder {
8588
Path path;
89+
Path rootDirectory;
8690
URL url;
8791
InputStream inputStream;
8892
Reader reader;
@@ -97,6 +101,11 @@ public XmlReaderRequestBuilder path(Path path) {
97101
return this;
98102
}
99103

104+
public XmlReaderRequestBuilder rootDirectory(Path rootDirectory) {
105+
this.rootDirectory = rootDirectory;
106+
return this;
107+
}
108+
100109
public XmlReaderRequestBuilder url(URL url) {
101110
this.url = url;
102111
return this;
@@ -139,11 +148,21 @@ public XmlReaderRequestBuilder addDefaultEntities(boolean addDefaultEntities) {
139148

140149
public XmlReaderRequest build() {
141150
return new DefaultXmlReaderRequest(
142-
path, url, inputStream, reader, transformer, strict, modelId, location, addDefaultEntities);
151+
path,
152+
rootDirectory,
153+
url,
154+
inputStream,
155+
reader,
156+
transformer,
157+
strict,
158+
modelId,
159+
location,
160+
addDefaultEntities);
143161
}
144162

145163
private static class DefaultXmlReaderRequest implements XmlReaderRequest {
146164
final Path path;
165+
final Path rootDirectory;
147166
final URL url;
148167
final InputStream inputStream;
149168
final Reader reader;
@@ -156,6 +175,7 @@ private static class DefaultXmlReaderRequest implements XmlReaderRequest {
156175
@SuppressWarnings("checkstyle:ParameterNumber")
157176
DefaultXmlReaderRequest(
158177
Path path,
178+
Path rootDirectory,
159179
URL url,
160180
InputStream inputStream,
161181
Reader reader,
@@ -165,6 +185,7 @@ private static class DefaultXmlReaderRequest implements XmlReaderRequest {
165185
String location,
166186
boolean addDefaultEntities) {
167187
this.path = path;
188+
this.rootDirectory = rootDirectory;
168189
this.url = url;
169190
this.inputStream = inputStream;
170191
this.reader = reader;
@@ -180,6 +201,11 @@ public Path getPath() {
180201
return path;
181202
}
182203

204+
@Override
205+
public Path getRootDirectory() {
206+
return rootDirectory;
207+
}
208+
183209
@Override
184210
public URL getURL() {
185211
return null;

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,18 @@ private Model doReadFileModel(
641641
try {
642642
boolean strict = request.getValidationLevel() >= ModelBuilderRequest.VALIDATION_LEVEL_MAVEN_2_0;
643643

644+
Path rootDirectory;
645+
try {
646+
rootDirectory = request.getSession().getRootDirectory();
647+
} catch (IllegalStateException ignore) {
648+
rootDirectory = modelSource.getPath();
649+
}
644650
try (InputStream is = modelSource.openStream()) {
645651
model = modelProcessor.read(XmlReaderRequest.builder()
646652
.strict(strict)
647653
.location(modelSource.getLocation())
648654
.path(modelSource.getPath())
655+
.rootDirectory(rootDirectory)
649656
.inputStream(is)
650657
.build());
651658
} catch (XmlReaderException e) {
@@ -657,6 +664,7 @@ private Model doReadFileModel(
657664
.strict(false)
658665
.location(modelSource.getLocation())
659666
.path(modelSource.getPath())
667+
.rootDirectory(rootDirectory)
660668
.inputStream(is)
661669
.build());
662670
} catch (XmlReaderException ne) {

maven-core/src/main/resources/META-INF/maven/extension.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ under the License.
145145
<exportedArtifacts>
146146
<!-- maven 4 api -->
147147
<exportedArtifact>org.apache.maven:maven-api-core</exportedArtifact>
148+
<exportedArtifact>org.apache.maven:maven-api-di</exportedArtifact>
148149
<exportedArtifact>org.apache.maven:maven-api-meta</exportedArtifact>
150+
<exportedArtifact>org.apache.maven:maven-api-metadata</exportedArtifact>
149151
<exportedArtifact>org.apache.maven:maven-api-model</exportedArtifact>
152+
<exportedArtifact>org.apache.maven:maven-api-plugin</exportedArtifact>
150153
<exportedArtifact>org.apache.maven:maven-api-settings</exportedArtifact>
154+
<exportedArtifact>org.apache.maven:maven-api-spi</exportedArtifact>
151155
<exportedArtifact>org.apache.maven:maven-api-toolchain</exportedArtifact>
152156
<exportedArtifact>org.apache.maven:maven-api-xml</exportedArtifact>
153157

maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,8 @@ PlexusContainer container(CliRequest cliRequest) throws Exception {
677677

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

680+
Thread.currentThread().setContextClassLoader(containerRealm);
681+
680682
DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {
681683
@Override
682684
protected void configure() {

0 commit comments

Comments
 (0)