Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/337 slf4j logging #339

Closed
wants to merge 16 commits into from
Closed
13 changes: 9 additions & 4 deletions pom.xml
Expand Up @@ -301,17 +301,22 @@
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>runtime</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
digulla marked this conversation as resolved.
Show resolved Hide resolved
<version>1.2.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -59,6 +58,8 @@
import org.codehaus.mojo.license.api.DependenciesToolException;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.transfer.ArtifactNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract mojo for all third-party mojos.
Expand All @@ -69,6 +70,7 @@
public abstract class AbstractAddThirdPartyMojo
extends AbstractLicenseMojo
{
private static final Logger LOG = LoggerFactory.getLogger( AbstractAddThirdPartyMojo.class );

// ----------------------------------------------------------------------
// Mojo Parameters
Expand Down Expand Up @@ -715,11 +717,8 @@ protected void init()
throws Exception
{

Log log = getLog();

if ( log.isDebugEnabled() )
if ( getLog().isDebugEnabled() )
{

// always be verbose in debug mode
setVerbose( true );
}
Expand All @@ -728,11 +727,8 @@ protected void init()

long buildTimestamp = getBuildTimestamp();

if ( isVerbose() || getLog().isDebugEnabled() )
{
log.debug( "Build start at : " + buildTimestamp );
log.debug( "third-party file : " + thirdPartyFile.lastModified() );
}
LOG.debug( "Build start at : " + buildTimestamp );
LOG.debug( "third-party file : " + thirdPartyFile.lastModified() );
digulla marked this conversation as resolved.
Show resolved Hide resolved

doGenerate = force || !thirdPartyFile.exists() || buildTimestamp > thirdPartyFile.lastModified();

Expand All @@ -741,10 +737,7 @@ protected void init()

File bundleFile = FileUtil.getFile( outputDirectory, bundleThirdPartyPath );

if ( isVerbose() || getLog().isDebugEnabled() )
{
log.debug( "bundle third-party file : " + bundleFile.lastModified() );
}
LOG.debug( "bundle third-party file : " + bundleFile.lastModified() );
digulla marked this conversation as resolved.
Show resolved Hide resolved
doGenerateBundle = force || !bundleFile.exists() || buildTimestamp > bundleFile.lastModified();
}
else
Expand All @@ -764,26 +757,26 @@ protected void init()
{
throw new MojoExecutionException( "You can't use both licenseMergesFile and licenseMergesUrl" );
}
getLog().warn( "" );
getLog().warn( "!!! licenseMergesFile is deprecated, use now licenseMergesUrl !!!" );
getLog().warn( "" );
getLog().warn( "licenseMerges will be overridden by licenseMergesFile." );
getLog().warn( "" );
LOG.warn( "" );
LOG.warn( "!!! licenseMergesFile is deprecated, use now licenseMergesUrl !!!" );
LOG.warn( "" );
LOG.warn( "licenseMerges will be overridden by licenseMergesFile." );
LOG.warn( "" );
licenseMerges = FileUtils.readLines( new File( licenseMergesFile ), "utf-8" );
}
else if ( licenseMergesUrl != null )
{
getLog().warn( "" );
getLog().warn( "licenseMerges will be overridden by licenseMergesUrl." );
getLog().warn( "" );
LOG.warn( "" );
LOG.warn( "licenseMerges will be overridden by licenseMergesUrl." );
LOG.warn( "" );
if ( UrlRequester.isStringUrl( licenseMergesUrl ) )
{
licenseMerges = Arrays.asList( UrlRequester.getFromUrl( licenseMergesUrl ).split( "[\n\r]+" ) );
}
}

resolvedOverrideUrl = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedOverrideUrl, overrideFile,
overrideUrl, project.getBasedir(), getLog() );
overrideUrl, project.getBasedir() );
}

