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
This commit is contained in:
Britton Isbell 2009-03-31 21:07:56 +00:00
parent 30053a1000
commit 8e004afe5f
10 changed files with 257 additions and 79 deletions

View File

@ -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<ArtifactRepository> 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;
}

View File

@ -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
{

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.mng4107</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Integration Test :: MNG-4107</name>
<description>
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.
</description>
<properties>
<!-- collect the effective property values as derived by interpolation -->
<pomVsSettingsInterpolated>${pomVsSettings}</pomVsSettingsInterpolated>
</properties>
<profiles>
<profile>
<id>pom</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pomProfile>applied</pomProfile>
<pomVsSettings>pom</pomVsSettings>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.its.plugins</groupId>
<artifactId>maven-it-plugin-expression</artifactId>
<version>2.1-SNAPSHOT</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>eval</goal>
</goals>
<configuration>
<outputFile>target/pom.properties</outputFile>
<expressions>
<expression>project/properties</expression>
</expressions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<settings>
<profiles>
<profile>
<id>settings</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<settingsProfile>applied</settingsProfile>
<pomVsSettings>settings</pomVsSettings>
</properties>
</profile>
</profiles>
</settings>

View File

@ -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));
}
}
}
}

View File

@ -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<ArtifactRepository> 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<ArtifactRepository> 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<ArtifactRepository>() );
@ -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<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
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<InterpolatorProperty>(), 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<ArtifactRepository> remoteRepositories, ProjectBuilderConfiguration config )
throws ProjectBuildingException
{
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
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<ArtifactRepository> 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<String> activeProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration.getGlobalProfileManager()
.getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList<String>();
List<String> inactiveProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration
.getGlobalProfileManager().getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds()
: new ArrayList<String>();
PomClassicDomainModel domainModel = buildModel( pomFile, new ArrayList<InterpolatorProperty>(), 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<InterpolatorProperty> interpolatorProperties, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories,
ProjectBuilderConfiguration projectBuilderConfiguration, MavenProjectBuilder mavenProjectBuilder )
throws IOException
{
List<String> activeProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration.getGlobalProfileManager()
.getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyActiveProfileIds() : new ArrayList<String>();
List<String> inactiveProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration
.getGlobalProfileManager().getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds()
: new ArrayList<String>();
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
{

View File

@ -40,6 +40,9 @@ public interface MavenProjectBuilder
MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository )
throws ProjectBuildingException;
MavenProject buildFromRepository(Artifact artifact, ProjectBuilderConfiguration configuration )
throws ProjectBuildingException;
MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration configuration )
throws ProjectBuildingException;

View File

@ -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" ) );
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<pomProfile>parent</pomProfile>
</properties>
</project>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.its</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>org.apache.maven.its</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<pomProfile>child</pomProfile>
</properties>
</project>