diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 7604aea1c9..b5841ecf15 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -91,6 +91,15 @@ public class DefaultProjectBuilder DefaultModelBuildingListener listener = new DefaultModelBuildingListener( projectBuildingHelper, configuration ); request.setModelBuildingListeners( Arrays.asList( listener ) ); + if ( localProject ) + { + request.setPomFile( pomFile ); + } + else + { + request.setModelSource( new FileModelSource( pomFile ) ); + } + ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); try @@ -98,14 +107,7 @@ public class DefaultProjectBuilder ModelBuildingResult result; try { - if ( localProject ) - { - result = modelBuilder.build( pomFile, request ); - } - else - { - result = modelBuilder.build( new FileModelSource( pomFile ), request ); - } + result = modelBuilder.build( request ); } catch ( ModelBuildingException e ) { @@ -254,10 +256,12 @@ public class DefaultProjectBuilder { ModelBuildingRequest request = getModelBuildingRequest( config ); + request.setModelSource( new UrlModelSource( getClass().getResource( "standalone.xml" ) ) ); + ModelBuildingResult result; try { - result = modelBuilder.build( new UrlModelSource( getClass().getResource( "standalone.xml" ) ), request ); + result = modelBuilder.build( request ); } catch ( ModelBuildingException e ) { diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index 8ddd69ef04..1d3144c447 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -103,30 +103,18 @@ public class DefaultModelBuilder @Requirement private PluginConfigurationExpander pluginConfigurationExpander; - public ModelBuildingResult build( File pomFile, ModelBuildingRequest request ) - throws ModelBuildingException - { - return build( new FileModelSource( pomFile ), pomFile, request ); - } - - public ModelBuildingResult build( ModelSource modelSource, ModelBuildingRequest request ) - throws ModelBuildingException - { - return build( modelSource, null, request ); - } - - private ModelBuildingResult build( ModelSource modelSource, File pomFile, ModelBuildingRequest request ) + public ModelBuildingResult build( ModelBuildingRequest request ) throws ModelBuildingException { DefaultModelBuildingResult result = new DefaultModelBuildingResult(); List problems = new ArrayList(); - ProfileActivationContext profileActivationContext = getProfileActivationContext( pomFile, request ); + ProfileActivationContext profileActivationContext = getProfileActivationContext( request ); List activeExternalProfiles = getActiveExternalProfiles( request, profileActivationContext, problems ); - Model inputModel = readModel( modelSource, pomFile, request, problems ); + Model inputModel = readModel( request.getModelSource(), request.getPomFile(), request, problems ); ModelData resultData = new ModelData( inputModel ); @@ -234,6 +222,18 @@ public class DefaultModelBuilder { Model model; + if ( modelSource == null ) + { + if ( pomFile != null ) + { + modelSource = new FileModelSource( pomFile ); + } + else + { + throw new IllegalArgumentException( "neither model source nor input file are specified" ); + } + } + try { boolean strict = request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0; @@ -300,14 +300,14 @@ public class DefaultModelBuilder } } - private ProfileActivationContext getProfileActivationContext( File pomFile, ModelBuildingRequest request ) + private ProfileActivationContext getProfileActivationContext( ModelBuildingRequest request ) { ProfileActivationContext context = new DefaultProfileActivationContext(); context.setActiveProfileIds( request.getActiveProfileIds() ); context.setInactiveProfileIds( request.getInactiveProfileIds() ); context.setExecutionProperties( request.getExecutionProperties() ); - context.setProjectDirectory( ( pomFile != null ) ? pomFile.getParentFile() : null ); + context.setProjectDirectory( ( request.getPomFile() != null ) ? request.getPomFile().getParentFile() : null ); return context; } @@ -438,7 +438,7 @@ public class DefaultModelBuilder return null; } - Model candidateModel = readModel( new FileModelSource( pomFile ), pomFile, request, problems ); + Model candidateModel = readModel( null, pomFile, request, problems ); String groupId = candidateModel.getGroupId(); if ( groupId == null && candidateModel.getParent() != null ) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java index c85f1154d0..56e40d0b28 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java @@ -19,6 +19,7 @@ package org.apache.maven.model.building; * under the License. */ +import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -36,6 +37,10 @@ public class DefaultModelBuildingRequest implements ModelBuildingRequest { + private File pomFile; + + private ModelSource modelSource; + private int validationLevel = VALIDATION_LEVEL_STRICT; private boolean processPlugins; @@ -54,6 +59,30 @@ public class DefaultModelBuildingRequest private List modelBuildingListeners; + public File getPomFile() + { + return pomFile; + } + + public DefaultModelBuildingRequest setPomFile( File pomFile ) + { + this.pomFile = pomFile; + + return this; + } + + public ModelSource getModelSource() + { + return modelSource; + } + + public DefaultModelBuildingRequest setModelSource( ModelSource modelSource ) + { + this.modelSource = modelSource; + + return this; + } + public int getValidationLevel() { return validationLevel; diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuilder.java index f769b532c1..473f94f90f 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuilder.java @@ -19,10 +19,6 @@ package org.apache.maven.model.building; * under the License. */ -import java.io.File; - -import org.apache.maven.model.resolution.ModelResolver; - /** * Builds the effective model from a POM. * @@ -32,29 +28,13 @@ public interface ModelBuilder { /** - * Builds the effective model of the specified POM file. Note that this method overload is meant to build the - * effective model for the build process of a project. Hence the effective model supports the notion of a project - * directory. + * Builds the effective model of the specified POM. * - * @param pomFile The POM file of the project to build the effective model from, must not be {@code null}. - * @param request The model building request that holds further settings, must not be {@code null}. + * @param request The model building request that holds the parameters, must not be {@code null}. * @return The result of the model building, never {@code null}. * @throws ModelBuildingException If the effective model could not be built. */ - ModelBuildingResult build( File pomFile, ModelBuildingRequest request ) - throws ModelBuildingException; - - /** - * Builds the effective model for the specified POM. In contrast to - * {@link #build(File, ModelBuildingRequest, ModelResolver)} the resulting model does not support the notion of a - * project directory. As a consequence, parent POMs are always resolved via the provided model resolver. - * - * @param modelSource The source of the POM, must not be {@code null}. - * @param request The model building request that holds further settings, must not be {@code null}. - * @return The result of the model building, never {@code null}. - * @throws ModelBuildingException If the effective model could not be built. - */ - ModelBuildingResult build( ModelSource modelSource, ModelBuildingRequest request ) + ModelBuildingResult build( ModelBuildingRequest request ) throws ModelBuildingException; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java index 9f52b14e15..20093f793b 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java @@ -19,6 +19,7 @@ package org.apache.maven.model.building; * under the License. */ +import java.io.File; import java.util.Date; import java.util.List; import java.util.Properties; @@ -61,6 +62,43 @@ public interface ModelBuildingRequest */ static final int VALIDATION_LEVEL_STRICT = VALIDATION_LEVEL_MAVEN_3_0; + /** + * Gets the source of the POM to process. + * + * @return The source of the POM or {@code null} if not set. + */ + ModelSource getModelSource(); + + /** + * Sets the source of the POM to process. Eventually, either {@link #setModelSource(ModelSource)} or + * {@link #setPomFile(File)} must be set. + * + * @param modelSource The source of the POM to process, may be {@code null}. + * @return This request, never {@code null}. + */ + ModelBuildingRequest setModelSource( ModelSource modelSource ); + + /** + * Gets the POM file of the project to build. + * + * @return The POM file of the project or {@code null} if not applicable (i.e. when processing a POM from the + * repository). + */ + File getPomFile(); + + /** + * Sets the POM file of the project to build. Note that providing the path to a POM file via this method will make + * the model builder operate in project mode. This mode is meant for effective models that are employed during the + * build process of a local project. Hence the effective model will support the notion of a project directory. To + * build the model for a POM from the repository, use {@link #setModelSource(ModelSource)} in combination with a + * {@link FileModelSource} instead. + * + * @param pomFile The POM file of the project to build the effective model for, may be {@code null} to build the + * model of some POM from the repository. + * @return This request, never {@code null}. + */ + ModelBuildingRequest setPomFile( File pomFile ); + /** * Gets the level of validation to perform on processed models. *