Author: tchemit Date: 2008-11-21 15:39:57 +0000 (Fri, 21 Nov 2008) New Revision: 1246 Added: maven-license-switcher-plugin/tags/0.5/ maven-license-switcher-plugin/tags/0.5/changelog maven-license-switcher-plugin/tags/0.5/pom.xml maven-license-switcher-plugin/tags/0.5/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java Removed: maven-license-switcher-plugin/tags/0.5/changelog maven-license-switcher-plugin/tags/0.5/pom.xml maven-license-switcher-plugin/tags/0.5/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java Log: [maven-release-plugin] copy for tag 0.5 Copied: maven-license-switcher-plugin/tags/0.5 (from rev 1234, maven-license-switcher-plugin/trunk) Deleted: maven-license-switcher-plugin/tags/0.5/changelog =================================================================== --- maven-license-switcher-plugin/trunk/changelog 2008-11-17 11:45:24 UTC (rev 1234) +++ maven-license-switcher-plugin/tags/0.5/changelog 2008-11-21 15:39:57 UTC (rev 1246) @@ -1,7 +0,0 @@ -ver-0-4 chemit 20081013 - * 20081013 [chemit] Improve third-party goal : onyl keep included dependencies - -ver-0-3 chemit 20081013 - * 20081013 [chemit] Improve licence resolving - * 20080922 [thimel] Modified version of jrst and plugin's groupId - * 20080922 [chemit] Using lutinpluginproject 3.0 Copied: maven-license-switcher-plugin/tags/0.5/changelog (from rev 1244, maven-license-switcher-plugin/trunk/changelog) =================================================================== --- maven-license-switcher-plugin/tags/0.5/changelog (rev 0) +++ maven-license-switcher-plugin/tags/0.5/changelog 2008-11-21 15:39:57 UTC (rev 1246) @@ -0,0 +1,11 @@ +ver-0-5 chemit 200811?? + * 20081121 [chemit] fix bug when no name is defined in license + * 20081118 [chemit] use lutinpluginproject 3.1 + +ver-0-4 chemit 20081013 + * 20081013 [chemit] Improve third-party goal : onyl keep included dependencies + +ver-0-3 chemit 20081013 + * 20081013 [chemit] Improve licence resolving + * 20080922 [thimel] Modified version of jrst and plugin's groupId + * 20080922 [chemit] Using lutinpluginproject 3.0 Deleted: maven-license-switcher-plugin/tags/0.5/pom.xml =================================================================== --- maven-license-switcher-plugin/trunk/pom.xml 2008-11-17 11:45:24 UTC (rev 1234) +++ maven-license-switcher-plugin/tags/0.5/pom.xml 2008-11-21 15:39:57 UTC (rev 1246) @@ -1,104 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<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/maven-v4_0_0.xsd"> - - <modelVersion>4.0.0</modelVersion> - - <!-- ************************************************************* --> - <!-- *** POM Relationships *************************************** --> - <!-- ************************************************************* --> - <parent> - <groupId>org.codelutin</groupId> - <artifactId>lutinpluginproject</artifactId> - <version>3.0</version> - </parent> - - <artifactId>maven-license-switcher-plugin</artifactId> - - <dependencies> - - <dependency> - <groupId>org.apache.maven.shared</groupId> - <artifactId>maven-dependency-tree</artifactId> - <version>1.1</version> - </dependency> - - <dependency> - <groupId>org.codelutin</groupId> - <artifactId>lutinprocessor</artifactId> - <version>0.15</version> - <scope>compile</scope> - </dependency> - - <dependency> - <groupId>org.codelutin</groupId> - <artifactId>lutinpluginutil</artifactId> - <version>0.2</version> - <scope>compile</scope> - </dependency> - - </dependencies> - - <!-- ************************************************************* --> - <!-- *** Project Information ************************************* --> - <!-- ************************************************************* --> - <name>Plugin maven pour switcher les licenses</name> - <version>0.5-SNAPSHOT</version> - <description>Plugin pour switcher de license dans un module maven 2</description> - <inceptionYear>2008</inceptionYear> - - <!-- ************************************************************* --> - <!-- *** Build Settings ****************************************** --> - <!-- ************************************************************* --> - - <packaging>maven-plugin</packaging> - - <build> - <plugins> - <plugin> - <artifactId>maven-antrun-plugin</artifactId> - <executions> - <!--Copy licenses files for site--> - <execution> - <id>CopySiteFiles2</id> - <phase>pre-site</phase> - <configuration> - <tasks> - <mkdir dir="${maven.site.gen.dir}/resources" /> - <copy todir="${maven.site.gen.dir}/resources" verbose="${maven.verbose}" overwrite="false"> - <fileset dir="${maven.src.dir}/main/resources/META-INF"> - <include name="licenses/**" /> - </fileset> - </copy> - </tasks> - </configuration> - <goals> - <goal>run</goal> - </goals> - </execution> - </executions> - </plugin> - </plugins> - </build> - - <properties> - - <!-- id du projet du labs --> - <labs.id>12</labs.id> - - <!-- nom du projet sur le labs --> - <labs.project>lutinutil</labs.project> - - </properties> - - <!-- ************************************************************* --> - <!-- *** Build Environment ************************************** --> - <!-- ************************************************************* --> - - <!--Source control management--> - <scm> - <connection>${maven.scm.connection}</connection> - <developerConnection>${maven.scm.developerConnection}</developerConnection> - <url>${maven.scm.url}</url> - </scm> - -</project> Copied: maven-license-switcher-plugin/tags/0.5/pom.xml (from rev 1245, maven-license-switcher-plugin/trunk/pom.xml) =================================================================== --- maven-license-switcher-plugin/tags/0.5/pom.xml (rev 0) +++ maven-license-switcher-plugin/tags/0.5/pom.xml 2008-11-21 15:39:57 UTC (rev 1246) @@ -0,0 +1,106 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <!-- ************************************************************* --> + <!-- *** POM Relationships *************************************** --> + <!-- ************************************************************* --> + <parent> + <groupId>org.codelutin</groupId> + <artifactId>lutinpluginproject</artifactId> + <version>3.1</version> + </parent> + + <artifactId>maven-license-switcher-plugin</artifactId> + + <version>0.5</version> + + <dependencies> + + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-dependency-tree</artifactId> + <version>1.1</version> + </dependency> + + <dependency> + <groupId>org.codelutin</groupId> + <artifactId>lutinprocessor</artifactId> + <version>0.15</version> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.codelutin</groupId> + <artifactId>lutinpluginutil</artifactId> + <version>0.2</version> + <scope>compile</scope> + </dependency> + + </dependencies> + + <!-- ************************************************************* --> + <!-- *** Project Information ************************************* --> + <!-- ************************************************************* --> + <name>Plugin maven pour switcher les licenses</name> + + <description>Plugin pour switcher de license dans un module maven 2</description> + <inceptionYear>2008</inceptionYear> + + <!-- ************************************************************* --> + <!-- *** Build Settings ****************************************** --> + <!-- ************************************************************* --> + + <packaging>maven-plugin</packaging> + + <build> + <plugins> + <plugin> + <artifactId>maven-antrun-plugin</artifactId> + <executions> + <!--Copy licenses files for site--> + <execution> + <id>CopySiteFiles2</id> + <phase>pre-site</phase> + <configuration> + <tasks> + <mkdir dir="${maven.site.gen.dir}/resources" /> + <copy todir="${maven.site.gen.dir}/resources" verbose="${maven.verbose}" overwrite="false"> + <fileset dir="${maven.src.dir}/main/resources/META-INF"> + <include name="licenses/**" /> + </fileset> + </copy> + </tasks> + </configuration> + <goals> + <goal>run</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <properties> + + <!-- id du projet du labs --> + <labs.id>12</labs.id> + + <!-- nom du projet sur le labs --> + <labs.project>lutinutil</labs.project> + + </properties> + + <!-- ************************************************************* --> + <!-- *** Build Environment ************************************** --> + <!-- ************************************************************* --> + + <!--Source control management--> + <scm> + <connection>scm:svn:svn://anonymous@labs.libre-entreprise.org/svnroot/lutinutil/maven-license-switcher-plugin/tags/0.5</connection> + <developerConnection>scm:svn:svn+ssh://tchemit@labs.libre-entreprise.org/svnroot/lutinutil/maven-license-switcher-plugin/tags/0.5</developerConnection> + <url>http://labs.libre-entreprise.org/plugins/scmsvn/viewcvs.php/maven-license-switcher-plugin/tags/0.5?root=lutinutil</url> + </scm> + +</project> Deleted: maven-license-switcher-plugin/tags/0.5/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java 2008-11-17 11:45:24 UTC (rev 1234) +++ maven-license-switcher-plugin/tags/0.5/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java 2008-11-21 15:39:57 UTC (rev 1246) @@ -1,324 +0,0 @@ -/** - * *##% Plugin maven pour switcher les licenses - * Copyright (C) 2008 CodeLutin - * - * 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 - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.codelutin.license.plugin; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.metadata.ArtifactMetadataSource; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.ArtifactCollector; -import org.apache.maven.artifact.resolver.filter.ArtifactFilter; -import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; -import org.apache.maven.model.License; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.shared.dependency.tree.DependencyNode; -import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; -import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; -import org.codelutin.util.FileUtil; - -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; - -/** - * Le goal pour copier le fichier THIRD-PARTY.txt (contenant les licenses de toutes les dependances du projet) - * dans le classpath (et le generer s'il n'existe pas). - * - * @author chemit - * @goal third-party - * @phase process-classes - * @requiresDependencyResolution test - * @requiresProject true - */ -public class ThirdPartyMojo extends AbstractLicenseMojo { - - private static final String unknownLicenseMessage = "Unknown license"; - - /** - * Local Repository. - * - * @parameter expression="${localRepository}" - * @required - * @readonly - */ - protected ArtifactRepository localRepository; - - - /** - * Remote repositories used for the project. - * - * @parameter expression="${project.remoteArtifactRepositories}" - * @required - * @readonly - */ - protected List remoteRepositories; - - /** - * Fichier ou ecrire les licences des dependances. - * - * @parameter expression="${license-switcher.thirdPartyFile}" default-value="${project.build.outputDirectory}/THIRD-PARTY.txt" - * @required - */ - protected File thirdPartyFile; - - /** - * Dependency tree builder component. - * - * @component - */ - protected DependencyTreeBuilder dependencyTreeBuilder; - - /** - * Artifact Factory component. - * - * @component - */ - protected ArtifactFactory factory; - - /** - * Artifact metadata source component. - * - * @component - */ - protected ArtifactMetadataSource artifactMetadataSource; - - /** - * Artifact collector component. - * - * @component - */ - protected ArtifactCollector collector; - - /** - * Maven Project Builder component. - * - * @component - */ - protected MavenProjectBuilder mavenProjectBuilder; - - /** - * Un flag pour indiquer la regeneration des fichiers. - * - * @parameter expression="${license-switcher.doGenerate}" default-value="true" - */ - protected boolean doGenerate; - /** - * content of third party file (only computed if {@link #doGenerate} is active or the - * {@link #thirdPartyFile} does not exist. - */ - protected String thirdPartyFileContent; - - protected void init() throws Exception { - - // must generate if file does not exist - doGenerate = doGenerate || !thirdPartyFile.exists(); - - if ("pom".equals(project.getPackaging()) || "site".equals(project.getPackaging())) { - // nothing to be done for this type of packaging - doGenerate = false; - getLog().info("skip third-party goal for packaging " + project.getPackaging()); - return; - } - - if (doGenerate) { - - // prepare thirdPartyFileContent - - DependencyNode dependencyTreeNode = resolveProject(); - - LicenseMap licenseMap = new LicenseMap(); - - for (Object o : dependencyTreeNode.getChildren()) { - - buildLicenseMap((DependencyNode) o, licenseMap); - } - - thirdPartyFileContent = buildGroupedLicenses(licenseMap); - - // log dependencies with no license - SortedSet<String> dependenciesWithNoLicense = licenseMap.get(unknownLicenseMessage); - if (dependenciesWithNoLicense != null) { - for (String dep : dependenciesWithNoLicense) { - // no license found for the dependency - getLog().warn("no license found for dependency " + dep); - } - } - } - } - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - - super.execute(); - - if (doGenerate) { - if (verbose) { - getLog().info("writing third-party file : " + thirdPartyFile); - } - if (keepBackup && thirdPartyFile.exists()) { - if (verbose) { - getLog().info("backup " + thirdPartyFile); - } - thirdPartyFile.renameTo(new File(thirdPartyFile.getAbsolutePath() + '~')); - } - try { - FileUtil.writeString(thirdPartyFile, thirdPartyFileContent, encoding); - } catch (IOException e) { - throw new MojoExecutionException("could not write file " + thirdPartyFile + " for reason : " + e.getMessage(), e); - } - } - - } - - - /** @return resolve the dependency tree */ - protected DependencyNode resolveProject() { - try { - ArtifactFilter artifactFilter = new ScopeArtifactFilter(Artifact.SCOPE_TEST); - return dependencyTreeBuilder.buildDependencyTree(project, localRepository, factory, - artifactMetadataSource, artifactFilter, collector); - } - catch (DependencyTreeBuilderException e) { - getLog().error("Unable to build dependency tree.", e); - return null; - } - } - - protected void buildLicenseMap(DependencyNode node, LicenseMap licenseMap) { - if (node.getState() != DependencyNode.INCLUDED) { - // this dependency is not included, so do not treate it - if (verbose) { - getLog().info("do not include this dependency " + node.toNodeString()); - } - return; - } - Artifact artifact = node.getArtifact(); - - if (verbose && getLog().isDebugEnabled()) { - getLog().debug("treate node " + node.toNodeString()); - } - - if (!Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) { - try { - MavenProject artifactProject = getMavenProjectFromRepository(artifact); - String artifactName = getArtifactName(artifactProject); - - List licenses = artifactProject.getLicenses(); - - if (licenses.isEmpty()) { - // no license found for the dependency - licenseMap.put(unknownLicenseMessage, artifactName); - - } else { - for (Object license : licenses) { - licenseMap.put(((License) license).getName(), artifactName); - } - } - } - catch (ProjectBuildingException e) { - getLog().error("ProjectBuildingException error : ", e); - } - } - if (!node.getChildren().isEmpty()) { - for (Object o : node.getChildren()) { - buildLicenseMap((DependencyNode) o, licenseMap); - } - } - } - - protected String buildGroupedLicenses(LicenseMap licenseMap) { - StringBuilder sb = new StringBuilder(); - sb.append("List of third-party dependencies grouped by their license type."); - for (String licenseName : licenseMap.keySet()) { - sb.append("\n\n").append(licenseName).append(" : "); - - SortedSet<String> projects = licenseMap.get(licenseName); - - for (String projectName : projects) { - sb.append("\n * ").append(projectName); - } - } - return sb.toString(); - } - - protected String getArtifactName(MavenProject artifactProject) { - StringBuilder sb = new StringBuilder(); - - sb.append(artifactProject.getName()); - sb.append(" ("); - sb.append(artifactProject.getGroupId()); - sb.append(":"); - sb.append(artifactProject.getArtifactId()); - sb.append(":"); - sb.append(artifactProject.getVersion()); - sb.append(" - "); - String url = artifactProject.getUrl(); - sb.append(url == null ? "no url defined" : url); - sb.append(")"); - - return sb.toString(); - } - - /** - * Get the <code>Maven project</code> from the repository depending the <code>Artifact</code> given. - * - * @param artifact an artifact - * @return the Maven project for the given artifact - * @throws ProjectBuildingException if any - */ - protected MavenProject getMavenProjectFromRepository(Artifact artifact) - throws ProjectBuildingException { - - boolean allowStubModel = false; - - if (!"pom".equals(artifact.getType())) { - artifact = factory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), - artifact.getVersion(), artifact.getScope()); - allowStubModel = true; - } - - // TODO: we should use the MavenMetadataSource instead - return mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories, localRepository, - allowStubModel); - } - - protected class LicenseMap extends java.util.TreeMap<String, SortedSet<String>> { - private static final long serialVersionUID = 864199843545688069L; - - /** {@inheritDoc} */ - public SortedSet<String> put(String key, String value) { - // handle multiple values as a set to avoid duplicates - SortedSet<String> valueList = get(key); - if (valueList == null) { - valueList = new TreeSet<String>(); - } - if (getLog().isDebugEnabled()) { - getLog().debug("key:" + key + ",value: " + value); - } - valueList.add(value); - return put(key, valueList); - } - } -} Copied: maven-license-switcher-plugin/tags/0.5/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java (from rev 1244, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java) =================================================================== --- maven-license-switcher-plugin/tags/0.5/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java (rev 0) +++ maven-license-switcher-plugin/tags/0.5/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java 2008-11-21 15:39:57 UTC (rev 1246) @@ -0,0 +1,333 @@ +/** + * *##% Plugin maven pour switcher les licenses + * Copyright (C) 2008 CodeLutin + * + * 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.codelutin.license.plugin; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactCollector; +import org.apache.maven.artifact.resolver.filter.ArtifactFilter; +import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; +import org.apache.maven.model.License; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.shared.dependency.tree.DependencyNode; +import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; +import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; +import org.codelutin.util.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Le goal pour copier le fichier THIRD-PARTY.txt (contenant les licenses de toutes les dependances du projet) + * dans le classpath (et le generer s'il n'existe pas). + * + * @author chemit + * @goal third-party + * @phase process-classes + * @requiresDependencyResolution test + * @requiresProject true + */ +public class ThirdPartyMojo extends AbstractLicenseMojo { + + private static final String unknownLicenseMessage = "Unknown license"; + + /** + * Local Repository. + * + * @parameter expression="${localRepository}" + * @required + * @readonly + */ + protected ArtifactRepository localRepository; + + + /** + * Remote repositories used for the project. + * + * @parameter expression="${project.remoteArtifactRepositories}" + * @required + * @readonly + */ + protected List remoteRepositories; + + /** + * Fichier ou ecrire les licences des dependances. + * + * @parameter expression="${license-switcher.thirdPartyFile}" default-value="${project.build.outputDirectory}/THIRD-PARTY.txt" + * @required + */ + protected File thirdPartyFile; + + /** + * Dependency tree builder component. + * + * @component + */ + protected DependencyTreeBuilder dependencyTreeBuilder; + + /** + * Artifact Factory component. + * + * @component + */ + protected ArtifactFactory factory; + + /** + * Artifact metadata source component. + * + * @component + */ + protected ArtifactMetadataSource artifactMetadataSource; + + /** + * Artifact collector component. + * + * @component + */ + protected ArtifactCollector collector; + + /** + * Maven Project Builder component. + * + * @component + */ + protected MavenProjectBuilder mavenProjectBuilder; + + /** + * Un flag pour indiquer la regeneration des fichiers. + * + * @parameter expression="${license-switcher.doGenerate}" default-value="true" + */ + protected boolean doGenerate; + /** + * content of third party file (only computed if {@link #doGenerate} is active or the + * {@link #thirdPartyFile} does not exist. + */ + protected String thirdPartyFileContent; + + protected void init() throws Exception { + + // must generate if file does not exist + doGenerate = doGenerate || !thirdPartyFile.exists(); + + if ("pom".equals(project.getPackaging()) || "site".equals(project.getPackaging())) { + // nothing to be done for this type of packaging + doGenerate = false; + getLog().info("skip third-party goal for packaging " + project.getPackaging()); + return; + } + + if (doGenerate) { + + // prepare thirdPartyFileContent + + DependencyNode dependencyTreeNode = resolveProject(); + + LicenseMap licenseMap = new LicenseMap(); + + for (Object o : dependencyTreeNode.getChildren()) { + + buildLicenseMap((DependencyNode) o, licenseMap); + } + + thirdPartyFileContent = buildGroupedLicenses(licenseMap); + + // log dependencies with no license + SortedSet<String> dependenciesWithNoLicense = licenseMap.get(unknownLicenseMessage); + if (dependenciesWithNoLicense != null) { + for (String dep : dependenciesWithNoLicense) { + // no license found for the dependency + getLog().warn("no license found for dependency " + dep); + } + } + } + } + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + + super.execute(); + + if (doGenerate) { + if (verbose) { + getLog().info("writing third-party file : " + thirdPartyFile); + } + if (keepBackup && thirdPartyFile.exists()) { + if (verbose) { + getLog().info("backup " + thirdPartyFile); + } + thirdPartyFile.renameTo(new File(thirdPartyFile.getAbsolutePath() + '~')); + } + try { + FileUtil.writeString(thirdPartyFile, thirdPartyFileContent, encoding); + } catch (IOException e) { + throw new MojoExecutionException("could not write file " + thirdPartyFile + " for reason : " + e.getMessage(), e); + } + } + + } + + + /** @return resolve the dependency tree */ + protected DependencyNode resolveProject() { + try { + ArtifactFilter artifactFilter = new ScopeArtifactFilter(Artifact.SCOPE_TEST); + return dependencyTreeBuilder.buildDependencyTree(project, localRepository, factory, + artifactMetadataSource, artifactFilter, collector); + } + catch (DependencyTreeBuilderException e) { + getLog().error("Unable to build dependency tree.", e); + return null; + } + } + + protected void buildLicenseMap(DependencyNode node, LicenseMap licenseMap) { + if (node.getState() != DependencyNode.INCLUDED) { + // this dependency is not included, so do not treate it + if (verbose) { + getLog().info("do not include this dependency " + node.toNodeString()); + } + return; + } + Artifact artifact = node.getArtifact(); + + if (verbose && getLog().isDebugEnabled()) { + getLog().debug("treate node " + node.toNodeString()); + } + + if (!Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) { + try { + MavenProject artifactProject = getMavenProjectFromRepository(artifact); + String artifactName = getArtifactName(artifactProject); + + List licenses = artifactProject.getLicenses(); + + if (licenses.isEmpty()) { + // no license found for the dependency + licenseMap.put(unknownLicenseMessage, artifactName); + + } else { + for (Object o : licenses) { + if (o == null) { + getLog().warn("could not acquire the license for " + artifactName); + continue; + } + License license = (License) o; + String licenseKey = license.getName(); + if (license.getName() == null) { + licenseKey = license.getUrl(); + } + licenseMap.put(licenseKey, artifactName); + } + } + } + catch (ProjectBuildingException e) { + getLog().error("ProjectBuildingException error : ", e); + } + } + if (!node.getChildren().isEmpty()) { + for (Object o : node.getChildren()) { + buildLicenseMap((DependencyNode) o, licenseMap); + } + } + } + + protected String buildGroupedLicenses(LicenseMap licenseMap) { + StringBuilder sb = new StringBuilder(); + sb.append("List of third-party dependencies grouped by their license type."); + for (String licenseName : licenseMap.keySet()) { + sb.append("\n\n").append(licenseName).append(" : "); + + SortedSet<String> projects = licenseMap.get(licenseName); + + for (String projectName : projects) { + sb.append("\n * ").append(projectName); + } + } + return sb.toString(); + } + + protected String getArtifactName(MavenProject artifactProject) { + StringBuilder sb = new StringBuilder(); + + sb.append(artifactProject.getName()); + sb.append(" ("); + sb.append(artifactProject.getGroupId()); + sb.append(":"); + sb.append(artifactProject.getArtifactId()); + sb.append(":"); + sb.append(artifactProject.getVersion()); + sb.append(" - "); + String url = artifactProject.getUrl(); + sb.append(url == null ? "no url defined" : url); + sb.append(")"); + + return sb.toString(); + } + + /** + * Get the <code>Maven project</code> from the repository depending the <code>Artifact</code> given. + * + * @param artifact an artifact + * @return the Maven project for the given artifact + * @throws ProjectBuildingException if any + */ + protected MavenProject getMavenProjectFromRepository(Artifact artifact) + throws ProjectBuildingException { + + boolean allowStubModel = false; + + if (!"pom".equals(artifact.getType())) { + artifact = factory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), + artifact.getVersion(), artifact.getScope()); + allowStubModel = true; + } + + // TODO: we should use the MavenMetadataSource instead + return mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories, localRepository, + allowStubModel); + } + + protected class LicenseMap extends java.util.TreeMap<String, SortedSet<String>> { + private static final long serialVersionUID = 864199843545688069L; + + /** {@inheritDoc} */ + public SortedSet<String> put(String key, String value) { + // handle multiple values as a set to avoid duplicates + SortedSet<String> valueList = get(key); + if (valueList == null) { + valueList = new TreeSet<String>(); + } + if (getLog().isDebugEnabled()) { + getLog().debug("key:" + key + ",value: " + value); + } + valueList.add(value); + return put(key, valueList); + } + } +}