Skip to content

Commit

Permalink
Fix Issue#377 Regression: parent dependencies missing in flattened pom
Browse files Browse the repository at this point in the history
... since 1.4.0. Introduce a new "inherited" flatten dependency mode that
includes the inherited dependencies like "direct" did in 1.3.0. And make this
the new default behaviour bringing it back to 1.3.0 and before
  • Loading branch information
hkampbjorn committed May 31, 2024
1 parent f5299fd commit ee3dd7d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<flattenDependencyMode>direct</flattenDependencyMode>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
12 changes: 0 additions & 12 deletions src/it/projects/inherit-parent-dependency/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,4 @@
</parent>
<artifactId>resolve-properties</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>
</project>
24 changes: 6 additions & 18 deletions src/it/projects/parent-with-version-range/pom.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
<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>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>parent-deps</artifactId>
<version>[0,2]</version>
</parent>
<parent>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>parent-deps</artifactId>
<version>[0,2]</version>
</parent>

<artifactId>parent-with-version-range</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>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
*/
package org.codehaus.mojo.flatten;

import javax.inject.Named;
import javax.inject.Singleton;

import java.util.HashMap;
import java.util.Map;

import org.apache.maven.model.Model;
import org.apache.maven.model.ModelBase;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
import org.apache.maven.model.merge.MavenModelMerger;

import javax.inject.Named;
import javax.inject.Singleton;
import java.util.HashMap;
import java.util.Map;

/**
* The DefaultInheritanceAssembler is encapsulating the InheritanceModelMerger.
* The only way to override functionality needed is to define an own InheritanceAssembler
Expand Down Expand Up @@ -144,7 +143,7 @@ public void merge(Model target, Model source, boolean sourceDominant, Map<?, ?>
@Override
protected void mergeModelBase_Dependencies(
ModelBase target, ModelBase source, boolean sourceDominant, Map<Object, Object> context) {
if (flattenDependencyMode == null || flattenDependencyMode == FlattenDependencyMode.direct) {
if (flattenDependencyMode == FlattenDependencyMode.direct) {
return;
}
super.mergeModelBase_Dependencies(target, source, sourceDominant, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@
*/
public enum FlattenDependencyMode {
/**
* Flatten only the direct dependency versions. This is the default mode and compatible with
* Flatten Plugin prior to 1.2.0.
* Flatten only the direct dependency versions, excluding inherited dependencies from a parent module.
*/
direct,

/**
* Flatten both direct and transitive dependencies. This will examine the full dependency tree, and pull up
* Flatten dependencies, including inherited dependencies from a parent module.
* This is the default mode and compatible with Flatten Plugin prior to 1.2.0.
*/
inherited,

/**
* Flatten both direct, inherited and transitive dependencies. This will examine the full dependency tree, and pull up
* all transitive dependencies as a direct dependency, and setting their versions appropriately.
*
* This is recommended if you are releasing a library that uses dependency management to manage dependency
Expand Down
65 changes: 37 additions & 28 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,6 @@
* under the License.
*/

import javax.inject.Inject;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
Expand Down Expand Up @@ -99,6 +74,30 @@
import org.xml.sax.SAXException;
import org.xml.sax.ext.DefaultHandler2;

import javax.inject.Inject;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* This MOJO realizes the goal <code>flatten</code> that generates the flattened POM and {@link #isUpdatePomFile()
* potentially updates the POM file} so that the current {@link MavenProject}'s {@link MavenProject#getFile() file}
Expand Down Expand Up @@ -339,8 +338,16 @@ public class FlattenMojo extends AbstractFlattenMojo {
* </thead><tbody>
* <tr>
* <td>direct</td>
* <td>Flatten only the direct dependency versions.
* This is the default mode and compatible with Flatten Plugin prior to 1.2.0.</td>
* <td><p>Flatten only the direct dependency versions, excluding inherited dependencies from a parent module.</p>
* <p>This was the default mode with Flatten Plugin in versions 1.4.0 up to 1.6.0.
* </td>
* </tr>
* <tr>
* <td>inherited</td>
* <td><p>Flatten the dependency versions, including inherited dependencies from a parent module</p>
* <p>This is the default mode and compatible with Flatten Plugin prior to 1.2.0, this mode was called <tt>direct</tt> between versions 1.2.0 and 1.3.0.</p>
* </td>
* </tr>
* <tr>
* <td>all</td>
* <td><p>Flatten both direct and transitive dependencies. This will examine the full dependency tree, and pull up
Expand Down Expand Up @@ -1225,7 +1232,9 @@ protected void createFlattenedDependencies(Model effectiveModel, List<Dependency
// List<Dependency> projectDependencies = currentProject.getOriginalModel().getDependencies();
List<Dependency> projectDependencies = effectiveModel.getDependencies();

if (flattenDependencyMode == null | flattenDependencyMode == FlattenDependencyMode.direct) {
if (flattenDependencyMode == null
|| flattenDependencyMode == FlattenDependencyMode.direct
|| flattenDependencyMode == FlattenDependencyMode.inherited) {
createFlattenedDependenciesDirect(projectDependencies, flattenedDependencies);
} else if (flattenDependencyMode == FlattenDependencyMode.all) {
try {
Expand Down

0 comments on commit ee3dd7d

Please sign in to comment.