void consolidate() throws IOException, ArtifactNotFoundException, ArtifactResolutionException, MojoFailureException,
Expand Down Expand Up @@ -874,7 +867,7 @@ protected ThirdPartyHelper getHelper()
{
helper = new DefaultThirdPartyHelper( getProject(), getEncoding(), isVerbose(), dependenciesTool,
thirdPartyTool, getProject().getRemoteArtifactRepositories(),
getProject().getRemoteProjectRepositories(), getLog() );
getProject().getRemoteProjectRepositories() );
}
return helper;
}
Expand Down Expand Up @@ -934,20 +927,19 @@ void checkUnsafeDependencies()
{
if ( CollectionUtils.isNotEmpty( unsafeDependencies ) )
{
Log log = getLog();
if ( log.isWarnEnabled() )
if ( LOG.isWarnEnabled() )
{
boolean plural = unsafeDependencies.size() > 1;
String message = String.format( "There %s %d %s with no license :",
plural ? "are" : "is",
unsafeDependencies.size(),
plural ? "dependencies" : "dependency" );
log.warn( message );
LOG.warn( message );
for ( MavenProject dep : unsafeDependencies )
{

// no license found for the dependency
log.warn( " - " + MojoHelper.getArtifactId( dep.getArtifact() ) );
LOG.warn( " - {}", MojoHelper.getArtifactId( dep.getArtifact() ) );
}
}
}
Expand All @@ -961,7 +953,7 @@ boolean checkForbiddenLicenses()
if ( CollectionUtils.isNotEmpty( blackLicenses ) )
{
Set<String> licenses = licenseMap.keySet();
getLog().info( "Excluded licenses (blacklist): " + blackLicenses );
LOG.info( "Excluded licenses (blacklist): {}", blackLicenses );

for ( String excludeLicense : blackLicenses )
{
Expand All @@ -977,20 +969,19 @@ boolean checkForbiddenLicenses()
if ( CollectionUtils.isNotEmpty( whiteLicenses ) )
{
Set<String> dependencyLicenses = licenseMap.keySet();
getLog().info( "Included licenses (whitelist): " + whiteLicenses );
LOG.info( "Included licenses (whitelist): {}", whiteLicenses );

for ( String dependencyLicense : dependencyLicenses )
{
getLog().debug( "Testing license '" + dependencyLicense + "'" );
LOG.debug( "Testing license '{}'", dependencyLicense );
if ( !whiteLicenses.contains( dependencyLicense )
&& CollectionUtils.isNotEmpty( licenseMap.get( dependencyLicense ) ) )
{
getLog().debug( "Testing dependency license '" + dependencyLicense
+ "' against all other licenses" );
LOG.debug( "Testing dependency license '{}' against all other licenses", dependencyLicense );

for ( MavenProject dependency : licenseMap.get( dependencyLicense ) )
{
getLog().debug( " testing dependency " + dependency );
LOG.debug( "- testing dependency {}" + dependency );

boolean forbiddenLicenseUsed = true;

Expand All @@ -1012,8 +1003,10 @@ boolean checkForbiddenLicenses()

if ( licenseMap.get( otherLicense ).contains( dependency ) )
{
getLog().info( "License: '" + dependencyLicense + "' for '" + dependency + "'is OK "
+ "since it is also licensed under '" + otherLicense + "'" );
LOG.info( "License: '{}' for '{}' is OK since it is also licensed under '{}'",
dependencyLicense,
dependency,
otherLicense );
// this dependency is licensed under another license from white list
forbiddenLicenseUsed = false;
break;
Expand All @@ -1035,8 +1028,7 @@ boolean checkForbiddenLicenses()

if ( !safe )
{
Log log = getLog();
log.warn( "There are " + unsafeLicenses.size() + " forbidden licenses used:" );
LOG.warn( "There are {} forbidden licenses used:", unsafeLicenses.size() );
for ( String unsafeLicense : unsafeLicenses )
{

Expand All @@ -1050,7 +1042,7 @@ boolean checkForbiddenLicenses()
{
sb.append( "\n -" ).append( MojoHelper.getArtifactName( dep ) );
}
log.warn( sb.toString() );
LOG.warn( "{}", sb );
}
}
}
Expand Down