Skip to content

Commit

Permalink
fixes #160: when the direct dependency is optional, flattened its tra…
Browse files Browse the repository at this point in the history
…nsitive dependencies and set them as optional (#164)

* fixes #160: when the direct dependency is optional, flattened its transitive dependencies and set them as optional

* remove optional dependency subtree

* return false in visitor when node is not included
  • Loading branch information
yangnuoyu authored and olamy committed May 3, 2022
1 parent 410fd89 commit cf36c80
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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>
<artifactId>flatten-dependency-all-depmgnt-optional-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>dep</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
33 changes: 33 additions & 0 deletions src/it/projects/flatten-dependency-all-depmgnt-optional/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<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>
<artifactId>flatten-dependency-all-depmgnt-optional</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>flatten-dependency-all-depmgnt-optional-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>parent</relativePath>
</parent>

<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<flattenDependencyMode>all</flattenDependencyMode>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>core</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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()

28 changes: 28 additions & 0 deletions src/it/projects/flatten-dependency-all-optional/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<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>
<artifactId>flatten-dependency-all-optional</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<flattenDependencyMode>all</flattenDependencyMode>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
32 changes: 32 additions & 0 deletions src/it/projects/flatten-dependency-all-optional/verify.groovy
Original file line number Diff line number Diff line change
@@ -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()
39 changes: 27 additions & 12 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -1049,31 +1049,42 @@ private void createFlattenedDependenciesDirect( List<Dependency> projectDependen
* @throws ArtifactDescriptorException
*/
private void createFlattenedDependenciesAll( List<Dependency> projectDependencies, List<Dependency> flattenedDependencies )
throws DependencyTreeBuilderException, ArtifactDescriptorException {
final Queue<DependencyNode> dependencyNodeLinkedList = new LinkedList<DependencyNode>() {};
throws DependencyTreeBuilderException, ArtifactDescriptorException
{
final Queue<DependencyNode> dependencyNodeLinkedList = new LinkedList<DependencyNode>();
final Set<String> processedDependencies = new HashSet<>();

final Artifact projectArtifact = this.project.getArtifact();

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();
Expand All @@ -1094,8 +1105,10 @@ private void createFlattenedDependenciesAll( List<Dependency> 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();
Expand All @@ -1109,12 +1122,14 @@ private void createFlattenedDependenciesAll( List<Dependency> 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);
}
}
Expand Down

0 comments on commit cf36c80

Please sign in to comment.