Skip to content

Commit

Permalink
#291 display-dependency-updates: introduce switch processDependencyMa…
Browse files Browse the repository at this point in the history
…nagementTransitive (#588)
  • Loading branch information
stefanseifert committed May 5, 2022
1 parent 7a5df14 commit 257d33c
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/it-repo/dummy-api-impl-bom-pom-1.0.pom
@@ -0,0 +1,25 @@
<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>localhost</groupId>
<artifactId>dummy-api-impl-bom-pom</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-impl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
25 changes: 25 additions & 0 deletions src/it-repo/dummy-api-impl-bom-pom-2.0.pom
@@ -0,0 +1,25 @@
<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>localhost</groupId>
<artifactId>dummy-api-impl-bom-pom</artifactId>
<version>2.0</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-impl</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
@@ -0,0 +1,2 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates
invoker.mavenOpts=-DprocessDependencyManagementTransitive=false
@@ -0,0 +1,58 @@
<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>localhost</groupId>
<artifactId>it-display-dependency-updates-001</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>display-dependency-updates</name>
<url>http://localhost/</url>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api-impl-bom-pom</artifactId>
<version>1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@@ -0,0 +1,45 @@
import java.io.*;
import org.codehaus.plexus.util.FileUtils;
import java.util.regex.*;

try
{
File file = new File( basedir, "build.log" );
String buf = FileUtils.fileRead( file );

Pattern p;
Matcher m;

p = Pattern.compile( "\\Qlocalhost:dummy-api\\E\\s*\\.*\\s*1\\.1\\s+->\\s+3\\.0" );
m = p.matcher( buf.toString() );
if ( !m.find() )
{
System.out.println( "Did not suggest updating dummy-api to version 3.0" );
return false;
}
System.out.println( m.group( 0 ) );

p = Pattern.compile( "\\Qlocalhost:dummy-impl\\E\\s*\\.*\\s*1\\.2\\s+->\\s+2\\.2" );
m = p.matcher( buf.toString() );
if ( m.find() )
{
System.out.println( "Did suggest updating dummy-impl to version 2.2" );
return false;
}

p = Pattern.compile( "\\Qlocalhost:dummy-api-impl-bom-pom\\E\\s*\\.*\\s*1\\.0\\s+->\\s+2\\.0" );
m = p.matcher( buf.toString() );
if ( !m.find() )
{
System.out.println( "Did not suggest updating dummy-api-impl-bom-pom to version 2.0" );
return false;
}
System.out.println( m.group( 0 ) );
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
@@ -0,0 +1,2 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates
invoker.mavenOpts=-DprocessDependencyManagementTransitive=true
@@ -0,0 +1,58 @@
<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>localhost</groupId>
<artifactId>it-display-dependency-updates-001</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>display-dependency-updates</name>
<url>http://localhost/</url>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api-impl-bom-pom</artifactId>
<version>1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@@ -0,0 +1,45 @@
import java.io.*;
import org.codehaus.plexus.util.FileUtils;
import java.util.regex.*;

try
{
File file = new File( basedir, "build.log" );
String buf = FileUtils.fileRead( file );

Pattern p;
Matcher m;

p = Pattern.compile( "\\Qlocalhost:dummy-api\\E\\s*\\.*\\s*1\\.1\\s+->\\s+3\\.0" );
m = p.matcher( buf.toString() );
if ( !m.find() )
{
System.out.println( "Did not suggest updating dummy-api to version 3.0" );
return false;
}
System.out.println( m.group( 0 ) );

p = Pattern.compile( "\\Qlocalhost:dummy-impl\\E\\s*\\.*\\s*1\\.2\\s+->\\s+2\\.2" );
m = p.matcher( buf.toString() );
if ( !m.find() )
{
System.out.println( "Did not suggest updating dummy-impl to version 2.2" );
return false;
}
System.out.println( m.group( 0 ) );

p = Pattern.compile( "\\Qlocalhost:dummy-api-impl-bom-pom\\E\\s*\\.*\\s*1\\.0\\s+->\\s+2\\.0" );
m = p.matcher( buf.toString() );
if ( m.find() )
{
System.out.println( "Did suggest updating dummy-api-impl-bom-pom to version 2.0" );
return false;
}
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
Expand Up @@ -25,11 +25,13 @@
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.UpdateScope;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
Expand Down Expand Up @@ -76,6 +78,19 @@ public class DisplayDependencyUpdatesMojo
@Parameter( property = "processDependencyManagement", defaultValue = "true" )
private boolean processDependencyManagement;

/**
* Whether to process the depdendencyManagement part transitive or not.
* In case of <code>&lt;type&gt;pom&lt;/type&gt;</code>and
* <code>&lt;scope&gt;import&lt;/scope&gt;</code> this means
* by default to report also the imported dependencies.
* If processTransitive is set to <code>false</code> the report will only show
* updates of the imported pom it self.
*
* @since 2.11
*/
@Parameter( property = "processDependencyManagementTransitive", defaultValue = "true" )
private boolean processDependencyManagementTransitive;

/**
* Whether to process the dependencies section of the project.
*
Expand Down Expand Up @@ -283,10 +298,11 @@ public void execute()
logInit();

Set<Dependency> dependencyManagement = new TreeSet<>( new DependencyComparator() );
if ( getProject().getDependencyManagement() != null )
DependencyManagement projectDependencyManagement = getProjectDependencyManagement(getProject());
if ( projectDependencyManagement != null )
{

List<Dependency> dependenciesFromPom = getProject().getDependencyManagement().getDependencies();
List<Dependency> dependenciesFromPom = projectDependencyManagement.getDependencies();
for ( Dependency dependency : dependenciesFromPom )
{
getLog().debug( "dependency from pom: " + dependency.getGroupId() + ":" + dependency.getArtifactId()
Expand All @@ -297,10 +313,10 @@ public void execute()
if ( getProject().hasParent() )
{
getLog().debug( "Reading parent dependencyManagement information" );
if ( getProject().getParent().getDependencyManagement() != null )
DependencyManagement parentProjectDependencyManagement = getProjectDependencyManagement(getProject().getParent());
if ( parentProjectDependencyManagement != null )
{
List<Dependency> parentDeps =
getProject().getParent().getDependencyManagement().getDependencies();
List<Dependency> parentDeps = parentProjectDependencyManagement.getDependencies();
for ( Dependency parentDep : parentDeps )
{
// only groupId && artifactId needed cause version is null
Expand Down Expand Up @@ -378,6 +394,15 @@ public void execute()
}
}

private DependencyManagement getProjectDependencyManagement(MavenProject project) {
if (processDependencyManagementTransitive) {
return project.getDependencyManagement();
}
else {
return project.getOriginalModel().getDependencyManagement();
}
}

private UpdateScope calculateUpdateScope()
{
UpdateScope result = UpdateScope.ANY;
Expand Down

0 comments on commit 257d33c

Please sign in to comment.