Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions maven-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ under the License.
<artifactId>plexus-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-test-util</artifactId>
<version>${resolverVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
32 changes: 30 additions & 2 deletions maven-core/src/main/java/org/apache/maven/RepositoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
Expand Down Expand Up @@ -112,6 +113,25 @@ public static org.apache.maven.artifact.Artifact toArtifact( Artifact artifact )
public static void toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifacts,
Collection<? extends DependencyNode> nodes, List<String> trail,
DependencyFilter filter )
{
Map<Integer, List<org.apache.maven.artifact.Artifact>> artifactsByDepth = new TreeMap<>();

List<org.apache.maven.artifact.Artifact> firstLevelArtifacts = new ArrayList<>( nodes.size() );
// we know there are at least direct dependencies
artifactsByDepth.put( 1, firstLevelArtifacts );

toArtifacts( artifactsByDepth, 1, nodes, trail, filter );

// list of artifacts ordered by depth level
for ( List<org.apache.maven.artifact.Artifact> artifactsInDepth : artifactsByDepth.values() )
{
artifacts.addAll( artifactsInDepth );
}
}

private static void toArtifacts( Map<Integer, List<org.apache.maven.artifact.Artifact>> artifactsByDepth,
int currentDepth, Collection<? extends DependencyNode> nodes, List<String> trail,
DependencyFilter filter )
{
for ( DependencyNode node : nodes )
{
Expand All @@ -124,10 +144,18 @@ public static void toArtifacts( Collection<org.apache.maven.artifact.Artifact> a
if ( filter == null || filter.accept( node, Collections.emptyList() ) )
{
artifact.setDependencyTrail( nodeTrail );
artifacts.add( artifact );

// add artifact in the list of the current depth
List<org.apache.maven.artifact.Artifact> artifactsCurrentDepth = artifactsByDepth.get( currentDepth );
if ( artifactsCurrentDepth == null )
{
artifactsCurrentDepth = new ArrayList<>();
artifactsByDepth.put( currentDepth, artifactsCurrentDepth );
}
artifactsCurrentDepth.add( artifact );
}

toArtifacts( artifacts, node.getChildren(), nodeTrail, filter );
toArtifacts( artifactsByDepth, currentDepth + 1, node.getChildren(), nodeTrail, filter );
}
}

Expand Down
114 changes: 114 additions & 0 deletions maven-core/src/test/java/org/apache/maven/RepositoryUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.apache.maven;

/*
* 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.
*/
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.internal.test.util.DependencyGraphParser;
import org.junit.jupiter.api.Test;

/**
* Test RepositoryUtils
*
* @author Gabriel Belingueres
*/
public class RepositoryUtilsTest
{

@Test
public void testToArtifactsCollectionOrderedByNodeDepth()
{
Collection<org.apache.maven.artifact.Artifact> artifacts = new ArrayList<>();

DependencyNode root = createDependencyTree();

List<String> trail = new ArrayList<>();
DependencyFilter filter = null;

RepositoryUtils.toArtifacts( artifacts, root.getChildren(), trail, filter );

String expected =
"[gid:zlevel1:jar:1:, gid:ylevel1:jar:1:, gid:xlevel1:jar:1:, gid:alevel2:jar:1:, gid:blevel2:jar:1:, gid:clevel2:jar:1:, gid:alevel3:jar:1:]";
assertEquals( expected, artifacts.toString() );
}

@Test
public void testToArtifactsCollectionOrderedByNodeDepthWithFilter()
{
Collection<org.apache.maven.artifact.Artifact> artifacts = new ArrayList<>();

DependencyNode root = createDependencyTree();

List<String> trail = new ArrayList<>();
DependencyFilter filter = new DependencyFilter()
{
@Override
public boolean accept( DependencyNode node, List<DependencyNode> parents )
{
// accept node if artifactId does NOT contain "level2"
return !node.getArtifact().getArtifactId().contains( "level2" );
}
};

RepositoryUtils.toArtifacts( artifacts, root.getChildren(), trail, filter );

String expected =
"[gid:zlevel1:jar:1:, gid:ylevel1:jar:1:, gid:xlevel1:jar:1:, gid:alevel3:jar:1:]";
assertEquals( expected, artifacts.toString() );
}

/**
* Create a dependency tree.
*
* @return the root node or the tree.
*/
private DependencyNode createDependencyTree()
{
String LS = System.lineSeparator();
DependencyGraphParser parser = new DependencyGraphParser();
String dependencyGraph =
"gid:root:1" + LS +
"+- gid:zlevel1:1" + LS +
"| \\- gid:alevel2:1" + LS +
"\\- gid:ylevel1:1" + LS +
"| \\- gid:blevel2:1" + LS +
"| \\- gid:alevel3:1" + LS +
"\\- gid:xlevel1:1" + LS +
" \\- gid:clevel2:1" + LS;

try
{
return parser.parseLiteral( dependencyGraph );
}
catch ( IOException e )
{
fail( "Failed the parsing of the dependency node graph" );
}
return null;
}

}