From 8e004afe5f2f9900c8916cfe1c2440e02789f1b9 Mon Sep 17 00:00:00 2001 From: Britton Isbell Date: Tue, 31 Mar 2009 21:07:56 +0000 Subject: [PATCH] Fixed settings profile activation for maven plugins. Cleaned up project builder. Unit Tests. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@760635 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/plugin/DefaultPluginManager.java | 10 +- .../PomConstructionWithSettingsTest.java | 14 +- .../pom.xml | 78 ++++++++++ .../settings.xml | 35 +++++ .../processor/ProfilePropertiesProcessor.java | 14 +- .../project/DefaultMavenProjectBuilder.java | 142 +++++++++--------- .../maven/project/MavenProjectBuilder.java | 3 + .../maven/project/PomConstructionTest.java | 12 +- .../properties-no-duplication/pom.xml | 11 ++ .../properties-no-duplication/sub/pom.xml | 17 +++ 10 files changed, 257 insertions(+), 79 deletions(-) create mode 100644 maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/pom.xml create mode 100644 maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/settings.xml create mode 100644 maven-project/src/test/resources-project-builder/properties-no-duplication/pom.xml create mode 100644 maven-project/src/test/resources-project-builder/properties-no-duplication/sub/pom.xml diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java index 4672f12a79..f9a3ca58d2 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java @@ -379,7 +379,6 @@ public class DefaultPluginManager try { Artifact pluginPomArtifact = repositorySystem.createProjectArtifact( pluginArtifact.getGroupId(), pluginArtifact.getArtifactId(), pluginArtifact.getVersion() ); - // This does not populate the artifacts of the dependenct projects MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginPomArtifact, project.getRemoteArtifactRepositories(), localRepository ); @@ -1847,7 +1846,6 @@ public class DefaultPluginManager try { artifact = repositorySystem.createProjectArtifact( groupId, artifactId, artifactVersion ); - pluginProject = mavenProjectBuilder.buildFromRepository( artifact, project.getRemoteArtifactRepositories(), localRepository ); } catch ( ProjectBuildingException e ) @@ -1875,10 +1873,10 @@ public class DefaultPluginManager throws PluginManagerException, InvalidPluginException, PluginVersionResolutionException, ArtifactResolutionException, ArtifactNotFoundException { logger.debug( "Resolving plugin artifact " + plugin.getKey() + " from " + project.getRemoteArtifactRepositories() ); - + ArtifactRepository localRepository = session.getLocalRepository(); - MavenProject pluginProject = buildPluginProject( plugin, localRepository, project.getRemoteArtifactRepositories() ); + MavenProject pluginProject = buildPluginProject( plugin, localRepository, session ); Artifact pluginArtifact = repositorySystem.createPluginArtifact( plugin ); @@ -1897,13 +1895,13 @@ public class DefaultPluginManager return pluginArtifact; } - public MavenProject buildPluginProject( Plugin plugin, ArtifactRepository localRepository, List remoteRepositories ) + public MavenProject buildPluginProject( Plugin plugin, ArtifactRepository localRepository, MavenSession session ) throws InvalidPluginException { Artifact artifact = repositorySystem.createProjectArtifact( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); try { - MavenProject p = mavenProjectBuilder.buildFromRepository( artifact, remoteRepositories, localRepository ); + MavenProject p = mavenProjectBuilder.buildFromRepository(artifact, session.getProjectBuilderConfiguration()); return p; } diff --git a/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java b/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java index 8b239ab96d..36a8d30763 100644 --- a/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java +++ b/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java @@ -45,7 +45,19 @@ public class PomConstructionWithSettingsTest PomTestWrapper pom = buildPom( "settings-no-pom" ); assertEquals( "local-profile-prop-value", pom.getValue( "properties/local-profile-prop" ) ); } - + + /**MNG-4107 */ + /* + public void testPomAndSettingsInterpolation() throws Exception + { + PomTestWrapper pom = buildPom( "test-pom-and-settings-interpolation" ); + System.out.println(pom.getDomainModel().asString()); + assertEquals("applied", pom.getValue( "properties/settingsProfile" ) ); + assertEquals("applied", pom.getValue( "properties/pomProfile" ) ); + assertEquals("settings", pom.getValue( "properties/pomVsSettings" ) ); + assertEquals("settings", pom.getValue( "properties/pomVsSettingsInterpolated" ) ); + } +*/ private PomTestWrapper buildPom( String pomPath ) throws Exception { diff --git a/maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/pom.xml b/maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/pom.xml new file mode 100644 index 0000000000..4d2f61d961 --- /dev/null +++ b/maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/pom.xml @@ -0,0 +1,78 @@ + + + + + + 4.0.0 + + org.apache.maven.its.mng4107 + test + 1.0-SNAPSHOT + jar + + Maven Integration Test :: MNG-4107 + + Test that POM interpolation uses the property values from the dominant profile source (POM vs. profiles.xml + vs. settings.xml). This boils down to the proper order of profile injection and interpolation, i.e. + interpolate after profiles from all sources are injected. + + + + + ${pomVsSettings} + + + + + pom + + true + + + applied + pom + + + + + + + + org.apache.maven.its.plugins + maven-it-plugin-expression + 2.1-SNAPSHOT + + + validate + + eval + + + target/pom.properties + + project/properties + + + + + + + + diff --git a/maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/settings.xml b/maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/settings.xml new file mode 100644 index 0000000000..2d42d495c5 --- /dev/null +++ b/maven-core/src/test/resources-settings/test-pom-and-settings-interpolation/settings.xml @@ -0,0 +1,35 @@ + + + + + + + + settings + + true + + + applied + settings + + + + diff --git a/maven-project-builder/src/main/java/org/apache/maven/project/processor/ProfilePropertiesProcessor.java b/maven-project-builder/src/main/java/org/apache/maven/project/processor/ProfilePropertiesProcessor.java index 6a2f928569..4a8a55bebb 100644 --- a/maven-project-builder/src/main/java/org/apache/maven/project/processor/ProfilePropertiesProcessor.java +++ b/maven-project-builder/src/main/java/org/apache/maven/project/processor/ProfilePropertiesProcessor.java @@ -13,7 +13,7 @@ public class ProfilePropertiesProcessor Model t = (Model) target, c = (Model) child, p = (Model) parent; Properties properties = new Properties(); - + if ( c.getProperties() != null ) { properties.putAll( c.getProperties() ); @@ -32,9 +32,21 @@ public class ProfilePropertiesProcessor } else { + //add(properties, t.getProperties()); t.getProperties().putAll( properties ); } } } + + private static void add(Properties source, Properties target) + { + for(Object key : source.keySet()) + { + if(!target.containsKey(key)) + { + target.put(key, source.get(key)); + } + } + } } diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 161df8c6bf..ce84161eea 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -26,6 +26,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; @@ -128,7 +129,12 @@ public class DefaultMavenProjectBuilder public MavenProject build( File pomFile, ProjectBuilderConfiguration configuration ) throws ProjectBuildingException { - MavenProject project = readModelFromLocalPath( "unknown", pomFile, configuration.getLocalRepository(), configuration.getRemoteRepositories(), configuration ); + MavenProject project; + try { + project = buildWithoutProfiles( "unknown", pomFile, configuration.getLocalRepository(), configuration.getRemoteRepositories(), configuration ); + } catch (IOException e) { + throw new ProjectBuildingException("", "", e); + } project.setFile( pomFile ); project = buildWithProfiles( project.getModel(), configuration, pomFile, project.getParentFile() ); @@ -161,10 +167,11 @@ public class DefaultMavenProjectBuilder return buildFromRepository( pomArtifact, remoteArtifactRepositories, localRepository ); } - - public MavenProject buildFromRepository( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) - throws ProjectBuildingException + + public MavenProject buildFromRepository(Artifact artifact, ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException { + MavenProject project = hm.get( artifact.getId() ); if ( project != null ) @@ -172,7 +179,7 @@ public class DefaultMavenProjectBuilder return project; } - ArtifactResolutionRequest request = new ArtifactResolutionRequest( artifact, localRepository, remoteRepositories ); + ArtifactResolutionRequest request = new ArtifactResolutionRequest( artifact, configuration.getLocalRepository(), configuration.getRemoteRepositories() ); ArtifactResolutionResult result = repositorySystem.resolve( request ); try @@ -183,19 +190,30 @@ public class DefaultMavenProjectBuilder { throw new ProjectBuildingException( artifact.getId(), "Error resolving project artifact.", e ); } - //Won't know anything about settings profiles in this path - ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration() - .setLocalRepository( localRepository ) - .setRemoteRepositories( remoteRepositories ); - project = readModelFromLocalPath( "unknown", artifact.getFile(), config.getLocalRepository(), remoteRepositories, config ); - project = buildWithProfiles( project.getModel(), config, artifact.getFile(), project.getParentFile() ); + try { + project = buildWithoutProfiles( "unknown", artifact.getFile(), configuration.getLocalRepository(), + configuration.getRemoteRepositories(), configuration ); + } catch (IOException e) { + throw new ProjectBuildingException(artifact.getId(), "Error reading project artifact.", e); + } + project = buildWithProfiles( project.getModel(), configuration, artifact.getFile(), project.getParentFile() ); + artifact.setFile( artifact.getFile() ); project.setVersion( artifact.getVersion() ); hm.put( artifact.getId(), project ); - return project; + return project; + } + + public MavenProject buildFromRepository( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) + throws ProjectBuildingException + { + ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration() + .setLocalRepository( localRepository ) + .setRemoteRepositories(remoteRepositories); + return buildFromRepository(artifact, configuration); } /** @@ -252,6 +270,7 @@ public class DefaultMavenProjectBuilder .setRemoteRepostories( project.getRemoteArtifactRepositories() ) .setManagedVersionMap( project.getManagedVersionMap() ); + if(request.getRemoteRepostories() == null) { request.setRemoteRepostories( new ArrayList() ); @@ -285,9 +304,11 @@ public class DefaultMavenProjectBuilder if(externalProfileManager != null) { + //System.out.println("PROFILES = " + externalProfileManager.getProfilesById().toString()); + try { - projectProfiles.addAll( externalProfileManager.getActiveProfiles( model ) ); + projectProfiles.addAll( externalProfileManager.getActiveProfiles( null ) ); } catch ( ProfileActivationException e ) { @@ -301,7 +322,7 @@ public class DefaultMavenProjectBuilder //System.out.println("PROFILE POM: COUNT = " + model.getProfiles().size()); try { - //System.out.println("PROFILE POM - ACTIVE: COUNT = " + profileManager.getActiveProfiles( model ).size()); + //System.out.println("PROFILE POM - ACTIVE: COUNT = " + profileManager.getActiveProfiles( model ).size() +"," + projectProfiles.size()); projectProfiles.addAll( profileManager.getActiveProfiles( model ) ); } catch ( ProfileActivationException e ) @@ -309,14 +330,21 @@ public class DefaultMavenProjectBuilder throw new ProjectBuildingException( projectId, "Failed to activate pom profiles.", projectDescriptor, e ); } + + List interpolatorProperties = new ArrayList(); + interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( config.getExecutionProperties(), PomInterpolatorTag.EXECUTION_PROPERTIES.name() ) ); + interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( config.getUserProperties(), PomInterpolatorTag.USER_PROPERTIES.name() ) ); - if(!projectProfiles.isEmpty()) - { + if ( config.getBuildStartTime() != null ) + { + interpolatorProperties.add( new InterpolatorProperty( "${build.timestamp}", new SimpleDateFormat( "yyyyMMdd-hhmm" ).format( config.getBuildStartTime() ), + PomInterpolatorTag.PROJECT_PROPERTIES.name() ) ); + } try { PomClassicDomainModel dm = ProcessorContext.mergeProfilesIntoModel( projectProfiles, model, false ); ProcessorContext.interpolateModelProperties( dm.getModelProperties(), - new ArrayList(), dm ); + interpolatorProperties, dm ); dm = new PomClassicDomainModel( dm.getModelProperties(), false ); model = dm.getModel(); } @@ -324,9 +352,11 @@ public class DefaultMavenProjectBuilder { throw new ProjectBuildingException(projectId, "", projectDescriptor, e); - } - } - + } + + // } + + MavenProject project; try @@ -351,33 +381,32 @@ public class DefaultMavenProjectBuilder return project; } - private MavenProject readModelFromLocalPath( String projectId, File pomFile, ArtifactRepository localRepository, List remoteRepositories, ProjectBuilderConfiguration config ) - throws ProjectBuildingException - { - List interpolatorProperties = new ArrayList(); - - interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( config.getExecutionProperties(), PomInterpolatorTag.EXECUTION_PROPERTIES.name() ) ); - - interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( config.getUserProperties(), PomInterpolatorTag.USER_PROPERTIES.name() ) ); - - if ( config.getBuildStartTime() != null ) + private MavenProject buildWithoutProfiles( String projectId, File pomFile, ArtifactRepository localRepository, + List remoteRepositories, ProjectBuilderConfiguration projectBuilderConfiguration ) + throws ProjectBuildingException, IOException { - interpolatorProperties.add( new InterpolatorProperty( "${build.timestamp}", new SimpleDateFormat( "yyyyMMdd-hhmm" ).format( config.getBuildStartTime() ), - PomInterpolatorTag.PROJECT_PROPERTIES.name() ) ); - } - MavenProject mavenProject; + List activeProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration.getGlobalProfileManager() + .getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList(); + + List inactiveProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration + .getGlobalProfileManager().getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds() + : new ArrayList(); + + PomClassicDomainModel domainModel = buildModel( pomFile, new ArrayList(), activeProfileIds, inactiveProfileIds, localRepository, remoteRepositories ); try { - mavenProject = buildFromLocalPath( pomFile, interpolatorProperties, localRepository, remoteRepositories, config, this ); - } - catch ( IOException e ) - { - throw new ProjectBuildingException( projectId, "File = " + pomFile.getAbsolutePath(), e ); - } + MavenProject mavenProject = new MavenProject( convertFromInputStreamToModel( domainModel.getInputStream() ), repositorySystem, this, projectBuilderConfiguration ); - return mavenProject; + mavenProject.setParentFile( domainModel.getParentFile() ); + + return mavenProject; + } + catch ( InvalidRepositoryException e ) + { + throw new IOException( e.getMessage() ); + } } @@ -520,7 +549,8 @@ public class DefaultMavenProjectBuilder else { profileModels.add( dm ); - } + } + } PomClassicDomainModel transformedDomainModel = ProcessorContext.build( profileModels, properties ); @@ -556,34 +586,6 @@ public class DefaultMavenProjectBuilder return new PomClassicDomainModel( new ByteArrayInputStream( baos.toByteArray() ), isMostSpecialized ); } - protected MavenProject buildFromLocalPath( File pom, Collection interpolatorProperties, ArtifactRepository localRepository, List remoteRepositories, - ProjectBuilderConfiguration projectBuilderConfiguration, MavenProjectBuilder mavenProjectBuilder ) - throws IOException - { - - List activeProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration.getGlobalProfileManager() - .getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList(); - - List inactiveProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration - .getGlobalProfileManager().getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds() - : new ArrayList(); - - PomClassicDomainModel domainModel = buildModel( pom, interpolatorProperties, activeProfileIds, inactiveProfileIds, localRepository, remoteRepositories ); - - try - { - MavenProject mavenProject = new MavenProject( convertFromInputStreamToModel( domainModel.getInputStream() ), repositorySystem, mavenProjectBuilder, projectBuilderConfiguration ); - - mavenProject.setParentFile( domainModel.getParentFile() ); - - return mavenProject; - } - catch ( InvalidRepositoryException e ) - { - throw new IOException( e.getMessage() ); - } - } - private static Model convertFromInputStreamToModel( InputStream inputStream ) throws IOException { diff --git a/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java index c908e3d4e1..effa4db409 100644 --- a/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/MavenProjectBuilder.java @@ -40,6 +40,9 @@ public interface MavenProjectBuilder MavenProject buildFromRepository( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) throws ProjectBuildingException; + + MavenProject buildFromRepository(Artifact artifact, ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException; MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration configuration ) throws ProjectBuildingException; diff --git a/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java b/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java index 0ef63c298e..09d1a9f9ea 100644 --- a/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Properties; import org.apache.maven.artifact.repository.DefaultArtifactRepository; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; @@ -218,6 +219,7 @@ public class PomConstructionTest throws Exception { PomTestWrapper pom = buildPom( "plugin-management-dependencies/sub", "test" ); + System.out.println(pom.getDomainModel().asString()); assertEquals( "1.0-alpha-21", pom.getValue( "build/plugins[1]/version" ) ); assertEquals( "1.0", pom.getValue( "build/plugins[1]/dependencies[1]/version" ) ); } @@ -1016,6 +1018,14 @@ public class PomConstructionTest assertEquals("true", pom.getValue( "reporting/plugins[1]/configuration/booleanParam")); } + public void testPropertiesNoDuplication() + throws Exception + { + PomTestWrapper pom = buildPom( "properties-no-duplication/sub" ); + assertEquals(1, ( (Properties) pom.getValue( "properties" ) ).size()); + assertEquals("child", pom.getValue( "properties/pomProfile" ) ); + } + public void testCompleteModelWithoutParent() throws Exception { @@ -1237,7 +1247,7 @@ public class PomConstructionTest { PomTestWrapper pom = buildPom( "profile-injection-order", "pom-a", "pom-b", "pom-e", "pom-c", "pom-d" ); - + System.out.println(pom.getDomainModel().asString()); assertEquals( "e", pom.getValue( "properties[1]/pomProperty" ) ); } diff --git a/maven-project/src/test/resources-project-builder/properties-no-duplication/pom.xml b/maven-project/src/test/resources-project-builder/properties-no-duplication/pom.xml new file mode 100644 index 0000000000..ebdb8b1cc1 --- /dev/null +++ b/maven-project/src/test/resources-project-builder/properties-no-duplication/pom.xml @@ -0,0 +1,11 @@ + + + 4.0.0 + org.apache.maven.its + test-parent + 1.0-SNAPSHOT + + parent + + diff --git a/maven-project/src/test/resources-project-builder/properties-no-duplication/sub/pom.xml b/maven-project/src/test/resources-project-builder/properties-no-duplication/sub/pom.xml new file mode 100644 index 0000000000..9241b8bb76 --- /dev/null +++ b/maven-project/src/test/resources-project-builder/properties-no-duplication/sub/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.apache.maven.its + test-parent + 1.0-SNAPSHOT + + org.apache.maven.its + test + 1.0-SNAPSHOT + + child + + +