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..0743d413
--- /dev/null
+++ b/src/it/projects/flatten-dependency-all-depmgnt-optional/verify.groovy
@@ -0,0 +1,37 @@
+/*
+ * 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 flattendPom = new File( basedir, '.flattened-pom.xml' )
+assert flattendPom.exists()
+
+def flattendProject = new XmlSlurper().parse( flattendPom )
+assert 2 == flattendProject.dependencies.dependency.size()
+assert "core" == flattendProject.dependencies.dependency[0].artifactId.text()
+assert "3.2.1" == flattendProject.dependencies.dependency[0].version.text()
+assert "true" == flattendProject.dependencies.dependency[0].optional.text()
+assert "dep" == flattendProject.dependencies.dependency[1].artifactId.text()
+assert "1.1" == flattendProject.dependencies.dependency[1].version.text()
+assert "true" == flattendProject.dependencies.dependency[1].optional.text()
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..59f3bbc2
--- /dev/null
+++ b/src/it/projects/flatten-dependency-all-optional/verify.groovy
@@ -0,0 +1,38 @@
+/*
+ * 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 flattendPom = new File( basedir, '.flattened-pom.xml' )
+assert flattendPom.exists()
+
+def flattendProject = new XmlSlurper().parse( flattendPom )
+assert 2 == flattendProject.dependencies.dependency.size()
+assert "core" == flattendProject.dependencies.dependency[0].artifactId.text()
+assert "3.2.1" == flattendProject.dependencies.dependency[0].version.text()
+assert "true" == flattendProject.dependencies.dependency[0].optional.text()
+assert "dep" == flattendProject.dependencies.dependency[1].artifactId.text()
+assert "3.2.1" == flattendProject.dependencies.dependency[1].version.text()
+assert "true" == flattendProject.dependencies.dependency[1].optional.text()
diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
index e12a165e..410cd94e 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,33 @@ 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 true;
+ }
+ if (node.getParent().getArtifact().isOptional())
+ {
+ node.getArtifact().setOptional(true);
+ }
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 +1106,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 +1123,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);
}
}