Skip to content

Commit

Permalink
add plugin excludes feature for plugin-management report Added plugin…
Browse files Browse the repository at this point in the history
…ManagementExcludes list parameter.
  • Loading branch information
belingueres authored and rfscholte committed Jan 11, 2019
1 parent c4014fa commit 82f4bf2
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 8 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -81,6 +81,7 @@ under the License.
</contributor>
<contributor>
<name>Gabriel Belingueres</name>
<email>belingueres@gmail.com</email>
</contributor>
<contributor>
<name>Simon Wang</name>
Expand Down
18 changes: 18 additions & 0 deletions src/it/MPIR-375/invoker.properties
@@ -0,0 +1,18 @@
# 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.

invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:plugin-management
125 changes: 125 additions & 0 deletions src/it/MPIR-375/pom.xml
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<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.apache.maven.plugins.project-info-reports.its</groupId>
<artifactId>MPIR-375</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://maven.apache.org/plugins/it/${project.artifactId}</url>

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>keytool-maven-plugin</artifactId>
<versionRange>[1.5,)</versionRange>
<goals>
<goal>clean</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>@sitePluginVersion@</version>
</plugin>
<!--
Example plugin generating the Eclipse's "Plugin execution not covered by lifecycle configuration" error message
-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>keytool-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>init</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>@pom.version@</version>
<configuration>
<pluginManagementExcludes>
<pluginManagementExclude>org.eclipse.m2e:lifecycle-mapping</pluginManagementExclude>
</pluginManagementExcludes>
</configuration>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>@pom.version@</version>
<reportSets>
<reportSet>
<reports>
<report>plugin-management</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
29 changes: 29 additions & 0 deletions src/it/MPIR-375/verify.groovy
@@ -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 log = new File( basedir, 'build.log' );
assert !( log.text.contains("org.eclipse.aether.transfer.ArtifactNotFoundException: Failure to find org.eclipse.m2e:lifecycle-mapping:pom") );

plugins = new File( basedir, 'target/site/plugin-management.html' ).text;

// maven-javadoc-plugin is defined in pluginManagement and not excluded
assert plugins.contains( 'maven-javadoc-plugin' );

// org.eclipse.m2e:lifecycle-mapping plugin is excluded
assert !( plugins.contains( 'org.eclipse.m2e' ) );
assert !( plugins.contains( 'lifecycle-mapping' ) );
Expand Up @@ -26,6 +26,7 @@
import org.apache.maven.model.PluginManagement;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
Expand All @@ -51,6 +52,17 @@
public class PluginManagementReport
extends AbstractProjectInfoReport
{

/**
* Specify the excluded plugins. This can be a list of artifacts in the format
* groupId[:artifactId[:type[:version]]]. <br>
* Plugins matching any exclude will not be present in the report.
*
* @since 3.0.1
*/
@Parameter
private List<String> pluginManagementExcludes = null;

// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
Expand All @@ -61,7 +73,8 @@ public void executeReport( Locale locale )
PluginManagementRenderer r =
new PluginManagementRenderer( getLog(), getSink(), locale, getI18N( locale ),
project.getPluginManagement().getPlugins(), project, projectBuilder,
repositorySystem, getSession().getProjectBuildingRequest() );
repositorySystem, getSession().getProjectBuildingRequest(),
pluginManagementExcludes );
r.render();
}

