From bc688e840061071c28586758b1702b17f59ec094 Mon Sep 17 00:00:00 2001 From: Joseph Leonard Date: Mon, 11 Jul 2022 19:02:23 +0100 Subject: [PATCH] Add configuration option to omit exclusions from flattened POMs Introduces a new 'omitExclusions' configuration option that is false by default to maintain backwards compatible behaviour. When this new configuration option is explicitly set to true then this will result in the flattened POM omitting all exclusions stanzas from the dependency stanzas. Fixes mojohaus#288 --- .../codehaus/mojo/flatten/FlattenMojo.java | 58 ++++++++++---- .../FlattenMojoOmitExclusionsTest.java | 77 +++++++++++++++++++ src/test/resources/omit-exclusions/pom.xml | 36 +++++++++ 3 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 src/test/java/org/codehaus/mojo/flatten/FlattenMojoOmitExclusionsTest.java create mode 100644 src/test/resources/omit-exclusions/pom.xml diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java index 84e6ad83..dd23790c 100644 --- a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java @@ -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 true. + * + * @since 1.3.0 + */ + @Parameter( defaultValue = "false", required = false ) + private boolean omitExclusions; + /** * The different possible values for flattenMode: * @@ -1014,7 +1024,12 @@ protected List 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 ); } } } @@ -1120,26 +1135,29 @@ private void createFlattenedDependenciesAll( List projectDependencie dependency.setScope(artifact.getScope()); dependency.setType(artifact.getType()); - List exclusions = new LinkedList<>(); + if ( !omitExclusions ) + { + List 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(); @@ -1190,7 +1208,17 @@ protected void createFlattenedDependencies( Model effectiveModel, List !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 ); + } + } + } +} diff --git a/src/test/resources/omit-exclusions/pom.xml b/src/test/resources/omit-exclusions/pom.xml new file mode 100644 index 00000000..9cc88ec1 --- /dev/null +++ b/src/test/resources/omit-exclusions/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + + omit-exclusions + 0.0.1-SNAPSHOT + + + groupA + artifactB + 1 + + + groupX + artifactY + + + group1 + artifact2 + + + + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + + true + + + + + \ No newline at end of file