Skip to content

Commit

Permalink
mojohaus#320 Fix importing dependency management in profiles from BOM…
Browse files Browse the repository at this point in the history
… imports
  • Loading branch information
Syquel committed Oct 12, 2022
1 parent c613968 commit 988f260
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 20 deletions.
41 changes: 41 additions & 0 deletions src/it/projects/profile-with-depMgmt-transitive/pom.xml
@@ -0,0 +1,41 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>profile-with-depMgmt-transitive</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<!-- This BOM contains a profile 'community-release' which is activated by default -->
<!-- This profile provides dependency management for the artifact 'org.infinispan:infinispan-marshaller-kryo' -->
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-bom</artifactId>
<version>13.0.11.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-marshaller-kryo</artifactId>
</dependency>
</dependencies>

</project>
31 changes: 31 additions & 0 deletions src/it/projects/profile-with-depMgmt-transitive/verify.groovy
@@ -0,0 +1,31 @@
/*
* 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.
*/
File originalPom = new File( basedir, 'pom.xml' )
assert originalPom.exists()

def originalProject = new XmlSlurper().parse( originalPom )
assert 1 == originalProject.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 '13.0.11.Final' == flattendProject.dependencies.dependency[0].version.text()

63 changes: 43 additions & 20 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Expand Up @@ -34,9 +34,11 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
Expand All @@ -48,6 +50,7 @@
import org.apache.maven.model.Activation;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
Expand All @@ -61,8 +64,8 @@
import org.apache.maven.model.interpolation.ModelInterpolator;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
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;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -938,33 +941,53 @@ protected Model createEffectivePom( ModelBuildingRequest buildingRequest,
{
ProfileInjector customInjector = ( model, profile, request, 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 );

// Copied from org.apache.maven.model.profile.DefaultProfileInjector
DependencyManagement profileDependencyManagement = profile.getDependencyManagement();
if ( profileDependencyManagement != null )
{
Properties merged = new Properties();
merged.putAll( model.getProperties() );
merged.putAll( profile.getProperties() );
model.setProperties( merged );
}
};
ProfileSelector customSelector = ( profiles, context, problems ) ->
{
List<Profile> activeProfiles = new ArrayList<>( profiles.size() );
DependencyManagement modelDependencyManagement = model.getDependencyManagement();
if ( modelDependencyManagement == null )
{
modelDependencyManagement = new DependencyManagement();
model.setDependencyManagement( modelDependencyManagement );
}

for ( Profile profile : profiles )
{
Activation activation = profile.getActivation();
if ( !embedBuildProfileDependencies || isBuildTimeDriven( activation ) )
List<Dependency> src = profileDependencyManagement.getDependencies();
if ( !src.isEmpty() )
{
activeProfiles.add( profile );
List<Dependency> tgt = modelDependencyManagement.getDependencies();
Map<Object, Dependency> mergedDependencies =
new LinkedHashMap<Object, Dependency>( ( src.size() + tgt.size() ) * 2 );

for ( Dependency element : tgt )
{
mergedDependencies.put( element.getManagementKey(), element );
}

for ( Dependency element : src )
{
String key = element.getManagementKey();
if ( !mergedDependencies.containsKey( key ) )
{
mergedDependencies.put( key, element );
}
}

modelDependencyManagement
.setDependencies( new ArrayList<Dependency>( mergedDependencies.values() ) );
}
}

return activeProfiles;
};

buildingResult =
modelBuilderThreadSafetyWorkaround.build( buildingRequest, customInjector, customSelector );
modelBuilderThreadSafetyWorkaround.build(
buildingRequest, customInjector, new DefaultProfileSelector()
);
}
catch ( ModelBuildingException e )
{
Expand Down

0 comments on commit 988f260

Please sign in to comment.