Skip to content

Commit

Permalink
Fix #283 Add artifactFiltersUrl parameter to all mojos that support
Browse files Browse the repository at this point in the history
artifact includes/excludes
  • Loading branch information
ppalaga committed Feb 22, 2019
1 parent 8ba4c63 commit 459870d
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 44 deletions.
3 changes: 3 additions & 0 deletions src/it/download-licenses-configured/artifact-filters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# comment

exclude gaPattern asm:asm
2 changes: 1 addition & 1 deletion src/it/download-licenses-configured/invoker.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
invoker.goals = clean validate
invoker.goals = clean validate -e
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<licenseSummary>
<dependencies>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
<licenses>
<license>
<name>Public Domain</name>
</license>
</licenses>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0</version>
<licenses>
<!-- No license information available. -->
</licenses>
</dependency>
</dependencies>
</licenseSummary>
18 changes: 18 additions & 0 deletions src/it/download-licenses-configured/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
Check default execution.
</description>

<properties>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>commons-logging</groupId>
Expand Down Expand Up @@ -70,6 +74,20 @@
</licenseUrlFileNameSanitizers>
</configuration>
</execution>
<execution>
<id>artifact-filters-url</id>
<phase>validate</phase>
<goals>
<goal>download-licenses</goal>
</goals>
<configuration>
<licensesConfigFile>${basedir}/src/license/licenses-config-artifact-filters-url.xml</licensesConfigFile>
<licensesOutputDirectory>${project.build.directory}/artifact-filters-url/licenses</licensesOutputDirectory>
<licensesOutputFile>${project.build.directory}/artifact-filters-url/licenses.xml</licensesOutputFile>
<sortByGroupIdAndArtifactId>true</sortByGroupIdAndArtifactId>
<artifactFiltersUrl>${project.baseUri}artifact-filters.txt</artifactFiltersUrl>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
11 changes: 11 additions & 0 deletions src/it/download-licenses-configured/postbuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ return {
assert Files.exists(bsdAsm)
assert bsdAsm.text.contains('ASM: a very small and fast Java bytecode manipulation framework')

final Path expectedLicensesXml = basePath.resolve('licenses-'+ id +'.expected.xml')
final Path licensesXml = outputBase.resolve('licenses.xml')
assert expectedLicensesXml.text.equals(licensesXml.text)
return true
}() && {
final String id = 'artifact-filters-url'
final Path outputBase = basePath.resolve('target/' + id)

final Path bsdAsm = outputBase.resolve('licenses/bsd 3-clause asm - license.txt')
assert !Files.exists(bsdAsm)

final Path expectedLicensesXml = basePath.resolve('licenses-'+ id +'.expected.xml')
final Path licensesXml = outputBase.resolve('licenses.xml')
assert expectedLicensesXml.text.equals(licensesXml.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,26 @@ public abstract class AbstractAddThirdPartyMojo
*/
private Map<String, String> globalKnownLicenses;

/**
* A URL returning a plain text file that contains include/exclude artifact filters in the following format:
* <pre>
* {@code
* # this is a comment
* include gaPattern org\.my-org:my-artifact
* include gaPattern org\.other-org:other-artifact
* exclude gaPattern org\.yet-anther-org:.*
* include scope compile
* include scope test
* exclude scope system
* include type jar
* exclude type war
* }</pre>
*
* @since 1.18
*/
@Parameter( property = "license.artifactFiltersUrl" )
protected String artifactFiltersUrl;

// ----------------------------------------------------------------------
// Abstract Methods
// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,26 @@ public abstract class AbstractDownloadLicensesMojo
@Parameter( property = "license.includedTypes" )
private String includedTypes;

/**
* A URL returning a plain text file that contains include/exclude artifact filters in the following format:
* <pre>
* {@code
* # this is a comment
* include gaPattern org\.my-org:my-artifact
* include gaPattern org\.other-org:other-artifact
* exclude gaPattern org\.yet-anther-org:.*
* include scope compile
* include scope test
* exclude scope system
* include type jar
* exclude type war
* }</pre>
*
* @since 1.18
*/
@Parameter( property = "license.artifactFiltersUrl" )
private String artifactFiltersUrl;

/**
* Settings offline flag (will not download anything if setted to true).
*
Expand Down Expand Up @@ -636,6 +656,15 @@ public void execute()

initDirectories();

if ( licensesOutputFileEncoding == null )
{
licensesOutputFileEncoding = System.getProperty( "file.encoding" );
getLog().warn( "Using the default system encoding for reading or writing licenses.xml file."
+ " This makes your build platform dependent. You should set either"
+ " project.build.sourceEncoding or licensesOutputFileEncoding" );
}
final Charset charset = Charset.forName( licensesOutputFileEncoding );

final LicenseMatchers matchers = LicenseMatchers.load( licensesConfigFile );

final Set<MavenProject> dependencies = getDependencies();
Expand Down Expand Up @@ -684,14 +713,6 @@ public void execute()
sortByGroupIdAndArtifactId( depProjectLicenses );
}

if ( licensesOutputFileEncoding == null )
{
licensesOutputFileEncoding = System.getProperty( "file.encoding" );
getLog().warn( "Using the default system encoding for reading or writing licenses.xml file."
+ " This makes your build platform dependent. You should set either"
+ " project.build.sourceEncoding or licensesOutputFileEncoding" );
}
final Charset charset = Charset.forName( licensesOutputFileEncoding );
if ( licensesOutputFileEol == Eol.AUTODETECT )
{
final Path autodetectFromFile = licensesConfigFile.exists() ? licensesConfigFile.toPath()
Expand Down Expand Up @@ -882,6 +903,18 @@ public String getExcludedArtifacts()
return excludedArtifacts;
}

/** {@inheritDoc} */
public String getArtifactFiltersUrl()
{
return artifactFiltersUrl;
}

/** {@inheritDoc} */
public String getEncoding()
{
return licensesOutputFileEncoding;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,26 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
@Component
private ThirdPartyTool thirdPartyTool;

/**
* A URL returning a plain text file that contains include/exclude artifact filters in the following format:
* <pre>
* {@code
* # this is a comment
* include gaPattern org\.my-org:my-artifact
* include gaPattern org\.other-org:other-artifact
* exclude gaPattern org\.yet-anther-org:.*
* include scope compile
* include scope test
* exclude scope system
* include type jar
* exclude type war
* }</pre>
*
* @since 1.18
*/
@Parameter( property = "license.artifactFiltersUrl" )
private String artifactFiltersUrl;

// ----------------------------------------------------------------------
// Protected Abstract Methods
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -658,4 +678,17 @@ else if ( !dependenciesWithNoLicense.contains( dependency ) )
return details;
}

/** {@inheritDoc} */
public String getArtifactFiltersUrl()
{
return artifactFiltersUrl;
}

/** {@inheritDoc} */
public String getEncoding()
{
return encoding;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ public String getIncludedArtifacts()
return includedArtifacts;
}

/** {@inheritDoc} */
public String getArtifactFiltersUrl()
{
return artifactFiltersUrl;
}

/**
* {@inheritDoc}
*/
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/org/codehaus/mojo/license/api/ArtifactFilters.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
* #L%
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import org.apache.maven.artifact.Artifact;
import org.codehaus.mojo.license.utils.UrlRequester;

/**
* Artifact filtering by
Expand Down Expand Up @@ -63,6 +66,23 @@ public static ArtifactFilters of( MavenProjectDependenciesConfigurator config )
builder.includeTypes( config.getIncludedTypes() );
builder.excludeTypes( config.getExcludedTypes() );

final String url = config.getArtifactFiltersUrl();
if ( url != null )
{
try
{
final String content = UrlRequester.getFromUrl( url, config.getEncoding() );
if ( content != null )
{
builder.script( url, content );
}
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}

return builder.build();
}

Expand Down Expand Up @@ -128,6 +148,72 @@ public ArtifactFilters build()
return new ArtifactFilters( scopeFilters.build(), typeFilters.build(), gaFilters.build() );
}

public void script( String url, String content )
{
final StringTokenizer st = new StringTokenizer( content, "\r\n" );
int i = 1;
while ( st.hasMoreTokens() )
{
final String line = st.nextToken().trim();
if ( !line.startsWith( "#" ) && !line.isEmpty() )
{
final String[] items = line.split( "\\s+" );
if ( items.length != 3 )
{
throw new IllegalStateException( "Expected 3 space separated tokens on line " + i + " in " + url
+ " found: " + line );
}
if ( "include".equals( items[0] ) )
{
if ( "gaPattern".equals( items[1] ) )
{
includeGa( items[2] );
}
else if ( "scope".equals( items[1] ) )
{
includeScope( items[2] );
}
else if ( "type".equals( items[1] ) )
{
includeType( items[2] );
}
else
{
throw new IllegalStateException( "Expected \"gaPattern\", \"scope\" or \"type\" after \""
+ items[0] + "\" on line " + i + " in " + url + " found: " + line );
}
}
else if ( "exclude".equals( items[0] ) )
{
if ( "gaPattern".equals( items[1] ) )
{
excludeGa( items[2] );
}
else if ( "scope".equals( items[1] ) )
{
excludeScope( items[2] );
}
else if ( "type".equals( items[1] ) )
{
excludeType( items[2] );
}
else
{
throw new IllegalStateException( "Expected \"gaPattern\", \"scope\" or \"type\" after \""
+ items[0] + "\" on line " + i + " in " + url + " found: " + line );
}
}
else
{
throw new IllegalStateException(
"Expected a line starting with \"include\" or \"exclude\" on line "
+ i + " in " + url + " found: " + line );
}
}
i++;
}
}

public Builder excludeGa( String pattern )
{
if ( pattern != null )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ public interface MavenProjectDependenciesConfigurator
*/
String getExcludedArtifacts();

/**
* Returns a URL returning a plain text file that contains include/exclude artifact filters in the following format:
* <pre>
* {@code
* # this is a comment
* include gaPattern org\.my-org:my-artifact
* include gaPattern org\.other-org:other-artifact
* exclude gaPattern org\.yet-anther-org:.*
* include scope compile
* include scope test
* exclude scope system
* include type jar
* exclude type war
* }</pre>
*
* @return a URL returning a plain text file
* @since 1.18
*/
String getArtifactFiltersUrl();

/**
* @return the encoding to use when reading {@link #getArtifactFiltersUrl()} unless it is an HTTP URL returning a
* valid charset in the {@code Content-Type} response header.
*/
String getEncoding();

/**
* @return {@code true} if verbose mode is on, {@code false} otherwise.
*/
Expand Down
Loading

0 comments on commit 459870d

Please sign in to comment.