Skip to content

Commit

Permalink
Issue mojohaus#91 :
Browse files Browse the repository at this point in the history
Projet properties should be a merge of all active profiles : activated
explicitly with -P, with property activation or by default
  • Loading branch information
lafoletc committed Nov 14, 2020
1 parent b57c507 commit 85a96b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
11 changes: 11 additions & 0 deletions src/it/projects/profile-with-dependencies-active/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>myprofile</id>
Expand All @@ -29,6 +37,9 @@
<version>1.1</version>
</dependency>
</dependencies>
<properties>
<junit.version>4.10</junit.version>
</properties>
</profile>
</profiles>
</project>
13 changes: 10 additions & 3 deletions src/it/projects/profile-with-dependencies-active/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ File originalPom = new File( basedir, 'pom.xml' )
assert originalPom.exists()

def originalProject = new XmlSlurper().parse( originalPom )
assert 0 == originalProject.dependencies.size()
assert 1 == originalProject.dependencies.size()
assert 'myprofile' == originalProject.profiles.profile.id.text()
assert 1 == originalProject.profiles.profile.dependencies.size()

File flattendPom = new File( basedir, '.flattened-pom.xml' )
assert flattendPom.exists()

def flattendProject = new XmlSlurper().parse( flattendPom )

assert 1 == flattendProject.dependencies.size()
assert 'dep' == flattendProject.dependencies.dependency.artifactId.text()
assert '1.1' == flattendProject.dependencies.dependency.version.text()
assert 2 == flattendProject.dependencies.dependency.size()

assert 'junit' == flattendProject.dependencies.dependency[0].artifactId.text()
assert '4.10' == flattendProject.dependencies.dependency[0].version.text()

assert 'dep' == flattendProject.dependencies.dependency[1].artifactId.text()
assert '1.1' == flattendProject.dependencies.dependency[1].version.text()

assert 0 == flattendProject.profiles.profile.size()
37 changes: 8 additions & 29 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.interpolation.ModelInterpolator;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.DefaultProfileSelector;
import org.apache.maven.model.profile.ProfileInjector;
import org.apache.maven.model.profile.ProfileSelector;
import org.apache.maven.plugin.MojoExecution;
Expand Down Expand Up @@ -866,38 +866,16 @@ protected Model createEffectivePom( ModelBuildingRequest buildingRequest,
{
ProfileInjector customInjector = new ProfileInjector()
{
public void injectProfile( Model model, Profile profile, ModelBuildingRequest request,
public void injectProfile( Model model, Profile activeProfile, ModelBuildingRequest request,
ModelProblemCollector problems )
{
List<String> activeProfileIds = request.getActiveProfileIds();
if ( activeProfileIds.contains( profile.getId() ) )
{
Properties merged = new Properties();
merged.putAll( model.getProperties() );
merged.putAll( profile.getProperties() );
model.setProperties( merged );
}
}
};
ProfileSelector customSelector = new ProfileSelector()
{
public List<Profile> getActiveProfiles( Collection<Profile> profiles, ProfileActivationContext context,
ModelProblemCollector problems )
{
List<Profile> activeProfiles = new ArrayList<Profile>( profiles.size() );

for ( Profile profile : profiles )
{
Activation activation = profile.getActivation();
if ( !embedBuildProfileDependencies || isBuildTimeDriven( activation ) )
{
activeProfiles.add( profile );
}
}

return activeProfiles;
// Note : only active profiles can be merged, not "isBuildTimeDriven" ones
model.getProperties().putAll(activeProfile.getProperties());
}
};

// Note : ProfileActivators can be added if necessary
ProfileSelector customSelector = new DefaultProfileSelector();

buildingResult = modelBuilderThreadSafetyWorkaround.build( buildingRequest, customInjector, customSelector );
}
Expand Down Expand Up @@ -1269,3 +1247,4 @@ public void startElement( String uri, String localName, String qName, Attributes
}

}

0 comments on commit 85a96b8

Please sign in to comment.