org.codehaus.mojo.license.AddThirdPartyMojo Maven / Gradle / Ivy
package org.codehaus.mojo.license;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
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.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.license.api.MavenProjectDependenciesConfigurator;
import org.codehaus.mojo.license.api.ThirdPartyToolException;
import org.codehaus.mojo.license.model.LicenseMap;
import org.codehaus.mojo.license.utils.FileUtil;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.codehaus.mojo.license.utils.SortedProperties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
/**
* Goal to generate the third-party license file.
*
* This file contains a list of the dependencies and their licenses. Each dependency and its
* license is displayed on a single line in the format
*
* (<license-name>) <project-name> <groupId>:<artifactId>:<version> - <project-url>
*
* The directory containing the license database file is added to the classpath as an additional resource.
*
* @author tchemit [email protected]
* @since 1.0
*/
@Mojo( name = "add-third-party", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.GENERATE_RESOURCES )
public class AddThirdPartyMojo extends AbstractAddThirdPartyMojo implements MavenProjectDependenciesConfigurator
{
// ----------------------------------------------------------------------
// Mojo Parameters
// ----------------------------------------------------------------------
/**
* To skip execution of this mojo.
*
* @since 1.5
*/
@Parameter( property = "license.skipAddThirdParty", defaultValue = "false" )
private boolean skipAddThirdParty;
// ----------------------------------------------------------------------
// Private Fields
// ----------------------------------------------------------------------
/**
* Internal flag to know if missing file must be generated.
*/
private boolean doGenerateMissing;
// ----------------------------------------------------------------------
// AbstractLicenseMojo Implementaton
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public boolean isSkip()
{
return skipAddThirdParty;
}
/**
* {@inheritDoc}
*/
@Override
protected boolean checkPackaging()
{
if ( acceptPomPackaging )
{
// rejects nothing
return true;
}
// can reject pom packaging
return rejectPackaging( "pom" );
}
/**
* {@inheritDoc}
*/
@Override
protected boolean checkSkip()
{
if ( !isDoGenerate() && !isDoGenerateBundle() && !doGenerateMissing )
{
getLog().info( "All files are up to date, skip goal execution." );
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
@Override
protected void doAction()
throws Exception
{
consolidate();
boolean unsafe = checkUnsafeDependencies();
boolean safeLicense = checkForbiddenLicenses();
final boolean unlistedDependencies = checkUnlistedDependencies();
checkBlacklist(safeLicense);
checkDependenciesWhiteList(unlistedDependencies);
getPluginContext().put(AbstractDownloadLicensesMojo.LICENSE_MAP_KEY, licenseMap);
licenseMap.removeEmptyLicenses();
writeThirdPartyFile();
writeThirdPartyDependenciesFile();
if ( doGenerateMissing )
{
writeMissingFile();
}
checkMissing(unsafe);
if ( !unsafe && isUseMissingFile() && MapUtils.isEmpty( getUnsafeMappings() ) && getMissingFile().exists() )
{
// there is no missing dependencies, but still a missing file, delete it
getLog().info( "There is no dependency to put in missing file, delete it at " + getMissingFile() );
FileUtil.deleteFile( getMissingFile() );
}
if ( !unsafe && deployMissingFile && MapUtils.isNotEmpty( getUnsafeMappings() ) )
{
// can deploy missing file
File file = getMissingFile();
getLog().info( "Will attach third party file from " + file );
getHelper().attachThirdPartyDescriptor( file );
}
addResourceDir( getOutputDirectory(), "**/*.txt", "**/*.json" );
}
// ----------------------------------------------------------------------
// AbstractAddThirdPartyMojo Implementation
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
protected SortedMap loadDependencies()
{
return getHelper().loadDependencies( this );
}
/**
* {@inheritDoc}
*/
@Override
protected SortedProperties createUnsafeMapping()
throws ProjectBuildingException, IOException, ThirdPartyToolException
{
SortedSet unsafeDependencies = getUnsafeDependencies();
SortedProperties unsafeMappings =
getHelper().createUnsafeMapping( getLicenseMap(), getMissingFile(), useRepositoryMissingFiles,
unsafeDependencies, getProjectDependencies() );
if ( isVerbose() )
{
getLog().info( "found " + unsafeMappings.size() + " unsafe mappings" );
}
// compute if missing file should be (re)-generate
doGenerateMissing = computeDoGenerateMissingFile( unsafeMappings, unsafeDependencies );
if ( doGenerateMissing && isVerbose() )
{
StringBuilder sb = new StringBuilder();
sb.append( "Will use from missing file " );
sb.append( unsafeMappings.size() );
sb.append( " dependencies :" );
for ( Map.Entry