[MNG-7881] Expose the rootDirectory for model processing (#1245)

This commit is contained in:
Guillaume Nodet 2023-09-19 16:31:35 +02:00 committed by GitHub
parent 9378676b80
commit 1eae6684e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 0 deletions

View File

@ -35,14 +35,18 @@ import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.internal.impl.DefaultSessionFactory;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.testing.PlexusTest;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.collection.DependencyGraphTransformer;
import org.eclipse.aether.collection.DependencyManager;
@ -100,6 +104,12 @@ public abstract class AbstractArtifactComponentTestCase // extends PlexusTestCas
RepositorySystemSession repoSession = initRepoSession();
MavenSession session = new MavenSession(
getContainer(), repoSession, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult());
new DefaultSessionFactory(
getContainer().lookup(RepositorySystem.class),
getContainer().lookup(MavenRepositorySystem.class),
getContainer(),
getContainer().lookup(RuntimeInformation.class))
.getSession(session);
legacySupport.setSession(session);
}

View File

@ -34,6 +34,7 @@ import org.apache.maven.artifact.InvalidArtifactRTException;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.internal.impl.DefaultSession;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
@ -269,6 +270,14 @@ public class DefaultProjectBuilder implements ProjectBuilder {
request.setModelCache(modelCacheFactory.createCache(config.session));
}
request.setTransformerContextBuilder(config.transformerContextBuilder);
DefaultSession session = (DefaultSession) config.session.getData().get(DefaultSession.class);
if (session != null) {
try {
request.setRootDirectory(session.getRootDirectory());
} catch (IllegalStateException e) {
// can happen if root directory cannot be found, just ignore
}
}
return request;
}

View File

@ -67,6 +67,7 @@ import org.apache.maven.model.inheritance.InheritanceAssembler;
import org.apache.maven.model.interpolation.ModelInterpolator;
import org.apache.maven.model.interpolation.ModelVersionProcessor;
import org.apache.maven.model.io.ModelParseException;
import org.apache.maven.model.io.ModelReader;
import org.apache.maven.model.management.DependencyManagementInjector;
import org.apache.maven.model.management.PluginManagementInjector;
import org.apache.maven.model.normalization.ModelNormalizer;
@ -1028,6 +1029,7 @@ public class DefaultModelBuilder implements ModelBuilder {
Map<String, Object> options = new HashMap<>(3);
options.put(ModelProcessor.IS_STRICT, strict);
options.put(ModelProcessor.SOURCE, modelSource);
options.put(ModelReader.ROOT_DIRECTORY, request.getRootDirectory());
InputSource source;
if (request.isLocationTracking()) {

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.building;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -70,6 +71,8 @@ public class DefaultModelBuildingRequest implements ModelBuildingRequest {
private TransformerContextBuilder contextBuilder;
private Path rootDirectory;
/**
* Creates an empty request.
*/
@ -98,6 +101,7 @@ public class DefaultModelBuildingRequest implements ModelBuildingRequest {
setModelCache(request.getModelCache());
setWorkspaceModelResolver(request.getWorkspaceModelResolver());
setTransformerContextBuilder(request.getTransformerContextBuilder());
setRootDirectory(request.getRootDirectory());
}
@Override
@ -370,4 +374,15 @@ public class DefaultModelBuildingRequest implements ModelBuildingRequest {
this.contextBuilder = contextBuilder;
return this;
}
@Override
public Path getRootDirectory() {
return rootDirectory;
}
@Override
public ModelBuildingRequest setRootDirectory(Path rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}
}

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.building;
import java.io.File;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
import java.util.Properties;
@ -264,4 +265,15 @@ class FilterModelBuildingRequest implements ModelBuildingRequest {
request.setTransformerContextBuilder(contextBuilder);
return this;
}
@Override
public Path getRootDirectory() {
return request.getRootDirectory();
}
@Override
public ModelBuildingRequest setRootDirectory(Path rootDirectory) {
request.setRootDirectory(rootDirectory);
return this;
}
}

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.building;
import java.io.File;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
import java.util.Properties;
@ -356,4 +357,8 @@ public interface ModelBuildingRequest {
TransformerContextBuilder getTransformerContextBuilder();
ModelBuildingRequest setTransformerContextBuilder(TransformerContextBuilder contextBuilder);
Path getRootDirectory();
ModelBuildingRequest setRootDirectory(Path rootDirectory);
}

View File

@ -95,6 +95,11 @@ public class DefaultModelReader implements ModelReader {
return (InputSource) value;
}
private Path getRootDirectory(Map<String, ?> options) {
Object value = (options != null) ? options.get(ROOT_DIRECTORY) : null;
return (Path) value;
}
private Model read(InputStream input, Path pomFile, Map<String, ?> options) throws IOException {
try {
XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory();

View File

@ -45,6 +45,12 @@ public interface ModelReader {
*/
String INPUT_SOURCE = "org.apache.maven.model.io.inputSource";
/**
* Name of the property used to store the project's root directory to use with
* XInclude support.
*/
String ROOT_DIRECTORY = "rootDirectory";
/**
* Reads the model from the specified file.
*