This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new fd8f99ed90 Fix Maven 4 extensions (#1601)
fd8f99ed90 is described below

commit fd8f99ed90ed4a0d1f1baa472a3661c980c2759c
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Tue Jul 9 14:10:26 2024 +0200

    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
---
 .../maven/api/services/xml/XmlReaderRequest.java   | 28 +++++++++++++++++++++-
 .../internal/impl/model/DefaultModelBuilder.java   |  8 +++++++
 .../main/resources/META-INF/maven/extension.xml    |  4 ++++
 .../main/java/org/apache/maven/cli/MavenCli.java   |  2 ++
 4 files changed, 41 insertions(+), 1 deletion(-)

diff --git 
a/api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlReaderRequest.java
 
b/api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlReaderRequest.java
index 3087e3a0b9..d261345f70 100644
--- 
a/api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlReaderRequest.java
+++ 
b/api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlReaderRequest.java
@@ -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;
diff --git 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
index 0ea2430097..079f960f0e 100644
--- 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
+++ 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
@@ -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) {
diff --git a/maven-core/src/main/resources/META-INF/maven/extension.xml 
b/maven-core/src/main/resources/META-INF/maven/extension.xml
index 8a87ea31f3..454115ecdc 100644
--- a/maven-core/src/main/resources/META-INF/maven/extension.xml
+++ b/maven-core/src/main/resources/META-INF/maven/extension.xml
@@ -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>
 
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java 
b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index b4219e66f0..1d1c85e1fd 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -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() {

Reply via email to