Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #181: Support dependency ordering #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/it/projects/order-dependency-simple/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-simple</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>dep</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>test</artifactId>
<version>2.0</version>
</dependency>
</dependencies>

<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<orderDependencies>true</orderDependencies>
<flattenDependencyMode>all</flattenDependencyMode>
</configuration>
</plugin>
</plugins>
</build>
</project>
29 changes: 29 additions & 0 deletions src/it/projects/order-dependency-simple/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 flattendPom = new File( basedir, '.flattened-pom.xml' )
assert flattendPom.exists()

def flattendProject = new XmlSlurper().parse( flattendPom )
assert 1 == flattendProject.dependencies.size()
assert 2 == flattendProject.dependencies.dependency.size()

assert "dep" == flattendProject.dependencies.dependency[0].artifactId.text()
assert "test" == flattendProject.dependencies.dependency[1].artifactId.text()


23 changes: 23 additions & 0 deletions src/main/java/org/codehaus/mojo/flatten/DependencyComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.codehaus.mojo.flatten;

import org.apache.maven.model.Dependency;

import java.util.Comparator;

/**
* Orders a {@link Dependency} through its groupId and artifactId.
*/
public class DependencyComparator implements Comparator<Dependency>
{
@Override
public int compare( Dependency o1, Dependency o2 )
{
int c = o1.getGroupId().compareTo( o2.getGroupId() );
if ( c == 0 )
{
// Same groupId
c = o1.getArtifactId().compareTo( o2.getArtifactId() );
}
return c;
}
}
15 changes: 13 additions & 2 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public class FlattenMojo
* </tr>
* <tr>
* <td>resolveCiFriendliesOnly</td>
* <td>Only resolves variables revision, sha1 and changelist. Keeps everything else.
* <td>Only resolves variables revision, sha1 and changelist. Keeps everything else.
* See <a href="https://maven.apache.org/maven-ci-friendly.html">Maven CI Friendly</a> for further details.</td>
* </tr>
* </tbody>
Expand Down Expand Up @@ -329,6 +329,12 @@ public class FlattenMojo
@Parameter( property = "flatten.dependency.mode", required = false )
private FlattenDependencyMode flattenDependencyMode;

/**
* Order dependencies by their groupId and artifactId after they are flattened
*/
@Parameter( defaultValue = "false" )
private boolean orderDependencies;

/**
* The ArtifactFactory required to resolve POM using {@link #modelBuilder}.
*/
Expand All @@ -341,7 +347,7 @@ public class FlattenMojo
*/
@Component( role = ModelInterpolator.class )
private ModelInterpolator modelInterpolator;

/**
* The {@link ModelInterpolator} used to resolve variables.
*/
Expand Down Expand Up @@ -387,6 +393,11 @@ public void execute()
Model flattenedPom = createFlattenedPom( originalPomFile );
String headerComment = extractHeaderComment( originalPomFile );

if ( orderDependencies )
{
Collections.sort( flattenedPom.getDependencies(), new DependencyComparator() );
}

File flattenedPomFile = getFlattenedPomFile();
writePom( flattenedPom, flattenedPomFile, headerComment );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.codehaus.mojo.flatten;

import org.apache.maven.model.Dependency;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class DependencyComparatorTest
{
private final DependencyComparator comparator = new DependencyComparator();

@Test
public void shouldOrder()
{
List<Dependency> dependencies = new ArrayList<>();
dependencies.add( createDependency( "g", "b", "1" ) );
dependencies.add( createDependency( "g", "a", "1" ) );
dependencies.add( createDependency( "g", "c", "1" ) );
Collections.sort( dependencies, comparator );
assertThat( dependencies ).usingComparatorForType( comparator, Dependency.class ).containsExactly(
createDependency( "g", "a", "1" ),
createDependency( "g", "b", "1" ),
createDependency( "g", "c", "1" ) );
}

private Dependency createDependency( String groupId, String artifactId, String version )
{
Dependency dependency = new Dependency();
dependency.setGroupId( groupId );
dependency.setArtifactId( artifactId );
dependency.setVersion( version );
return dependency;
}
}