Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential fix Issue 73 + properly override dependency #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 66 additions & 11 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
import org.apache.maven.model.Repository;
import org.apache.maven.model.RepositoryPolicy;
import org.apache.maven.model.building.DefaultModelBuilder;
import org.apache.maven.model.building.DefaultModelBuilderFactory;
import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelBuildingResult;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.building.ModelProblemCollectorRequest;
import org.apache.maven.model.interpolation.ModelInterpolator;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.profile.DefaultProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.ProfileInjector;
import org.apache.maven.model.profile.ProfileSelector;
Expand All @@ -52,7 +54,6 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependencies.resolve.DependencyResolver;
import org.codehaus.mojo.flatten.cifriendly.CiInterpolator;
import org.codehaus.mojo.flatten.cifriendly.CiModelInterpolator;
import org.codehaus.mojo.flatten.model.resolution.FlattenModelResolver;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
Expand All @@ -72,10 +73,10 @@
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import org.apache.maven.model.building.ModelBuilder;
import org.codehaus.plexus.component.annotations.Requirement;
import java.util.Set;

/**
* This MOJO realizes the goal <code>flatten</code> that generates the flattened POM and {@link #isUpdatePomFile()
Expand Down Expand Up @@ -312,6 +313,9 @@ public class FlattenMojo
@Component
private DependencyResolver dependencyResolver;

@Component
private ProfileSelector profileSelector;

@Component(role = ModelBuilder.class)
private DefaultModelBuilder defaultModelBuilder;

Expand Down Expand Up @@ -480,7 +484,7 @@ protected Model createFlattenedPom( File pomFile )
}
flattenedPom.setModelEncoding( modelEncoding );

Model cleanPom = createCleanPom( effectivePom );
Model cleanPom = createCleanPom( effectivePom, buildingRequest);

FlattenDescriptor descriptor = getFlattenDescriptor();
Model originalPom = this.project.getOriginalModel();
Expand Down Expand Up @@ -531,9 +535,10 @@ private Model createResolvedPom( ModelBuildingRequest buildingRequest )
* to be kept in flattened POM.
*
* @param effectivePom is the effective POM.
* @param buildingRequest
* @return the clean POM.
*/
protected Model createCleanPom( Model effectivePom )
protected Model createCleanPom(Model effectivePom, ModelBuildingRequest buildingRequest)
{
Model cleanPom = new Model();

Expand Down Expand Up @@ -587,7 +592,7 @@ protected Model createCleanPom( Model effectivePom )
}

// transform dependencies...
List<Dependency> dependencies = createFlattenedDependencies( effectivePom );
List<Dependency> dependencies = createFlattenedDependencies( effectivePom, buildingRequest );
cleanPom.setDependencies( dependencies );
return cleanPom;
}
Expand Down Expand Up @@ -846,21 +851,24 @@ protected static boolean isBuildTimeDriven( Activation activation )
* @param effectiveModel is the effective POM {@link Model} to process.
* @return the {@link List} of {@link Dependency dependencies}.
*/
protected List<Dependency> createFlattenedDependencies( Model effectiveModel )
protected List<Dependency> createFlattenedDependencies(final Model effectiveModel, final ModelBuildingRequest modelBuildingRequest )
{

List<Dependency> flattenedDependencies = new ArrayList<Dependency>();
// resolve all direct and inherited dependencies...
createFlattenedDependencies( effectiveModel, flattenedDependencies );
if ( isEmbedBuildProfileDependencies() )
{
Model projectModel = this.project.getModel();
final Model projectModel = this.project.getModel();
Dependencies modelDependencies = new Dependencies();
modelDependencies.addAll( projectModel.getDependencies() );

Set<String> activeProfile = resolveActiveProfileIds(effectiveModel, modelBuildingRequest);

for ( Profile profile : projectModel.getProfiles() )
{
// build-time driven activation (by property or file)?
if ( isBuildTimeDriven( profile.getActivation() ) )
if ( isBuildTimeDriven( profile.getActivation() ) && activeProfile.contains(profile.getId()))
{
List<Dependency> profileDependencies = profile.getDependencies();
for ( Dependency profileDependency : profileDependencies )
Expand All @@ -872,7 +880,12 @@ protected List<Dependency> createFlattenedDependencies( Model effectiveModel )
// Non build-time driven profiles will remain in the flattened POM with their dependencies
// and
// allow dynamic dependencies due to OS or JDK.
flattenedDependencies.add( profileDependency );
int depIndex = findDependencyToOverride(flattenedDependencies, profileDependency);
if (depIndex == -1) {
flattenedDependencies.add(profileDependency);
} else {
flattenedDependencies.set(depIndex, profileDependency);
}
}
}
}
Expand All @@ -882,6 +895,48 @@ protected List<Dependency> createFlattenedDependencies( Model effectiveModel )
return flattenedDependencies;
}

private int findDependencyToOverride(List<Dependency> dependencies, Dependency override) {

for( int i = 0; i < dependencies.size(); i++ )
{
Dependency candidate = dependencies.get(i);
if (isOverrideOf(override, candidate))
{
return i;
}
}
return -1;
}

private boolean isOverrideOf(Dependency override, Dependency candidate) {
Dependency clone = candidate.clone();
clone.setVersion(override.getVersion());
return clone.getManagementKey().equals(override.getManagementKey());
}

private Set<String> resolveActiveProfileIds(final Model effectiveModel, final ModelBuildingRequest buildingRequest) {
DefaultProfileActivationContext context = new DefaultProfileActivationContext();
context.setActiveProfileIds(buildingRequest.getActiveProfileIds());
context.setInactiveProfileIds(buildingRequest.getInactiveProfileIds());
context.setSystemProperties(buildingRequest.getSystemProperties());
context.setUserProperties(buildingRequest.getUserProperties());
context.setProjectDirectory(buildingRequest.getPomFile() != null ? buildingRequest.getPomFile().getParentFile() : null);

List<Profile> activeProfiles = profileSelector.getActiveProfiles(effectiveModel.getProfiles(), context, new ModelProblemCollector() {
@Override
public void add(ModelProblemCollectorRequest modelProblemCollectorRequest) {

}
});


Set<String> activeProfile = new HashSet<String>();
for(Profile p : activeProfiles) {
activeProfile.add(p.getId());
}
return activeProfile;
}

/**
* Collects the resolved {@link Dependency dependencies} from the given <code>effectiveModel</code>.
*
Expand Down