From f6c2684903b9f9e8ad5000b49905881cc8a442b6 Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Thu, 10 Mar 2005 23:01:38 +0000 Subject: [PATCH] o Moved the default plugin stuff to . Plugin config will be pulled from here and injected into as needed to satisfy type-handlers at runtime. o Added the ability for DefaultMaven to load only the super-pom in the event that no pom.xml exists to initiate the assembly. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163527 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/DefaultMaven.java | 12 +- .../MavenInitializingExecutionRequest.java | 3 +- .../lifecycle/DefaultLifecycleExecutor.java | 35 +++- .../project/DefaultMavenProjectBuilder.java | 156 +++++++++++------- .../apache/maven/project/MavenProject.java | 29 ++++ .../maven/project/MavenProjectBuilder.java | 6 + .../DefaultModelInheritanceAssembler.java | 89 ++++++---- .../DefaultModelDefaultsInjector.java | 2 +- .../org/apache/maven/project/pom-4.0.0.xml | 96 +++++------ maven-model/maven.mdo | 22 +-- maven-model/pom.xml | 3 +- maven-user-model/pom.xml | 3 +- 12 files changed, 296 insertions(+), 160 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index e0dc2fe81e..4121c6039b 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -97,7 +97,17 @@ public class DefaultMaven { MavenSession session = createSession( request ); - MavenProject project = getProject( (File) request.getProjectFiles().get( 0 ), request.getLocalRepository() ); + List projectFiles = request.getProjectFiles(); + + MavenProject project = null; + if(projectFiles != null && !projectFiles.isEmpty()) + { + project = getProject( (File) request.getProjectFiles().get( 0 ), request.getLocalRepository() ); + } + else + { + project = projectBuilder.buildSuperProject( request.getLocalRepository() ); + } session.setProject( project ); diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenInitializingExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/MavenInitializingExecutionRequest.java index 085446fdd9..905d8aa362 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenInitializingExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenInitializingExecutionRequest.java @@ -21,6 +21,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.user.UserModel; import org.apache.maven.monitor.event.EventDispatcher; +import java.util.Collections; import java.util.List; /** @@ -39,6 +40,6 @@ extends AbstractMavenExecutionRequest public List getProjectFiles() { - return null; + return Collections.EMPTY_LIST; } } diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java index f8b8c82feb..2da6a0558c 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java @@ -22,6 +22,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.execution.MavenExecutionResponse; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginManagement; import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.monitor.event.MavenEvents; import org.apache.maven.plugin.PluginExecutionResponse; @@ -81,9 +82,11 @@ public class DefaultLifecycleExecutor try { + MavenProject project = session.getProject(); + // TODO: should enrich this with the type handler, but for now just // use "type" as is - ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( session.getProject().getPackaging() ); + ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( project.getPackaging() ); if ( handler != null ) { @@ -96,6 +99,11 @@ public class DefaultLifecycleExecutor } if ( handler.additionalPlugin() != null ) { + String additionalPluginGroupId = "maven"; + String additionalPluginArtifactId = "maven-" + handler.additionalPlugin() + "-plugin"; + + injectHandlerPluginConfiguration( project, additionalPluginGroupId, additionalPluginArtifactId ); + processPluginPhases( "maven", "maven-" + handler.additionalPlugin() + "-plugin", session ); } } @@ -139,6 +147,31 @@ public class DefaultLifecycleExecutor return response; } + private void injectHandlerPluginConfiguration( MavenProject project, String groupId, String artifactId ) + { + PluginManagement mgmt = project.getPluginManagement(); + if( mgmt != null ) + { + List pluginList = mgmt.getPlugins(); + + Plugin handlerPlugin = null; + for ( Iterator it = pluginList.iterator(); it.hasNext(); ) + { + Plugin plugin = (Plugin) it.next(); + if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) ) + { + handlerPlugin = plugin; + break; + } + } + + if ( handlerPlugin != null ) + { + project.addPlugin( handlerPlugin ); + } + } + } + // TODO: don't throw Exception private void processPluginConfiguration( MavenProject project, MavenSession mavenSession ) throws Exception diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 8a991d2937..e520fbdb21 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -31,6 +31,7 @@ import org.apache.maven.model.Repository; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.project.inheritance.ModelInheritanceAssembler; import org.apache.maven.project.injection.ModelDefaultsInjector; +import org.apache.maven.project.interpolation.ModelInterpolationException; import org.apache.maven.project.interpolation.ModelInterpolator; import org.apache.maven.project.path.PathTranslator; import org.apache.maven.project.validation.ModelValidationResult; @@ -118,57 +119,11 @@ public class DefaultMavenProjectBuilder previous = current; } - Model model = modelInterpolator.interpolate( project.getModel() ); - - // interpolation is before injection, because interpolation is off-limits in the injected variables - modelDefaultsInjector.injectDefaults( model ); - - MavenProject parentProject = project.getParent(); - - project = new MavenProject( model ); - project.setFile( projectDescriptor ); - project.setParent( parentProject ); - project.setArtifacts( artifactFactory.createArtifacts( project.getDependencies(), localRepository, null ) ); - - // ---------------------------------------------------------------------- - // Typically when the project builder is being used from maven proper - // the transitive dependencies will not be resolved here because this - // requires a lot of work when we may only be interested in running - // something simple like 'm2 clean'. So the artifact collector is used - // in the dependency resolution phase if it is required by any of the - // goals being executed. But when used as a component in another piece - // of code people may just want to build maven projects and have the - // dependencies resolved for whatever reason: this is why we keep - // this snippet of code here. - // ---------------------------------------------------------------------- - - if ( resolveDependencies ) - { - List repos = buildArtifactRepositories( project.getRepositories() ); - - MavenMetadataSource sourceReader = new MavenMetadataSource( artifactResolver, this ); - - ArtifactResolutionResult result = artifactResolver.resolveTransitively( project.getArtifacts(), repos, - localRepository, sourceReader ); - - project.getArtifacts().addAll( result.getArtifacts().values() ); - } - - ModelValidationResult validationResult = validator.validate( project.getModel() ); - - if ( validationResult.getMessageCount() > 0 ) - { - throw new ProjectBuildingException( "Exception while building project: " + validationResult.toString() ); - } + project = processProjectLogic( project, localRepository, resolveDependencies ); project.setFile( projectDescriptor ); - pathTranslator.alignToBaseDirectory( project.getModel(), projectDescriptor ); - project.addCompileSourceRoot( project.getBuild().getSourceDirectory() ); - project.addScriptSourceRoot( project.getBuild().getScriptSourceDirectory() ); - project.addTestCompileSourceRoot( project.getBuild().getTestSourceDirectory() ); - return project; } catch ( Exception e ) @@ -177,8 +132,61 @@ public class DefaultMavenProjectBuilder } } + private MavenProject processProjectLogic( MavenProject project, ArtifactRepository localRepository, + boolean resolveDependencies ) + throws ProjectBuildingException, ModelInterpolationException, ArtifactResolutionException + { + Model model = modelInterpolator.interpolate( project.getModel() ); + + // interpolation is before injection, because interpolation is off-limits in the injected variables + modelDefaultsInjector.injectDefaults( model ); + + MavenProject parentProject = project.getParent(); + + project = new MavenProject( model ); + project.setParent( parentProject ); + project.setArtifacts( artifactFactory.createArtifacts( project.getDependencies(), localRepository, null ) ); + + // ---------------------------------------------------------------------- + // Typically when the project builder is being used from maven proper + // the transitive dependencies will not be resolved here because this + // requires a lot of work when we may only be interested in running + // something simple like 'm2 clean'. So the artifact collector is used + // in the dependency resolution phase if it is required by any of the + // goals being executed. But when used as a component in another piece + // of code people may just want to build maven projects and have the + // dependencies resolved for whatever reason: this is why we keep + // this snippet of code here. + // ---------------------------------------------------------------------- + + if ( resolveDependencies ) + { + List repos = buildArtifactRepositories( project.getRepositories() ); + + MavenMetadataSource sourceReader = new MavenMetadataSource( artifactResolver, this ); + + ArtifactResolutionResult result = artifactResolver.resolveTransitively( project.getArtifacts(), repos, + localRepository, sourceReader ); + + project.getArtifacts().addAll( result.getArtifacts().values() ); + } + + ModelValidationResult validationResult = validator.validate( project.getModel() ); + + if ( validationResult.getMessageCount() > 0 ) + { + throw new ProjectBuildingException( "Exception while building project: " + validationResult.toString() ); + } + + project.addCompileSourceRoot( project.getBuild().getSourceDirectory() ); + project.addScriptSourceRoot( project.getBuild().getScriptSourceDirectory() ); + project.addTestCompileSourceRoot( project.getBuild().getTestSourceDirectory() ); + + return project; + } + private MavenProject assembleLineage( File projectDescriptor, ArtifactRepository localRepository, - LinkedList lineage, List aggregatedRemoteWagonRepositories ) + LinkedList lineage, List aggregatedRemoteWagonRepositories ) throws ProjectBuildingException { Model model = readModel( projectDescriptor ); @@ -245,8 +253,7 @@ public class DefaultMavenProjectBuilder return repos; } - private Model readModel( File file ) - throws ProjectBuildingException + private Model readModel( File file ) throws ProjectBuildingException { try { @@ -259,12 +266,12 @@ public class DefaultMavenProjectBuilder catch ( Exception e ) { throw new ProjectBuildingException( - "Error while reading model from file '" + file.getAbsolutePath() + "'.", e ); + "Error while reading model from file '" + file.getAbsolutePath() + "'.", + e ); } } - private Model readModel( URL url ) - throws ProjectBuildingException + private Model readModel( URL url ) throws ProjectBuildingException { try { @@ -293,8 +300,8 @@ public class DefaultMavenProjectBuilder catch ( ArtifactResolutionException e ) { // @todo use parent.toString() if modello could generate it, or specify in a code segment - throw new ProjectBuildingException( "Missing parent POM: " + parent.getGroupId() + ":" + - parent.getArtifactId() + "-" + parent.getVersion(), e ); + throw new ProjectBuildingException( "Missing parent POM: " + parent.getGroupId() + ":" + + parent.getArtifactId() + "-" + parent.getVersion(), e ); } return artifact.getFile(); @@ -311,8 +318,7 @@ public class DefaultMavenProjectBuilder *
  • do a topo sort on the graph that remains.
  • * */ - public List getSortedProjects( List projects ) - throws CycleDetectedException + public List getSortedProjects( List projects ) throws CycleDetectedException { DAG dag = new DAG(); @@ -360,15 +366,45 @@ public class DefaultMavenProjectBuilder return sortedProjects; } + public MavenProject buildSuperProject( ArtifactRepository localRepository ) + throws ProjectBuildingException + { + return buildSuperProject( localRepository, false ); + } + + public MavenProject buildSuperProject( ArtifactRepository localRepository, boolean resolveDependencies ) + throws ProjectBuildingException + { + MavenProject project = new MavenProject( getSuperModel() ); + + try + { + project = processProjectLogic( project, localRepository, resolveDependencies ); + + File projectFile = new File( ".", "pom.xml" ); + project.setFile( projectFile ); + pathTranslator.alignToBaseDirectory( project.getModel(), projectFile ); + + return project; + } + catch ( ModelInterpolationException e ) + { + throw new ProjectBuildingException( "Error building super-project", e ); + } + catch ( ArtifactResolutionException e ) + { + throw new ProjectBuildingException( "Error building super-project", e ); + } + } + // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- - private Model getSuperModel() - throws ProjectBuildingException + private Model getSuperModel() throws ProjectBuildingException { URL url = DefaultMavenProjectBuilder.class.getResource( "pom-" + MavenConstants.MAVEN_MODEL_VERSION + ".xml" ); return readModel( url ); } -} +} \ No newline at end of file diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 1525613788..d013d5662a 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -29,6 +29,8 @@ import org.apache.maven.model.License; import org.apache.maven.model.MailingList; import org.apache.maven.model.Model; import org.apache.maven.model.Organization; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginManagement; import org.apache.maven.model.Reports; import org.apache.maven.model.Scm; @@ -522,5 +524,32 @@ public class MavenProject } return model.getBuild().getPlugins(); } + + public PluginManagement getPluginManagement() + { + PluginManagement pluginMgmt = null; + + Build build = model.getBuild(); + if ( build != null ) + { + pluginMgmt = build.getPluginManagement(); + } + + return pluginMgmt; + } + + public void addPlugin( Plugin plugin ) + { + Build build = model.getBuild(); + + if ( build == null ) + { + build = new Build(); + + model.setBuild( build ); + } + + build.addPlugin( plugin ); + } } diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java index 454d35a9a3..a8d03804a8 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java @@ -33,6 +33,12 @@ public interface MavenProjectBuilder MavenProject build( File project, ArtifactRepository localRepository, boolean transitive ) throws ProjectBuildingException; + MavenProject buildSuperProject( ArtifactRepository localRepository ) + throws ProjectBuildingException; + + MavenProject buildSuperProject( ArtifactRepository localRepository, boolean transitive ) + throws ProjectBuildingException; + // take this out List getSortedProjects( List projects ) diff --git a/maven-core/src/main/java/org/apache/maven/project/inheritance/DefaultModelInheritanceAssembler.java b/maven-core/src/main/java/org/apache/maven/project/inheritance/DefaultModelInheritanceAssembler.java index f0dc87d467..03e7b1549e 100644 --- a/maven-core/src/main/java/org/apache/maven/project/inheritance/DefaultModelInheritanceAssembler.java +++ b/maven-core/src/main/java/org/apache/maven/project/inheritance/DefaultModelInheritanceAssembler.java @@ -219,66 +219,83 @@ public class DefaultModelInheritanceAssembler private void assemblePluginManagementInheritance( Model child, Model parent ) { - PluginManagement parentPluginMgmt = parent.getPluginManagement(); + Build parentBuild = parent.getBuild(); + Build childBuild = parent.getBuild(); - PluginManagement childPluginMgmt = child.getPluginManagement(); - - if ( parentPluginMgmt != null ) + if ( childBuild == null ) { - if ( childPluginMgmt == null ) + if ( parentBuild != null ) { - child.setPluginManagement( parentPluginMgmt ); + child.setBuild( parentBuild ); } else { - List childPlugins = childPluginMgmt.getPlugins(); + childBuild = new Build(); + } + } + else + { + PluginManagement parentPluginMgmt = parentBuild.getPluginManagement(); - Map mappedChildPlugins = new TreeMap(); - for ( Iterator it = childPlugins.iterator(); it.hasNext(); ) + PluginManagement childPluginMgmt = childBuild.getPluginManagement(); + + if ( parentPluginMgmt != null ) + { + if ( childPluginMgmt == null ) { - Plugin plugin = (Plugin) it.next(); - mappedChildPlugins.put( constructPluginKey( plugin ), plugin ); + childBuild.setPluginManagement( parentPluginMgmt ); } - - for ( Iterator it = parentPluginMgmt.getPlugins().iterator(); it.hasNext(); ) + else { - Plugin plugin = (Plugin) it.next(); - if ( !mappedChildPlugins.containsKey( constructPluginKey( plugin ) ) ) + List childPlugins = childPluginMgmt.getPlugins(); + + Map mappedChildPlugins = new TreeMap(); + for ( Iterator it = childPlugins.iterator(); it.hasNext(); ) { - childPluginMgmt.addPlugin( plugin ); + Plugin plugin = (Plugin) it.next(); + mappedChildPlugins.put( constructPluginKey( plugin ), plugin ); } - else + + for ( Iterator it = parentPluginMgmt.getPlugins().iterator(); it.hasNext(); ) { - Plugin childPlugin = (Plugin) mappedChildPlugins.get( constructPluginKey( plugin ) ); - - Map mappedChildGoals = new TreeMap(); - for ( Iterator itGoals = childPlugin.getGoals().iterator(); itGoals.hasNext(); ) + Plugin plugin = (Plugin) it.next(); + if ( !mappedChildPlugins.containsKey( constructPluginKey( plugin ) ) ) { - Goal goal = (Goal) itGoals.next(); - mappedChildGoals.put( goal.getId(), goal ); + childPluginMgmt.addPlugin( plugin ); } - - for ( Iterator itGoals = plugin.getGoals().iterator(); itGoals.hasNext(); ) + else { - Goal parentGoal = (Goal) itGoals.next(); - Goal childGoal = (Goal) mappedChildGoals.get( parentGoal.getId() ); + Plugin childPlugin = (Plugin) mappedChildPlugins.get( constructPluginKey( plugin ) ); - if ( childGoal == null ) + Map mappedChildGoals = new TreeMap(); + for ( Iterator itGoals = childPlugin.getGoals().iterator(); itGoals.hasNext(); ) { - childPlugin.addGoal( parentGoal ); + Goal goal = (Goal) itGoals.next(); + mappedChildGoals.put( goal.getId(), goal ); } - else + + for ( Iterator itGoals = plugin.getGoals().iterator(); itGoals.hasNext(); ) { - Boolean disabled = childGoal.isDisabled(); - if ( disabled == null ) + Goal parentGoal = (Goal) itGoals.next(); + Goal childGoal = (Goal) mappedChildGoals.get( parentGoal.getId() ); + + if ( childGoal == null ) { - childGoal.setDisabled( parentGoal.isDisabled() ); + childPlugin.addGoal( parentGoal ); + } + else + { + Boolean disabled = childGoal.isDisabled(); + if ( disabled == null ) + { + childGoal.setDisabled( parentGoal.isDisabled() ); - Properties conf = new Properties( childGoal.getConfiguration() ); + Properties conf = new Properties( childGoal.getConfiguration() ); - conf.putAll( parentGoal.getConfiguration() ); + conf.putAll( parentGoal.getConfiguration() ); - childGoal.setConfiguration( conf ); + childGoal.setConfiguration( conf ); + } } } } diff --git a/maven-core/src/main/java/org/apache/maven/project/injection/DefaultModelDefaultsInjector.java b/maven-core/src/main/java/org/apache/maven/project/injection/DefaultModelDefaultsInjector.java index f14f0abf82..078c946c67 100644 --- a/maven-core/src/main/java/org/apache/maven/project/injection/DefaultModelDefaultsInjector.java +++ b/maven-core/src/main/java/org/apache/maven/project/injection/DefaultModelDefaultsInjector.java @@ -42,7 +42,7 @@ public class DefaultModelDefaultsInjector injectDependencyDefaults( model.getDependencies(), model.getDependencyManagement() ); if ( model.getBuild() != null ) { - injectPluginDefaults( model.getBuild().getPlugins(), model.getPluginManagement() ); + injectPluginDefaults( model.getBuild().getPlugins(), model.getBuild().getPluginManagement() ); } } diff --git a/maven-core/src/main/resources/org/apache/maven/project/pom-4.0.0.xml b/maven-core/src/main/resources/org/apache/maven/project/pom-4.0.0.xml index ec1b735fd7..327c7661db 100644 --- a/maven-core/src/main/resources/org/apache/maven/project/pom-4.0.0.xml +++ b/maven-core/src/main/resources/org/apache/maven/project/pom-4.0.0.xml @@ -30,53 +30,55 @@ - - - maven - maven-compiler-plugin - 1.0-SNAPSHOT - - - maven - maven-resources-plugin - 1.0-SNAPSHOT - - - maven - maven-jar-plugin - 1.0-SNAPSHOT - - - maven - maven-surefire-plugin - 1.0-SNAPSHOT - - - maven - maven-clean-plugin - 1.0-SNAPSHOT - - - maven - maven-deploy-plugin - 1.0-SNAPSHOT - - - maven - maven-install-plugin - 1.0-SNAPSHOT - - - maven - maven-pom-plugin - 1.0-SNAPSHOT - - - maven - maven-plugin-plugin - 1.0-SNAPSHOT - - + + + + maven + maven-compiler-plugin + 1.0-SNAPSHOT + + + maven + maven-resources-plugin + 1.0-SNAPSHOT + + + maven + maven-jar-plugin + 1.0-SNAPSHOT + + + maven + maven-surefire-plugin + 1.0-SNAPSHOT + + + maven + maven-clean-plugin + 1.0-SNAPSHOT + + + maven + maven-deploy-plugin + 1.0-SNAPSHOT + + + maven + maven-install-plugin + 1.0-SNAPSHOT + + + maven + maven-pom-plugin + 1.0-SNAPSHOT + + + maven + maven-plugin-plugin + 1.0-SNAPSHOT + + + diff --git a/maven-model/maven.mdo b/maven-model/maven.mdo index 106e9efd1f..1428a726f5 100644 --- a/maven-model/maven.mdo +++ b/maven-model/maven.mdo @@ -413,17 +413,6 @@ DependencyManagement - - - pluginManagement - 4.0.0 - false - - - PluginManagement - - properties 3.0.0 @@ -682,6 +671,17 @@ * + + + pluginManagement + 4.0.0 + false + + + PluginManagement + + diff --git a/maven-model/pom.xml b/maven-model/pom.xml index b291fc6647..7674d0c4dc 100644 --- a/maven-model/pom.xml +++ b/maven-model/pom.xml @@ -16,6 +16,7 @@ maven maven-modello-plugin + 1.0-SNAPSHOT 4.0.0 maven.mdo @@ -23,4 +24,4 @@
    - \ No newline at end of file + diff --git a/maven-user-model/pom.xml b/maven-user-model/pom.xml index dc945a84d7..e90e039d75 100644 --- a/maven-user-model/pom.xml +++ b/maven-user-model/pom.xml @@ -12,6 +12,7 @@ maven maven-modello-plugin + 1.0-SNAPSHOT 4.0.0 maven-user.mdo @@ -19,4 +20,4 @@ - \ No newline at end of file +