diff --git a/src/it/projects/flatten-dependency-all-depmgnt-optional/parent/pom.xml b/src/it/projects/flatten-dependency-all-depmgnt-optional/parent/pom.xml new file mode 100644 index 00000000..f45e910c --- /dev/null +++ b/src/it/projects/flatten-dependency-all-depmgnt-optional/parent/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + flatten-dependency-all-depmgnt-optional-parent + 0.0.1-SNAPSHOT + pom + + + + + org.codehaus.mojo.flatten.its + core + 3.2.1 + + + org.codehaus.mojo.flatten.its + dep + 1.1 + + + + + \ No newline at end of file diff --git a/src/it/projects/flatten-dependency-all-depmgnt-optional/pom.xml b/src/it/projects/flatten-dependency-all-depmgnt-optional/pom.xml new file mode 100644 index 00000000..7eed0968 --- /dev/null +++ b/src/it/projects/flatten-dependency-all-depmgnt-optional/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + flatten-dependency-all-depmgnt-optional + 0.0.1-SNAPSHOT + + org.codehaus.mojo.flatten.its + flatten-dependency-all-depmgnt-optional-parent + 0.0.1-SNAPSHOT + parent + + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + + all + + + + + + + + org.codehaus.mojo.flatten.its + core + true + + + \ No newline at end of file diff --git a/src/it/projects/flatten-dependency-all-depmgnt-optional/verify.groovy b/src/it/projects/flatten-dependency-all-depmgnt-optional/verify.groovy new file mode 100644 index 00000000..b607baf1 --- /dev/null +++ b/src/it/projects/flatten-dependency-all-depmgnt-optional/verify.groovy @@ -0,0 +1,32 @@ +/* + * 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() +assert "core" == originalProject.dependencies.dependency.artifactId.text() +assert "true" == originalProject.dependencies.dependency.optional.text() + +File flattenedPom = new File( basedir, '.flattened-pom.xml' ) +assert flattenedPom.exists() + +def flattenedProject = new XmlSlurper().parse( flattenedPom ) +assert 0 == flattenedProject.dependencies.dependency.size() + diff --git a/src/it/projects/flatten-dependency-all-optional/pom.xml b/src/it/projects/flatten-dependency-all-optional/pom.xml new file mode 100644 index 00000000..0aedb441 --- /dev/null +++ b/src/it/projects/flatten-dependency-all-optional/pom.xml @@ -0,0 +1,28 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + flatten-dependency-all-optional + 0.0.1-SNAPSHOT + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + + all + + + + + + + + org.codehaus.mojo.flatten.its + core + 3.2.1 + true + + + \ No newline at end of file diff --git a/src/it/projects/flatten-dependency-all-optional/verify.groovy b/src/it/projects/flatten-dependency-all-optional/verify.groovy new file mode 100644 index 00000000..5e2f8b3e --- /dev/null +++ b/src/it/projects/flatten-dependency-all-optional/verify.groovy @@ -0,0 +1,32 @@ +/* + * 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() +assert "core" == originalProject.dependencies.dependency.artifactId.text() +assert "3.2.1" == originalProject.dependencies.dependency.version.text() +assert "true" == originalProject.dependencies.dependency.optional.text() + +File flattenedPom = new File( basedir, '.flattened-pom.xml' ) +assert flattenedPom.exists() + +def flattenedProject = new XmlSlurper().parse( flattenedPom ) +assert 0 == flattenedProject.dependencies.dependency.size() \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java index e12a165e..ac9b97b1 100644 --- a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java @@ -1049,8 +1049,9 @@ private void createFlattenedDependenciesDirect( List projectDependen * @throws ArtifactDescriptorException */ private void createFlattenedDependenciesAll( List projectDependencies, List flattenedDependencies ) - throws DependencyTreeBuilderException, ArtifactDescriptorException { - final Queue dependencyNodeLinkedList = new LinkedList() {}; + throws DependencyTreeBuilderException, ArtifactDescriptorException + { + final Queue dependencyNodeLinkedList = new LinkedList(); final Set processedDependencies = new HashSet<>(); final Artifact projectArtifact = this.project.getArtifact(); @@ -1058,22 +1059,32 @@ private void createFlattenedDependenciesAll( List projectDependencie final DependencyNode dependencyNode = this.dependencyTreeBuilder.buildDependencyTree(this.project, this.localRepository, null); - dependencyNode.accept(new DependencyNodeVisitor() { - @Override public boolean visit(DependencyNode node) { - if (node.getArtifact().getGroupId().equals(projectArtifact.getGroupId()) && node.getArtifact().getArtifactId().equals(projectArtifact.getArtifactId())) { + dependencyNode.accept(new DependencyNodeVisitor() + { + @Override public boolean visit(DependencyNode node) + { + if (node.getArtifact().getGroupId().equals(projectArtifact.getGroupId()) && node.getArtifact().getArtifactId().equals(projectArtifact.getArtifactId())) + { return true; } - if (node.getState() != DependencyNode.INCLUDED) return true; + if (node.getState() != DependencyNode.INCLUDED) { + return false; + } + if (node.getArtifact().isOptional()) { + return false; + } dependencyNodeLinkedList.add(node); return true; } - @Override public boolean endVisit(DependencyNode node) { + @Override public boolean endVisit(DependencyNode node) + { return true; } }); - while (!dependencyNodeLinkedList.isEmpty()) { + while (!dependencyNodeLinkedList.isEmpty()) + { DependencyNode node = dependencyNodeLinkedList.poll(); Artifact artifact = node.getArtifact(); @@ -1094,8 +1105,10 @@ private void createFlattenedDependenciesAll( List projectDependencie 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(); @@ -1109,12 +1122,14 @@ private void createFlattenedDependenciesAll( List projectDependencie // convert dependency to string for the set, since Dependency doesn't implement equals, etc. String dependencyString = dependency.getManagementKey(); - if (!processedDependencies.add(dependencyString)) { + if (!processedDependencies.add(dependencyString)) + { continue; } Dependency flattenedDependency = createFlattenedDependency( dependency ); - if (flattenedDependency != null) { + if (flattenedDependency != null) + { flattenedDependencies.add(flattenedDependency); } }