Skip to content

Commit

Permalink
Fixed #34
Browse files Browse the repository at this point in the history
 o Added implementation to report the plugin dependencies as well.
 o Added IT...not verifing yet..
  • Loading branch information
khmarbaise committed Jun 9, 2017
1 parent 55f519e commit 43e933d
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates
69 changes: 69 additions & 0 deletions src/it/it-display-dependency-updates-007-issue-34/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<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-004</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>display-dependency-updates-004</name>
<url>http://localhost/</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</dependency>

</dependencies>
</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>

<properties>
<processDependencyManagement>false</processDependencyManagement>
</properties>
</project>
32 changes: 32 additions & 0 deletions src/it/it-display-dependency-updates-007-issue-34/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.*;
import org.codehaus.plexus.util.FileUtils;
import java.util.regex.*;

try
{
System.out.println("Running verification...");
File file = new File( basedir, "build.log" );
String buf = FileUtils.fileRead( file );

[INFO] The following dependencies in Dependencies have newer versions:
[INFO] localhost:dummy-api ....................................... 2.0 -> 3.0
[INFO]

[INFO] The following dependencies in pluginManagement of plugins have newer versions:
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0

Pattern p = Pattern.compile( "localhost:dummy-api .* 1.1" );
Matcher m = p.matcher( buf.toString() );
if ( m.find() )
{
System.out.println( "localhost:dummy-api 1.1 was processed (but should not have been)" );
return false;
}
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
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;
Expand All @@ -34,15 +36,15 @@
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.plexus.util.StringUtils;

import javax.xml.stream.XMLStreamException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.stream.XMLStreamException;

/**
* Displays all dependencies that have newer versions available.
*
Expand Down Expand Up @@ -79,6 +81,22 @@ public class DisplayDependencyUpdatesMojo
@Parameter( property = "processDependencies", defaultValue = "true" )
protected boolean processDependencies;

/**
* Whether to process the dependencies sections of plugins.
*
* @since 2.5
*/
@Parameter( property = "processPluginDependencies", defaultValue = "true" )
protected boolean processPluginDependencies;

/**
* Whether to process the dependencies sections of plugins which are defined in pluginManagement.
*
* @since 2.5
*/
@Parameter( property = "processPluginDependenciesInPluginManagement", defaultValue = "true" )
protected boolean processPluginDependenciesInPluginManagement;

/**
* Whether to show additional information such as dependencies that do not need updating.
*
Expand All @@ -89,6 +107,41 @@ public class DisplayDependencyUpdatesMojo

// --------------------- GETTER / SETTER METHODS ---------------------

private static Set<Dependency> extractPluginDependenciesFromPluginsInPluginManagement( Build build )
{
Set<Dependency> result = new TreeSet<Dependency>( new DependencyComparator() );
if ( build.getPluginManagement() != null )
{
for ( Plugin plugin : build.getPluginManagement().getPlugins() )
{
if ( plugin.getDependencies() != null && !plugin.getDependencies().isEmpty() )
{
for ( Dependency pluginDependency : plugin.getDependencies() )
{
result.add( pluginDependency );
}
}
}
}
return result;
}

private static Set<Dependency> extractDependenciesFromPlugins( List<Plugin> plugins )
{
Set<Dependency> result = new TreeSet<Dependency>( new DependencyComparator() );
for ( Plugin plugin : plugins )
{
if ( plugin.getDependencies() != null && !plugin.getDependencies().isEmpty() )
{
for ( Dependency pluginDependency : plugin.getDependencies() )
{
result.add( pluginDependency );
}
}
}
return result;
}

/**
* Returns a set of dependencies where the dependencies which are defined in the dependency management section have
* been filtered out.
Expand All @@ -99,9 +152,9 @@ public class DisplayDependencyUpdatesMojo
* management dependencies.
* @since 1.0-beta-1
*/
private static Set removeDependencyManagment( Set dependencies, Set dependencyManagement )
private static Set<Dependency> removeDependencyManagment( Set dependencies, Set dependencyManagement )
{
Set result = new TreeSet( new DependencyComparator() );
Set<Dependency> result = new TreeSet<Dependency>( new DependencyComparator() );
for ( Iterator i = dependencies.iterator(); i.hasNext(); )
{
Dependency c = (Dependency) i.next();
Expand Down Expand Up @@ -139,6 +192,16 @@ public boolean isProcessingDependencies()
return processDependencies;
}

public boolean isProcessingPluginDependencies()
{
return processPluginDependencies;
}

public boolean isProcessPluginDependenciesInDependencyManagement()
{
return processPluginDependenciesInPluginManagement;
}

public boolean isVerbose()
{
return verbose;
Expand All @@ -159,7 +222,7 @@ public void execute()
{
logInit();

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

Expand Down Expand Up @@ -206,14 +269,28 @@ public void execute()
}
}

Set dependencies = new TreeSet( new DependencyComparator() );
Set<Dependency> dependencies = new TreeSet<Dependency>( new DependencyComparator() );
dependencies.addAll( getProject().getDependencies() );

if ( isProcessingDependencyManagement() )
{
dependencies = removeDependencyManagment( dependencies, dependencyManagement );
}

Set<Dependency> pluginDependencies = new TreeSet<Dependency>( new DependencyComparator() );

if ( isProcessingPluginDependencies() )
{
pluginDependencies = extractDependenciesFromPlugins( getProject().getBuildPlugins() );
}

Set<Dependency> pluginDependenciesInPluginManagement = new TreeSet<Dependency>( new DependencyComparator() );
if ( isProcessPluginDependenciesInDependencyManagement() )
{
pluginDependenciesInPluginManagement =
extractPluginDependenciesFromPluginsInPluginManagement( getProject().getBuild() );
}

try
{
if ( isProcessingDependencyManagement() )
Expand All @@ -225,6 +302,15 @@ public void execute()
{
logUpdates( getHelper().lookupDependenciesUpdates( dependencies, false ), "Dependencies" );
}
if ( isProcessPluginDependenciesInDependencyManagement() )
{
logUpdates( getHelper().lookupDependenciesUpdates( pluginDependenciesInPluginManagement, false ),
"pluginManagement of plugins" );
}
if ( isProcessingPluginDependencies() )
{
logUpdates( getHelper().lookupDependenciesUpdates( pluginDependencies, false ), "Plugin Dependencies" );
}
}
catch ( InvalidVersionSpecificationException e )
{
Expand All @@ -236,7 +322,7 @@ public void execute()
}
}

private void logUpdates( Map updates, String section )
private void logUpdates( Map<Dependency, ArtifactVersions> updates, String section )
{
List withUpdates = new ArrayList();
List usingCurrent = new ArrayList();
Expand Down Expand Up @@ -266,7 +352,7 @@ private void logUpdates( Map updates, String section )
}
}
String right = " " + ( latest == null ? current : current + " -> " + latest.toString() );
List t = latest == null ? usingCurrent : withUpdates;
List<String> t = latest == null ? usingCurrent : withUpdates;
if ( right.length() + left.length() + 3 > INFO_PAD_SIZE )
{
t.add( left + "..." );
Expand All @@ -278,27 +364,39 @@ private void logUpdates( Map updates, String section )
t.add( StringUtils.rightPad( left, INFO_PAD_SIZE - right.length(), "." ) + right );
}
}
if ( isVerbose() && usingCurrent.isEmpty() && !withUpdates.isEmpty() )
{
logLine( false, "No dependencies in " + section + " are using the newest version." );
logLine( false, "" );
}
else if ( isVerbose() && !usingCurrent.isEmpty() )

if ( isVerbose() )
{
logLine( false, "The following dependencies in " + section + " are using the newest version:" );
i = usingCurrent.iterator();
while ( i.hasNext() )
if ( usingCurrent.isEmpty() )
{
logLine( false, (String) i.next() );
if ( !withUpdates.isEmpty() )
{
logLine( false, "No dependencies in " + section + " are using the newest version." );
logLine( false, "" );
}
}
logLine( false, "" );
}
if ( withUpdates.isEmpty() && !usingCurrent.isEmpty() )
else
{
logLine( false, "The following dependencies in " + section + " are using the newest version:" );
i = usingCurrent.iterator();
while ( i.hasNext() )
{
logLine( false, (String) i.next() );
}
logLine( false, "" );
}
}


if ( withUpdates.isEmpty() )
{
logLine( false, "No dependencies in " + section + " have newer versions." );
logLine( false, "" );
if ( !usingCurrent.isEmpty() )
{
logLine( false, "No dependencies in " + section + " have newer versions." );
logLine( false, "" );
}
}
else if ( !withUpdates.isEmpty() )
else
{
logLine( false, "The following dependencies in " + section + " have newer versions:" );
i = withUpdates.iterator();
Expand Down

0 comments on commit 43e933d

Please sign in to comment.