Expand Down Expand Up @@ -102,6 +115,7 @@ public boolean canGenerateReport()
protected static class PluginManagementRenderer
extends AbstractProjectInfoRenderer
{

private final Log log;

private final List<Plugin> pluginManagement;
Expand All @@ -113,6 +127,8 @@ protected static class PluginManagementRenderer
private final RepositorySystem repositorySystem;

private final ProjectBuildingRequest buildingRequest;

private final List<String> excludes;

/**
* @param log {@link #log}
Expand All @@ -124,10 +140,12 @@ protected static class PluginManagementRenderer
* @param projectBuilder {@link ProjectBuilder}
* @param repositorySystem {@link RepositorySystem}
* @param buildingRequest {@link ProjectBuildingRequest}
* @param excludes the list of plugins to be excluded from the report
*/
public PluginManagementRenderer( Log log, Sink sink, Locale locale, I18N i18n, List<Plugin> plugins,
MavenProject project, ProjectBuilder projectBuilder,
RepositorySystem repositorySystem, ProjectBuildingRequest buildingRequest )
RepositorySystem repositorySystem, ProjectBuildingRequest buildingRequest,
List<String> excludes )
{
super( sink, i18n, locale );

Expand All @@ -142,6 +160,8 @@ public PluginManagementRenderer( Log log, Sink sink, Locale locale, I18N i18n, L
this.repositorySystem = repositorySystem;

this.buildingRequest = buildingRequest;

this.excludes = excludes;
}

@Override
Expand Down Expand Up @@ -200,13 +220,21 @@ private void renderSectionPluginManagement()

Artifact pluginArtifact = repositorySystem.createProjectArtifact( plugin.getGroupId(), plugin
.getArtifactId(), versionRange.toString() );

try
{
MavenProject pluginProject = projectBuilder.build( pluginArtifact, buildingRequest ).getProject();

tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
.getVersion(), pluginProject.getUrl() ) );
if ( isExcluded( pluginArtifact ) )
{
log.debug( "Excluding plugin " + pluginArtifact.getId() + " from report" );
}
else
{
MavenProject pluginProject =
projectBuilder.build( pluginArtifact, buildingRequest ).getProject();

tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(),
pluginProject.getVersion(), pluginProject.getUrl() ) );
}
}
catch ( ProjectBuildingException e )
{
Expand Down Expand Up @@ -255,5 +283,41 @@ public int compare( Plugin a1, Plugin a2 )
}
};
}

private boolean isExcluded( Artifact pluginArtifact )
{
if ( excludes == null )
{
return false;
}

for ( String pattern : excludes )
{
String[] subStrings = pattern.split( ":" );
subStrings = StringUtils.stripAll( subStrings );
String resultPattern = StringUtils.join( subStrings, ":" );

if ( compareDependency( resultPattern, pluginArtifact ) )
{
return true;
}
}

return false;
}

/**
* Compares the given pattern against the given artifact. The pattern should follow the format
* <code>groupId:artifactId:type:classifier:version</code>.
*
* @param pattern The pattern to compare the artifact with.
* @param artifact the artifact
* @return <code>true</code> if the artifact matches the pattern
*/
protected boolean compareDependency( String pattern, Artifact artifact )
{
// TODO: compare with a better pattern matcher, like class ArtifactMatcher from Enforcer rules plugin
return artifact.getId().startsWith( pattern );
}
}
}
Expand Up @@ -118,7 +118,52 @@ public void testReport()
TextBlock[] textBlocks = response.getTextBlocks();
assertEquals( getString( "report.plugin-management.title" ), textBlocks[0].getText() );
}


/**
* Test report with excludes (to solve MPIR-375)
*
* @throws Exception if any
*/
public void testReportEclipseM2EPluginLifecycleMapping()
throws Exception
{
generateReport( "plugin-management", "plugin-management-plugin-config-MPIR-375.xml" );
assertTrue( "Test html generated", getGeneratedReport( "plugin-management.html" ).exists() );

URL reportURL = getGeneratedReport( "plugin-management.html" ).toURI().toURL();
assertNotNull( reportURL );

// HTTPUnit
WebRequest request = new GetMethodWebRequest( reportURL.toString() );
WebResponse response = WEB_CONVERSATION.getResponse( request );

// Basic HTML tests
assertTrue( response.isHTML() );
assertTrue( response.getContentLength() > 0 );

// Test the Page title
String expectedTitle = prepareTitle( getString( "report.plugin-management.name" ),
getString( "report.plugin-management.title" ) );
assertEquals( expectedTitle, response.getTitle() );

// Test the tables
WebTable[] webTables = response.getTables();
assertEquals( 1, webTables.length );

// generated table for the plugin management
assertEquals( 3, webTables[0].getColumnCount() );
assertEquals( 2, webTables[0].getRowCount() );
// row 0 are the table titles
// row 1 is the m-javadoc-plugin
assertEquals( "org.apache.maven.plugins", webTables[0].getCellAsText( 1, 0 ) );
assertEquals( "maven-javadoc-plugin", webTables[0].getCellAsText( 1, 1 ) );
assertEquals( "3.0.1", webTables[0].getCellAsText( 1, 2 ) );

// Test the texts
TextBlock[] textBlocks = response.getTextBlocks();
assertEquals( getString( "report.plugin-management.title" ), textBlocks[0].getText() );
}

private static ProjectBuildingResult createProjectBuildingResult( Artifact artifact, String url )
{
ProjectBuildingResult result = mock( ProjectBuildingResult.class );
Expand Down

0 comments on commit 82f4bf2

Please sign in to comment.