Author: tchemit Date: 2008-08-18 22:16:08 +0000 (Mon, 18 Aug 2008) New Revision: 1050 Added: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AbstractLicenceMojo.java trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseMojo.java Modified: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AvailableLicensesMojo.java trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseType.java trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/SwitchLicenseMojo.java trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/ThirdPartyMojo.java trunk/maven-license-switcher-plugin/src/site/apt/index.apt trunk/maven-license-switcher-plugin/src/site/apt/usage.apt trunk/maven-license-switcher-plugin/src/test/java/org/codelutin/license/LicenseTypeTest.java Log: version finale du plugin license-switcher Added: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AbstractLicenceMojo.java =================================================================== --- trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AbstractLicenceMojo.java (rev 0) +++ trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AbstractLicenceMojo.java 2008-08-18 22:16:08 UTC (rev 1050) @@ -0,0 +1,107 @@ +/** + * *##% 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; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.codelutin.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +/** + * Un MOJO de base pour les autres MOJO concrets avec les options communes. + * + * @author chemit + */ +public abstract class AbstractLicenceMojo extends AbstractMojo { + + /** + * la methode qui est lancee au debut de la methode {@link #execute()} pour preparer l'init du goal. + * + * @throws Exception if any + */ + protected abstract void init() throws Exception; + + /** + * Dependance du projet. + * + * @parameter default-value="${project}" + * @required + * @readonly + */ + protected MavenProject project; + + /** + * Repertoire de sortie des classes (classpath). + * + * @parameter expression="${licence-switcher.outputDirectory}" default-value="${project.build.outputDirectory}" + * @required + */ + protected File projectOutputDirectory; + + /** + * Encoding a utiliser pour lire et ecrire les fichiers. + * + * @parameter expression="${licence-switcher.encoding}" default-value="${maven.compile.encoding}" + * @required + */ + protected String encoding; + + /** + * Un flag pour conserver un backup des fichiers modifies. + * + * @parameter expression="${license-switcher.keepBackup}" default-value="false" + */ + protected boolean keepBackup; + /** + * Un flag pour activer le mode verbeux. + * + * @parameter expression="${license-switcher.verbose}" default-value="false" + */ + protected boolean verbose; + + public void execute() throws MojoExecutionException, MojoFailureException { + try { + init(); + } catch (Exception e) { + throw new MojoExecutionException("could not init goal " + getClass().getSimpleName() + " for reason : " + e.getMessage(), e); + } + } + + protected void copyFileToOutputDirectory(File file) throws MojoExecutionException { + + final String type = project.getArtifact().getType(); + if ("pom".equals(type) || "site".equals(type)) { + // project is not classpath cabable + return; + } + File target = new File(projectOutputDirectory, file.getName()); + + try { + + FileUtil.copy(file, target); + + } catch (IOException e) { + throw new MojoExecutionException("could not write file " + target + " for reason : " + e.getMessage(), e); + } + } +} Modified: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AvailableLicensesMojo.java =================================================================== --- trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AvailableLicensesMojo.java 2008-08-18 19:14:39 UTC (rev 1049) +++ trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/AvailableLicensesMojo.java 2008-08-18 22:16:08 UTC (rev 1050) @@ -3,8 +3,8 @@ * 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 + * 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, @@ -12,7 +12,7 @@ * 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 + * 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>. ##%* */ Added: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseMojo.java =================================================================== --- trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseMojo.java (rev 0) +++ trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseMojo.java 2008-08-18 22:16:08 UTC (rev 1050) @@ -0,0 +1,108 @@ +/** + * *##% 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; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codelutin.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +/** + * Le goal pour ajouter le fichier LICENSE.txt dans le classpath (et le generer s'il n'existe pas). + * + * @author chemit + * @goal license + * @phase process-classes + * @requiresProject true + */ +public class LicenseMojo extends AbstractLicenceMojo { + /** + * Un flag pour indiquer la regeneration des fichiers. + * + * @parameter expression="${license-switcher.doGenerate}" default-value="false" + */ + protected boolean doGenerate; + /** + * Fichier de la licence du module. + * + * @parameter expression="${license-switcher.licenceFile}" default-value="${basedir}/LICENSE.txt" + * @required + * @readonly + */ + protected File licenseFile; + + /** + * Le type de license a appliquer. + * + * @parameter expression="${license-switcher.licenseType}" default-value="LGPL_V3" + * @required + */ + protected String licenseType; + + /** le header a ajouter dans chaque fichier java */ + protected String licenseHeaderContent; + + /** le texte de la licence a placer dans le fichier ${basedir}/LICENSE.txt */ + protected String licenseFileContent; + + protected void init() throws Exception { + + // must generate if file does not exist + doGenerate = doGenerate || !licenseFile.exists(); + + if (doGenerate) { + + // prepare licenseFileContent + + LicenseType type = LicenseType.valueOf(licenseType); + + licenseFileContent = FileUtil.readAsString(type.getLicenceFileReader(encoding)); + } + } + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + + super.execute(); + + if (doGenerate) { + + if (licenseFile.exists() && keepBackup) { + if (verbose) { + getLog().info("backup " + licenseFile); + } + // copy it to backup file + File backup = new File(licenseFile.getAbsolutePath() + "~"); + licenseFile.renameTo(backup); + } + try { + FileUtil.writeString(licenseFile, licenseFileContent, encoding); + + } catch (IOException e) { + throw new MojoExecutionException("could not write license file " + licenseFile + " for reason : " + e.getMessage(), e); + } + } + + // copy LICENSE.txt to classpath + copyFileToOutputDirectory(licenseFile); + } + +} Modified: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseType.java =================================================================== --- trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseType.java 2008-08-18 19:14:39 UTC (rev 1049) +++ trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/LicenseType.java 2008-08-18 22:16:08 UTC (rev 1050) @@ -3,8 +3,8 @@ * 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 + * 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, @@ -12,7 +12,7 @@ * 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 + * 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>. ##%* */ Modified: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/SwitchLicenseMojo.java =================================================================== --- trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-08-18 19:14:39 UTC (rev 1049) +++ trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-08-18 22:16:08 UTC (rev 1050) @@ -1,10 +1,9 @@ -/** - * *##% Plugin maven pour switcher les licenses +/*##% 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 + * 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, @@ -12,16 +11,14 @@ * 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 + * 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; -import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.DirectoryScanner; import org.codelutin.processor.LicenseProcessor; import org.codelutin.processor.filters.LicenseFilter; @@ -30,29 +27,25 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; /** - * Le goal pour switcher les licenses de tous les sources java d'un module maven et de positionner le fichier LICENSE.txt + * Le goal pour switcher les licenses de tous les sources java d'un module + * maven. * * @author chemit * @goal switch - * @phase process-sources - * @execute goal=third-party + * @phase process-resources * @requiresProject true */ -public class SwitchLicenseMojo extends AbstractMojo { +public class SwitchLicenseMojo extends AbstractLicenceMojo { /** - * Dependance du projet. - * - * @parameter default-value="${project}" - * @readonly - */ - protected MavenProject project; - - /** * l'annee de creation du module (sera place dans le header) * * @parameter default-value="${project.inceptionYear}" @@ -61,7 +54,7 @@ protected String inceptionYear; /** - * le nom de l'organisation ( sera place dans le header) + * le nom de l'organisation (sera place dans le header) * * @parameter default-value="${project.organization.name}" * @required @@ -70,7 +63,7 @@ protected String organizationName; /** - * le nom du projet ( sera place dans le header) + * le nom du projet (sera place dans le header) * * @parameter default-value="${project.name}" * @required @@ -78,128 +71,163 @@ */ protected String projectName; - /** - * encoding a utiliser pour lire et ecrire les fichiers. + * Le type de license a appliquer. * - * @parameter expression="${licence-switcher.encoding}" default-value="${maven.compile.encoding}" + * @parameter expression="${license-switcher.licenseType}" default-value="LGPL_V3" * @required */ - protected String encoding; + protected String licenseType; /** - * Repertoire des fichiers sources a traiter. + * Repertoires des fichiers sources a traiter. * - * @parameter expression="${license-switcher.src}" default-value="${basedir}/src/main/java" + * @parameter expression="${license-switcher.compileSourceRoots}" default-value="${project.compileSourceRoots}" * @required - * @readonly */ - protected File src; + protected List<String> compileSourceRoots; /** - * Fichier de la licence du module. + * Repertoires des fichiers sources de test a traiter. * - * @parameter expression="${license-switcher.licenceFile}" default-value="${basedir}/LICENSE.txt" + * @parameter expression="${license-switcher.testCompileSourceRoots}" default-value="${project.testCompileSourceRoots}" * @required - * @readonly */ - protected File licenseFile; + protected List<String> testCompileSourceRoots; /** - * Le type de license a appliquer. + * Un flag pour indiquer de lancer le goal. * - * @parameter expression="${license-switcher.licenseType}" default-value="LGPL_V3" + * @parameter expression="${license-switcher.doSwitch}" default-value="false" * @required */ - protected String licenseType; + protected boolean doSwitch; - /** - * un flag pour conserver un backup des fichiers modifies - * - * @parameter expression="${license-switcher.keepBackup}" default-value="false" - */ - protected boolean keepBackup; + /** le header a ajouter dans chaque fichier source java */ + protected String licenseHeaderContent; - /** - * un flag pour indiquer la recopie vers le repertoire des resources. - * - * @parameter expression="${license-switcher.copyToResource}" default-value="true" - */ - protected boolean copyToResource; + /** la liste des chemin relatifs des sources java a traiter pour chaque repertoire contenant des sources */ + protected Map<File, String[]> javaFilesToTreate; - /** - * un flag pour avoir les traces - * - * @parameter expression="${license-switcher.verbose}" default-value="false" - */ - protected boolean verbose; + protected long timestamp; + protected void init() throws IOException { - /** le header a ajouter dans chaque fichier java */ - protected String licenseHeaderContent; + timestamp = System.nanoTime(); - /** le texte de la licence a placer dans le fichier ${basedir}/LICENSE.txt */ - protected String licenseFileContent; + LicenseType type = LicenseType.valueOf(licenseType); - /** la liste des chemin relatifs des sources java a traiter a partir de {@link #src} */ - protected String[] javaFilesToTreate; + // obtain all java source files to be treated + javaFilesToTreate = getFilesToTreate(); - protected long timestamp; + if (javaFilesToTreate.isEmpty()) { + // nothing to do, since no file to treate was found + return; + } + // obtain content of license header + licenseHeaderContent = computeHeader(type); + + + if (verbose) { + getLog().info("header to write on java source files \n" + licenseHeaderContent); + } + + } + + @Override public void execute() throws MojoExecutionException, MojoFailureException { - try { - init(); - } catch (IOException e) { - throw new MojoExecutionException("could not init goal for reason : " + e.getMessage(), e); + + if (!doSwitch) { + getLog().info("goal was not executed, you must specify the license-switcher.doSwitch to launch the goal"); + return; } - // write project license - writeLicense(); + super.execute(); - if (javaFilesToTreate.length == 0) { - getLog().warn("no java source files to treate."); + if (javaFilesToTreate.isEmpty()) { + getLog().warn("no java source files found to be treated."); return; } // create a licence processor with given header LicenseProcessor p = new LicenseProcessor(licenseHeaderContent); - for (String javaRelativePath : javaFilesToTreate) { - - File sourceFile = new File(src, javaRelativePath); - - try { - processJavaSource(p, sourceFile); - } catch (Exception e) { - throw new MojoExecutionException("could not treate java source file " + sourceFile + " for reason : " + e.getMessage(), e); + for (Entry<File, String[]> entry : javaFilesToTreate.entrySet()) { + File src = entry.getKey(); + for (String javaRelativePath : entry.getValue()) { + File sourceFile = new File(src, javaRelativePath); + try { + processJavaSource(p, sourceFile); + } catch (Exception e) { + throw new MojoExecutionException("could not treate java source file " + sourceFile + " for reason : " + e.getMessage(), e); + } } } } - protected void writeLicense() throws MojoExecutionException { - if (licenseFile.exists() && keepBackup) { + protected Map<File, String[]> getFilesToTreate() { + + // init directory scanner + DirectoryScanner ds = new DirectoryScanner(); + ds.setIncludes(new String[]{"**\\/*.java"}); + + Map<File, String[]> files = new java.util.HashMap<File, String[]>(); + + List<String> roots = new ArrayList<String>(); + + roots.addAll(compileSourceRoots); + roots.addAll(testCompileSourceRoots); + + for (String src : roots) { if (verbose) { - getLog().info("backup " + licenseFile); + getLog().info("discovering java source files in root " + src); } - // copy it to backup file - File backup = new File(licenseFile.getAbsolutePath() + "~"); - licenseFile.renameTo(backup); + File f = new File(src); + ds.setBasedir(f); + // scan + ds.scan(); + + // get files + String[] tmp = ds.getIncludedFiles(); + if (tmp.length > 0) { + files.put(f, tmp); + } } - try { - FileUtil.writeString(licenseFile, licenseFileContent, encoding); - if (copyToResource) { - File target = new File(project.getBasedir(), "src" + File.separator + "main" + File.separator + "resources" + File.separator + licenseFile.getName()); - if (target.exists() && keepBackup) { - if (verbose) { - getLog().info("backup " + target); - } - target.renameTo(new File(target.getAbsolutePath() + '~')); - } - FileUtil.copy(licenseFile, target); + + return files; + } + + protected String computeHeader(LicenseType type) throws IOException { + String tmpHeader = FileUtil.readAsString(type.getLicenceHeaderReader(encoding)); + + // defined inceptionYear (if year is older than now suffix with a - thisYear) + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + String thisYear = cal.get(Calendar.YEAR) + ""; + if (!thisYear.equals(inceptionYear)) { + inceptionYear = inceptionYear + " - " + thisYear; + } + + // format header with projet informations + tmpHeader = String.format(tmpHeader, projectName, inceptionYear, organizationName); + + // add " * " before each line + BufferedReader reader = new BufferedReader(new java.io.StringReader(tmpHeader)); + StringBuilder sb = new StringBuilder(); + + String line = reader.readLine(); + sb.append(line).append('\n'); + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.isEmpty()) { + sb.append(" *\n"); + } else { + sb.append(" * ").append(line).append("\n"); } - } catch (IOException e) { - throw new MojoExecutionException("could not write license file " + licenseFile + " for reason : " + e.getMessage(), e); } + tmpHeader = sb.toString(); + return tmpHeader.substring(0, tmpHeader.length() - 1); } /** @@ -208,16 +236,19 @@ * @throws java.io.IOException if IO pb */ protected void processJavaSource(LicenseProcessor p, File sourceFile) throws Exception { + if (verbose) { - getLog().info("treate file " + sourceFile); + getLog().info("process file " + sourceFile); } + // file where to write result File processFile = new File(sourceFile.getAbsolutePath() + "_" + timestamp); try { p.process(sourceFile, processFile); if (!p.getLicenceFilter().wasTouched()) { + // no license header found in file, add it addLicenseToJavaSourceFile(sourceFile, processFile); } @@ -242,62 +273,8 @@ protected void addLicenseToJavaSourceFile(File sourceFile, File processFile) throws IOException { getLog().warn("no license was found on file " + sourceFile + ", adding one"); String content = FileUtil.readAsString(sourceFile, encoding); - content = "/**\n * " + LicenseFilter.HEADER + " " + licenseHeaderContent + " " + LicenseFilter.HEADER + "\n */\n" + content; + content = "/**\n * " + LicenseFilter.HEADER + " " + licenseHeaderContent + " " + LicenseFilter.FOOTER + "\n */\n" + content; FileUtil.writeString(processFile, content, encoding); } - protected void init() throws IOException { - - timestamp = System.nanoTime(); - - LicenseType type = LicenseType.valueOf(licenseType); - - // obtain content of license file - licenseFileContent = FileUtil.readAsString(type.getLicenceFileReader(encoding)); - - // obtain content of license header - String tmpHeader = FileUtil.readAsString(type.getLicenceHeaderReader(encoding)); - - // defined inceptionYear (if year is older than now suffix with a - thisYear) - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date()); - String thisYear = cal.get(Calendar.YEAR) + ""; - if (!thisYear.equals(inceptionYear)) { - inceptionYear = inceptionYear + " - " + thisYear; - } - - // format header with projet informations - tmpHeader = String.format(tmpHeader, projectName, inceptionYear, organizationName); - - // add " * " before each line - BufferedReader reader = new BufferedReader(new java.io.StringReader(tmpHeader)); - StringBuilder sb = new StringBuilder(); - - String line = reader.readLine(); - sb.append(line).append('\n'); - while ((line = reader.readLine()) != null) { - if (line.isEmpty()) { - sb.append(" *\n"); - } else { - sb.append(" * ").append(line).append("\n"); - } - } - tmpHeader = sb.toString(); - licenseHeaderContent = tmpHeader.substring(0, tmpHeader.length() - 1); - - if (verbose) { - getLog().info("header to write on java source files \n" + licenseHeaderContent); - } - - // init directory scanner - DirectoryScanner ds = new DirectoryScanner(); - ds.setBasedir(src); - ds.setIncludes(new String[]{"**\\/*.java"}); - - // scan - ds.scan(); - - // get files - javaFilesToTreate = ds.getIncludedFiles(); - } } Modified: trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/ThirdPartyMojo.java =================================================================== --- trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/ThirdPartyMojo.java 2008-08-18 19:14:39 UTC (rev 1049) +++ trunk/maven-license-switcher-plugin/src/main/java/org/codelutin/license/ThirdPartyMojo.java 2008-08-18 22:16:08 UTC (rev 1050) @@ -3,8 +3,8 @@ * 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 + * 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, @@ -12,7 +12,7 @@ * 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 + * 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>. ##%* */ @@ -26,7 +26,6 @@ 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.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; @@ -44,54 +43,20 @@ import java.util.TreeSet; /** - * Le goal pour ecrire le fichier THIRD-PARTY.txt contenant les licenses de toutes les dependances du projet. + * 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 AbstractMojo { +public class ThirdPartyMojo extends AbstractLicenceMojo { private static final String unknownLicenseMessage = "Unknown license"; /** - * Dependance du projet. - * - * @parameter default-value="${project}" - * @readonly - */ - protected MavenProject project; - - /** - * encoding a utiliser pour lire et ecrire les fichiers. - * - * @parameter expression="${licence-switcher.encoding}" default-value="${maven.compile.encoding}" - * @required - */ - protected String encoding; - - /** - * un flag pour conserver un backup des fichiers modifies - * - * @parameter expression="${license-switcher.keepBackup}" default-value="false" - */ - protected boolean keepBackup; - - /** - * un flag pour indiquer la recopie vers le repertoire des resources. - * - * @parameter expression="${license-switcher.copyToResource}" default-value="true" - */ - protected boolean copyToResource; - - /** - * un flag pour avoir les traces - * - * @parameter expression="${license-switcher.verbose}" default-value="false" - */ - protected boolean verbose; - - /** * Local Repository. * * @parameter expression="${localRepository}" @@ -107,7 +72,6 @@ * @parameter expression="${project.remoteArtifactRepositories}" * @required * @readonly - * @since 2.1 */ protected List remoteRepositories; @@ -116,7 +80,6 @@ * * @parameter expression="${license-switcher.thirdPartyFile}" default-value="${basedir}/THIRD-PARTY.txt" * @required - * @readonly */ protected File thirdPartyFile; @@ -155,46 +118,67 @@ */ protected MavenProjectBuilder mavenProjectBuilder; - public void execute() throws MojoExecutionException, MojoFailureException { + /** + * Un flag pour indiquer la regeneration des fichiers. + * + * @parameter expression="${license-switcher.doGenerate}" default-value="false" + */ + 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; - DependencyNode dependencyTreeNode = resolveProject(); + protected void init() throws Exception { - LicenseMap licenseMap = new LicenseMap(); + // must generate if file does not exist + doGenerate = doGenerate || !thirdPartyFile.exists(); - for (Object o : dependencyTreeNode.getChildren()) { + if (doGenerate) { - buildLicenseMap((DependencyNode) o, licenseMap); + // prepare thirdPartyFileContent + + DependencyNode dependencyTreeNode = resolveProject(); + + LicenseMap licenseMap = new LicenseMap(); + + for (Object o : dependencyTreeNode.getChildren()) { + + buildLicenseMap((DependencyNode) o, licenseMap); + } + + thirdPartyFileContent = buildGroupedLicenses(licenseMap); } + } - String content = buildGroupedLicenses(licenseMap); - try { + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + + super.execute(); + + if (doGenerate) { if (verbose) { getLog().info("writing third-party file : " + thirdPartyFile); } - if (thirdPartyFile.exists() && keepBackup) { + if (keepBackup && thirdPartyFile.exists()) { if (verbose) { getLog().info("backup " + thirdPartyFile); } thirdPartyFile.renameTo(new File(thirdPartyFile.getAbsolutePath() + '~')); } - FileUtil.writeString(thirdPartyFile, content, encoding); - if (copyToResource) { - File target = new File(project.getBasedir(), "src" + File.separator + "main" + File.separator + "resources" + File.separator + thirdPartyFile.getName()); - if (target.exists() && keepBackup) { - if (verbose) { - getLog().info("backup " + target); - } - target.renameTo(new File(target.getAbsolutePath() + '~')); - } - FileUtil.copy(thirdPartyFile, target); + try { + FileUtil.writeString(thirdPartyFile, thirdPartyFileContent, encoding); + } catch (IOException e) { + throw new MojoExecutionException("could not write file " + thirdPartyFile + " for reason : " + e.getMessage(), e); } - } catch (IOException e) { - throw new MojoExecutionException("could not write file " + thirdPartyFile + " for reason : " + e.getMessage(), e); - } + // copy third party file to classpath + copyFileToOutputDirectory(thirdPartyFile); } + /** @return resolve the dependency tree */ protected DependencyNode resolveProject() { try { @@ -220,15 +204,14 @@ List licenses = artifactProject.getLicenses(); if (licenses.isEmpty()) { + // no license found for the dependency + getLog().warn("no license found for dependency " + artifactName); licenseMap.put(unknownLicenseMessage, artifactName); } else { for (Object license : licenses) { - License element = (License) license; - String licenseName = element.getName(); - - licenseMap.put(licenseName, artifactName); + licenseMap.put(((License) license).getName(), artifactName); } } } @@ -285,17 +268,17 @@ */ protected MavenProject getMavenProjectFromRepository(Artifact artifact) throws ProjectBuildingException { - Artifact projectArtifact = artifact; boolean allowStubModel = false; + if (!"pom".equals(artifact.getType())) { - projectArtifact = factory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), + artifact = factory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope()); allowStubModel = true; } // TODO: we should use the MavenMetadataSource instead - return mavenProjectBuilder.buildFromRepository(projectArtifact, remoteRepositories, localRepository, + return mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories, localRepository, allowStubModel); } @@ -309,9 +292,11 @@ if (valueList == null) { valueList = new TreeSet<String>(); } - getLog().debug("key:" + key + ",value: " + value); + if (getLog().isDebugEnabled()) { + getLog().debug("key:" + key + ",value: " + value); + } valueList.add(value); - return super.put(key, valueList); + return put(key, valueList); } } } Modified: trunk/maven-license-switcher-plugin/src/site/apt/index.apt =================================================================== --- trunk/maven-license-switcher-plugin/src/site/apt/index.apt 2008-08-18 19:14:39 UTC (rev 1049) +++ trunk/maven-license-switcher-plugin/src/site/apt/index.apt 2008-08-18 22:16:08 UTC (rev 1050) @@ -14,13 +14,15 @@ The plugin has the following goals: - * {{{switch-mojo.html}llicense-switcher:switch}} parse Java sources and switch license header. + * {{{license-mojo.html}license-switcher:license}} add license file and third-party files to classpath (generate them if necessary). * {{{third-party-mojo.html}license-switcher:third-party}} write the licenses of all third-party libraries used in project. + + * {{{switch-mojo.html}license-switcher:switch}} parse Java sources and switch license header. * {{{help-mojo.html}license-switcher:help}} display help about the plugin (goals, usage). - * {{{help-mojo.html}license-switcher:available-licenses}} display the known licenses of the plugin. + * {{{available-licenses-mojo.html}license-switcher:available-licenses}} display the known licenses of the plugin. * Usage Modified: trunk/maven-license-switcher-plugin/src/site/apt/usage.apt =================================================================== --- trunk/maven-license-switcher-plugin/src/site/apt/usage.apt 2008-08-18 19:14:39 UTC (rev 1049) +++ trunk/maven-license-switcher-plugin/src/site/apt/usage.apt 2008-08-18 22:16:08 UTC (rev 1050) @@ -10,13 +10,34 @@ Below are the different goals and configurations of the plugin. +* The <<<license>>> Mojo + + The <<<license>>> mojo is used to copy LICENSE.txt file on classpath and generate it on the + basedir of the project if necessary. + + By default, the license to apply is the lgpl v 3. To change it pass the maven parameter + <<<-Dlicense-switcher.licenseType=ANOTHER LICENCE TYPE>>> + ++-----+ +mvn license-switcher:license ++-----+ + +* The <<<third-party>>> Mojo + + The <<<third-party>>> copy the <<<THIRD-PARTY.txt>>> file (the licenses of all third party librairies + used by the project) to the classpath and generate it on the basedir if necessary. + ++-----+ +mvn license-switcher:third-party ++-----+ + * The <<<switch>>> Mojo - The <<<switch>>> mojo is used to switch licence header on each java source files and add a LICENSE.txt file on the - basedir of the project. + The <<<switch>>> mojo is used to switch licence header on each java source files and add a <<<LICENSE.txt>>> file + on the basedir of the project. - By default, the license to apply is the lgpl v 3. To change it pass the maven parameter <<<-Dlicense-switcher.licenseType=ANOTHER LICENCE TYPE>>> - + By default, the license to apply is the lgpl v 3. To change it pass the maven parameter + <<<-Dlicense-switcher.licenseType=ANOTHER LICENCE TYPE>>> +-----+ mvn license-switcher:switch @@ -30,14 +51,6 @@ mvn license-switcher:available-licenses +-----+ -* The <<<third-party>>> Mojo - - The <<<third-party>>> write in the <<<THIRD-PARTY.txt>>> the licenses of all third party librairies used by the project. - -+-----+ -mvn license-switcher:third-party -+-----+ - * The <<<help>>> Mojo The <<<help>>> mojo display in the console the help for the plugin. @@ -45,4 +58,3 @@ +-----+ mvn license-switcher:help +-----+ - Modified: trunk/maven-license-switcher-plugin/src/test/java/org/codelutin/license/LicenseTypeTest.java =================================================================== --- trunk/maven-license-switcher-plugin/src/test/java/org/codelutin/license/LicenseTypeTest.java 2008-08-18 19:14:39 UTC (rev 1049) +++ trunk/maven-license-switcher-plugin/src/test/java/org/codelutin/license/LicenseTypeTest.java 2008-08-18 22:16:08 UTC (rev 1050) @@ -1,20 +1,20 @@ /** * *##% 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 + * 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 + * + * 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>. *##% + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ package org.codelutin.license;