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

Fix #288 Add configuration option to omit dependency exclusions stanzas from flattened POM #289

Merged
merged 1 commit into from Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 43 additions & 15 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Expand Up @@ -252,6 +252,16 @@ public class FlattenMojo
@Parameter( required = false )
private FlattenDescriptor pomElements;

/**
* Dictates whether dependency exclusions stanzas should be included in the flattened POM. By default exclusions
* will be included in the flattened POM but if you wish to omit exclusions stanzas from being present then set
* this configuration property to <code>true</code>.
*
* @since 1.3.0
*/
@Parameter( defaultValue = "false", required = false )
josple marked this conversation as resolved.
Show resolved Hide resolved
private boolean omitExclusions;

/**
* The different possible values for flattenMode:
* <table border="1" summary="">
Expand Down Expand Up @@ -1014,7 +1024,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( modelDependencies.resolve(profileDependency) );
Dependency resolvedProfileDependency = modelDependencies.resolve(profileDependency);
if ( omitExclusions )
{
resolvedProfileDependency.setExclusions(Collections.emptyList());
}
flattenedDependencies.add( resolvedProfileDependency );
}
}
}
Expand Down Expand Up @@ -1120,26 +1135,29 @@ private void createFlattenedDependenciesAll( List<Dependency> projectDependencie
dependency.setScope(artifact.getScope());
dependency.setType(artifact.getType());

List<Exclusion> exclusions = new LinkedList<>();
if ( !omitExclusions )
{
List<Exclusion> exclusions = new LinkedList<>();

org.eclipse.aether.artifact.Artifact aetherArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), null, artifact.getVersion());
ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(aetherArtifact, null, null);
ArtifactDescriptorResult artifactDescriptorResult = this.artifactDescriptorReader
org.eclipse.aether.artifact.Artifact aetherArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), null, artifact.getVersion());
ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(aetherArtifact, null, null);
ArtifactDescriptorResult artifactDescriptorResult = this.artifactDescriptorReader
.readArtifactDescriptor(this.session.getRepositorySession(), request);

for (org.eclipse.aether.graph.Dependency artifactDependency: artifactDescriptorResult.getDependencies())
{
if ("test".equals(artifactDependency.getScope()))
for (org.eclipse.aether.graph.Dependency artifactDependency: artifactDescriptorResult.getDependencies())
{
if ("test".equals(artifactDependency.getScope()))
{
continue;
}
Exclusion exclusion = new Exclusion();
exclusion.setGroupId(artifactDependency.getArtifact().getGroupId());
exclusion.setArtifactId(artifactDependency.getArtifact().getArtifactId());
exclusions.add(exclusion);
}
Exclusion exclusion = new Exclusion();
exclusion.setGroupId(artifactDependency.getArtifact().getGroupId());
exclusion.setArtifactId(artifactDependency.getArtifact().getArtifactId());
exclusions.add(exclusion);
}

dependency.setExclusions(exclusions);
dependency.setExclusions(exclusions);
}

// convert dependency to string for the set, since Dependency doesn't implement equals, etc.
String dependencyString = dependency.getManagementKey();
Expand Down Expand Up @@ -1190,7 +1208,17 @@ protected void createFlattenedDependencies( Model effectiveModel, List<Dependenc
*/
protected Dependency createFlattenedDependency( Dependency projectDependency )
{
return "test".equals( projectDependency.getScope() ) ? null : projectDependency;
if ( "test".equals( projectDependency.getScope() ) )
{
return null;
}

if ( omitExclusions )
{
projectDependency.setExclusions(Collections.emptyList());
}

return projectDependency;
}

/**
Expand Down
@@ -0,0 +1,77 @@
package org.codehaus.mojo.flatten;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;

/**
* Test case for the omitexclusions configuration option.
*/
public class FlattenMojoOmitExclusionsTest {

private static final String PATH = "src/test/resources/omit-exclusions/";
private static final String FLATTENED_POM = PATH + ".flattened-pom.xml";

@Rule public MojoRule rule = new MojoRule();


/**
* Verify that when the omit exclusions configuration option is set then the
* exclusions stanza of any dependencies is not copied into the flattened
* POM.
*/
@Test
public void testOmitExclusions() throws Exception {
// -- Given...
//
MavenProject project = rule.readMavenProject( new File( PATH ) );
FlattenMojo flattenMojo = (FlattenMojo) rule.lookupConfiguredMojo( project, "flatten" );

// -- When...
//
flattenMojo.execute();

// -- Then...
//
readPom( FLATTENED_POM )
.getDependencies()
.stream()
.filter(dep -> !dep.getExclusions().isEmpty())
.findAny()
.ifPresent(dep -> fail("No exclusions should be present in flattened POM."));
}


private static Model readPom(String pomFilePath) throws IOException, XmlPullParserException {
try ( FileInputStream input = new FileInputStream( new File( pomFilePath ) ) ) {
return new MavenXpp3Reader().read( input );
}
}


/**
* After test method. Removes flattened-pom.xml file which is created during test.
*
* @throws IOException if can't remove file.
*/
@After
public void removeFlattenedPom() throws IOException {
File flattenedPom = new File( FLATTENED_POM );
if ( flattenedPom.exists() ) {
if ( !flattenedPom.delete() ) {
throw new IOException( "Can't delete " + flattenedPom );
}
}
}
}
36 changes: 36 additions & 0 deletions src/test/resources/omit-exclusions/pom.xml
@@ -0,0 +1,36 @@
<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>
<!-- https://github.com/mojohaus/flatten-maven-plugin/issues/288 -->
<artifactId>omit-exclusions</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>groupA</groupId>
<artifactId>artifactB</artifactId>
<version>1</version>
<exclusions>
<exclusion>
<groupId>groupX</groupId>
<artifactId>artifactY</artifactId>
</exclusion>
<exclusion>
<groupId>group1</groupId>
<artifactId>artifact2</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<omitExclusions>true</omitExclusions>
</configuration>
</plugin>
</plugins>
</build>
</project>