Nuiton-utils-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
October 2008
- 2 participants
- 84 discussions
[Lutinutil-commits] r1179 - maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 21:45:37 +0000 (Sun, 12 Oct 2008)
New Revision: 1179
Modified:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java
Log:
remove other than default constructor
Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 21:45:18 UTC (rev 1178)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 21:45:37 UTC (rev 1179)
@@ -110,7 +110,11 @@
return null;
}
- return new License(licenseName, licenseURL, headerURL);
+ License license = new License();
+ license.setName(licenseName);
+ license.setLicenseURL(licenseURL);
+ license.setHeaderURL(headerURL);
+ return license;
} catch (MalformedURLException e) {
1
0
[Lutinutil-commits] r1178 - maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 21:45:18 +0000 (Sun, 12 Oct 2008)
New Revision: 1178
Modified:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java
Log:
enable to have multi extra resolvers
Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java 2008-10-12 21:44:54 UTC (rev 1177)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java 2008-10-12 21:45:18 UTC (rev 1178)
@@ -1,31 +1,42 @@
package org.codelutin.license;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* Class responsible of instanciate {@link License}.
+ * <p/>
+ * The factory use a table of {@link #resolvers} to lookup for licenses.
+ * <p/>
+ * To obtain an instance of a {@link org.codelutin.license.LicenseFactory} use the method {@link #newInstance(String[])}.
*
* @author chemit
*/
public class LicenseFactory {
- protected LicenseResolver[] resolvers;
+ protected List<LicenseResolver> resolvers;
- public static LicenseFactory newInstance(String extraResolver) throws IllegalArgumentException {
- if (extraResolver == null || extraResolver.trim().isEmpty()) {
- return new LicenseFactory(new LicenseResolver[]{
- new JarLicenseResolver(),
- });
+ public static LicenseFactory newInstance(String... extraResolvers) throws IllegalArgumentException {
+ LicenseFactory result = new LicenseFactory();
+ // always put jar resolver first
+ result.addResolver(new JarLicenseResolver());
+
+ for (String extraResolver : extraResolvers) {
+ if (extraResolver != null && !extraResolver.trim().isEmpty()) {
+ // add a extra resolver
+ result.addResolver(new LicenseResolver(extraResolver));
+ }
}
- return new LicenseFactory(new LicenseResolver[]{
- new JarLicenseResolver(),
- new LicenseResolver(extraResolver)
- });
+ return result;
}
+ public void addResolver(LicenseResolver licenseResolver) {
+ resolvers.add(licenseResolver);
+ }
+
public Map<String, String> getLicenseNames() throws IllegalArgumentException {
Map<String, String> result = new TreeMap<String, String>();
for (LicenseResolver resolver : resolvers) {
@@ -50,14 +61,11 @@
return result;
}
}
- throw new IllegalArgumentException("could not find the license " + licenseName + " with resolvers" + Arrays.toString(resolvers));
+ throw new IllegalArgumentException("could not find the license " + licenseName + " with resolvers" + resolvers);
}
- protected LicenseFactory(LicenseResolver[] resolvers) throws IllegalArgumentException {
- if (resolvers == null || resolvers.length == 0) {
- throw new IllegalArgumentException("no resolver defined");
- }
- this.resolvers = resolvers;
+ protected LicenseFactory() throws IllegalArgumentException {
+ this.resolvers = new ArrayList<LicenseResolver>();
}
}
1
0
[Lutinutil-commits] r1177 - maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 21:44:54 +0000 (Sun, 12 Oct 2008)
New Revision: 1177
Modified:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/License.java
Log:
remove other than default constructor
Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/License.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/License.java 2008-10-12 21:44:25 UTC (rev 1176)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/License.java 2008-10-12 21:44:54 UTC (rev 1177)
@@ -28,16 +28,6 @@
public License() {
}
- public License(String name, URL licenseURL, URL headerURL) {
- this(licenseURL,headerURL);
- this.name=name;
- }
-
- public License(URL licenseURL, URL headerURL) {
- this.licenseURL=licenseURL;
- this.headerURL=headerURL;
- }
-
public String getName() {
return name;
}
1
0
[Lutinutil-commits] r1176 - in maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license: . plugin
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 21:44:25 +0000 (Sun, 12 Oct 2008)
New Revision: 1176
Added:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/AbstractLicenseMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/AvailableLicensesMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/LicenseMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/SwitchLicenseMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java
Removed:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AvailableLicensesMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/ThirdPartyMojo.java
Log:
introduce plugin package to put mojo
Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java 2008-10-12 21:44:03 UTC (rev 1175)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -1,108 +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;
-
-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 AbstractLicenseMojo 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="${maven.verbose}"
- */
- 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;
- }
- //TODO should be able to use path
- 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);
- }
- }
-}
Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AvailableLicensesMojo.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AvailableLicensesMojo.java 2008-10-12 21:44:03 UTC (rev 1175)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AvailableLicensesMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -1,55 +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;
-
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-
-import java.util.Map.Entry;
-
-/**
- * Le goal pour afficher dans la console les differentes licenses connues.
- *
- * @author chemit
- * @goal available-licenses
- * @requiresProject false
- */
-public class AvailableLicensesMojo extends AbstractMojo {
-
- /**
- * La baseURL d'un resolver de license supplementaire
- *
- * @parameter expression="${license-switcher.extraResolver}"
- */
- protected String extraResolver;
-
- public void execute() throws MojoExecutionException, MojoFailureException {
- StringBuilder sb = new StringBuilder();
- sb.append("Available licenses :\n");
-
- LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
-
- for (Entry<String, String> license : factory.getLicenseNames().entrySet()) {
- sb.append(" * ").append(license.getKey()).append(" : ").append(license.getValue()).append('\n');
- }
- System.out.println(sb.toString());
- }
-
-}
Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java 2008-10-12 21:44:03 UTC (rev 1175)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -1,118 +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;
-
-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 AbstractLicenseMojo {
- /**
- * 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.licenseName}"
- * @required
- */
- protected String licenseName;
-
- /**
- * La baseURL d'un resolver de license supplementaire
- *
- * @parameter expression="${license-switcher.extraResolver}"
- */
- protected String extraResolver;
-
- protected License license;
-
- protected void init() throws Exception {
-
- // must generate if file does not exist
- doGenerate = doGenerate || !licenseFile.exists();
-
- if (doGenerate) {
-
- // acquire license
-
- LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
-
- license = factory.revolv(licenseName);
-
- }
- }
-
- @Override
- public void execute() throws MojoExecutionException, MojoFailureException {
-
- super.execute();
-
- if (doGenerate) {
-
- getLog().info("using licence ["+licenseName+"]");
-
- if (verbose) {
- getLog().info("licence : "+license);
- }
-
- 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, license.getLicenseContent(encoding), 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);
- }
-
-}
Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-10-12 21:44:03 UTC (rev 1175)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -1,282 +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;
-
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-import org.codehaus.plexus.util.DirectoryScanner;
-import org.codelutin.processor.LicenseProcessor;
-import org.codelutin.processor.filters.LicenseFilter;
-import org.codelutin.util.FileUtil;
-
-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.
- *
- * @author chemit
- * @goal switch
- * @phase process-resources
- * @requiresProject true
- */
-public class SwitchLicenseMojo extends AbstractLicenseMojo {
-
- /**
- * l'annee de creation du module (sera place dans le header)
- *
- * @parameter default-value="${project.inceptionYear}"
- * @required
- */
- protected String inceptionYear;
-
- /**
- * le nom de l'organisation (sera place dans le header)
- *
- * @parameter default-value="${project.organization.name}"
- * @required
- * @readonly
- */
- protected String organizationName;
-
- /**
- * le nom du projet (sera place dans le header)
- *
- * @parameter default-value="${project.name}"
- * @required
- * @readonly
- */
- protected String projectName;
-
- /**
- * Le type de license a appliquer.
- *
- * @parameter expression="${license-switcher.licenseName}"
- * @required
- */
- protected String licenseName;
-
- /**
- * Repertoires des fichiers sources a traiter.
- *
- * @parameter expression="${license-switcher.compileSourceRoots}" default-value="${project.compileSourceRoots}"
- * @required
- */
- protected List<String> compileSourceRoots;
-
- /**
- * Repertoires des fichiers sources de test a traiter.
- *
- * @parameter expression="${license-switcher.testCompileSourceRoots}" default-value="${project.testCompileSourceRoots}"
- * @required
- */
- protected List<String> testCompileSourceRoots;
-
- /**
- * Un resolver externe
- *
- * @parameter expression="${license-switcher.extraResolver}"
- */
- protected String extraResolver;
-
- /** le header a ajouter dans chaque fichier source java */
- protected String licenseHeaderContent;
-
- /** la liste des chemin relatifs des sources java a traiter pour chaque repertoire contenant des sources */
- protected Map<File, String[]> javaFilesToTreate;
-
- protected long timestamp;
-
- protected void init() throws IOException {
-
- timestamp = System.nanoTime();
-
- // obtain all java source files to be treated
- javaFilesToTreate = getFilesToTreate();
-
- if (javaFilesToTreate.isEmpty()) {
- // nothing to do, since no file to treate was found
- return;
- }
-
- LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
-
- License license = factory.revolv(licenseName);
-
- // obtain content of license header
- licenseHeaderContent = computeHeader(license);
-
- if (verbose) {
- getLog().info("header to write on java source files \n" + licenseHeaderContent);
- }
-
- }
-
- @Override
- public void execute() throws MojoExecutionException, MojoFailureException {
-
- super.execute();
-
- 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 (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 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) {
- File f = new File(src);
- if (!f.exists()) {
- // do nothing
- continue;
- }
- if (verbose) {
- getLog().info("discovering java source files in root " + src);
- }
- ds.setBasedir(f);
- // scan
- ds.scan();
-
- // get files
- String[] tmp = ds.getIncludedFiles();
- if (tmp.length > 0) {
- files.put(f, tmp);
- }
- }
-
- return files;
- }
-
- protected String computeHeader(License license) throws IOException {
-
-
- String tmpHeader = license.getHeaderContent(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");
- }
- }
- tmpHeader = sb.toString();
- return tmpHeader.substring(0, tmpHeader.length() - 1);
- }
-
- /**
- * @param p license processor
- * @param sourceFile the java source file where to switch (or add the licence)
- * @throws java.io.IOException if IO pb
- */
- protected void processJavaSource(LicenseProcessor p, File sourceFile) throws Exception {
-
- if (verbose) {
- 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);
- }
-
- if (keepBackup) {
- File backupFile = new File(sourceFile.getAbsolutePath() + "~");
- if (verbose) {
- getLog().debug("backup original file " + sourceFile);
- }
- sourceFile.renameTo(backupFile);
- }
- processFile.renameTo(sourceFile);
-
- } catch (Exception e) {
- getLog().error("could not process file " + sourceFile + " for reason " + e.getMessage(), e);
- processFile.delete();
- throw e;
- } finally {
- p.getLicenceFilter().reset();
- }
- }
-
- 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.FOOTER + "\n */\n" + content;
- FileUtil.writeString(processFile, content, encoding);
- }
-
-}
Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/ThirdPartyMojo.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/ThirdPartyMojo.java 2008-10-12 21:44:03 UTC (rev 1175)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/ThirdPartyMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -1,300 +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;
-
-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 (doGenerate) {
-
- // prepare thirdPartyFileContent
-
- DependencyNode dependencyTreeNode = resolveProject();
-
- LicenseMap licenseMap = new LicenseMap();
-
- for (Object o : dependencyTreeNode.getChildren()) {
-
- buildLicenseMap((DependencyNode) o, licenseMap);
- }
-
- thirdPartyFileContent = buildGroupedLicenses(licenseMap);
- }
- }
-
- @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) {
-
- Artifact artifact = node.getArtifact();
-
- 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
- getLog().warn("no license found for dependency " + artifactName);
-
- 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/trunk/src/main/java/org/codelutin/license/plugin/AbstractLicenseMojo.java (from rev 1165, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java)
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/AbstractLicenseMojo.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/AbstractLicenseMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -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.plugin;
+
+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 AbstractLicenseMojo 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="${maven.verbose}"
+ */
+ 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;
+ }
+ //TODO should be able to use path
+ 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);
+ }
+ }
+}
Copied: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/AvailableLicensesMojo.java (from rev 1169, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AvailableLicensesMojo.java)
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/AvailableLicensesMojo.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/AvailableLicensesMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -0,0 +1,56 @@
+/**
+ * *##% 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.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.codelutin.license.LicenseFactory;
+
+import java.util.Map.Entry;
+
+/**
+ * Le goal pour afficher dans la console les differentes licenses connues.
+ *
+ * @author chemit
+ * @goal available-licenses
+ * @requiresProject false
+ */
+public class AvailableLicensesMojo extends AbstractMojo {
+
+ /**
+ * La baseURL d'un resolver de license supplementaire
+ *
+ * @parameter expression="${license-switcher.extraResolver}"
+ */
+ protected String extraResolver;
+
+ public void execute() throws MojoExecutionException, MojoFailureException {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Available licenses :\n");
+
+ LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
+
+ for (Entry<String, String> license : factory.getLicenseNames().entrySet()) {
+ sb.append(" * ").append(license.getKey()).append(" : ").append(license.getValue()).append('\n');
+ }
+ System.out.println(sb.toString());
+ }
+
+}
Copied: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/LicenseMojo.java (from rev 1171, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java)
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/LicenseMojo.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/LicenseMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -0,0 +1,120 @@
+/**
+ * *##% 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.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.codelutin.util.FileUtil;
+import org.codelutin.license.License;
+import org.codelutin.license.LicenseFactory;
+
+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 AbstractLicenseMojo {
+ /**
+ * 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.licenseName}"
+ * @required
+ */
+ protected String licenseName;
+
+ /**
+ * La baseURL d'un resolver de license supplementaire
+ *
+ * @parameter expression="${license-switcher.extraResolver}"
+ */
+ protected String[] extraResolver;
+
+ protected License license;
+
+ protected void init() throws Exception {
+
+ // must generate if file does not exist
+ doGenerate = doGenerate || !licenseFile.exists();
+
+ if (doGenerate) {
+
+ // acquire license
+
+ LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
+
+ license = factory.revolv(licenseName);
+
+ }
+ }
+
+ @Override
+ public void execute() throws MojoExecutionException, MojoFailureException {
+
+ super.execute();
+
+ if (doGenerate) {
+
+ getLog().info("using licence ["+licenseName+"]");
+
+ if (verbose) {
+ getLog().info("licence : "+license);
+ }
+
+ 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, license.getLicenseContent(encoding), 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);
+ }
+
+}
Copied: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/SwitchLicenseMojo.java (from rev 1171, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java)
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/SwitchLicenseMojo.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/SwitchLicenseMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -0,0 +1,284 @@
+/**
+ * *##% 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.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.codehaus.plexus.util.DirectoryScanner;
+import org.codelutin.processor.LicenseProcessor;
+import org.codelutin.processor.filters.LicenseFilter;
+import org.codelutin.util.FileUtil;
+import org.codelutin.license.LicenseFactory;
+import org.codelutin.license.License;
+
+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.
+ *
+ * @author chemit
+ * @goal switch
+ * @phase process-resources
+ * @requiresProject true
+ */
+public class SwitchLicenseMojo extends AbstractLicenseMojo {
+
+ /**
+ * l'annee de creation du module (sera place dans le header)
+ *
+ * @parameter default-value="${project.inceptionYear}"
+ * @required
+ */
+ protected String inceptionYear;
+
+ /**
+ * le nom de l'organisation (sera place dans le header)
+ *
+ * @parameter default-value="${project.organization.name}"
+ * @required
+ * @readonly
+ */
+ protected String organizationName;
+
+ /**
+ * le nom du projet (sera place dans le header)
+ *
+ * @parameter default-value="${project.name}"
+ * @required
+ * @readonly
+ */
+ protected String projectName;
+
+ /**
+ * Le type de license a appliquer.
+ *
+ * @parameter expression="${license-switcher.licenseName}"
+ * @required
+ */
+ protected String licenseName;
+
+ /**
+ * Repertoires des fichiers sources a traiter.
+ *
+ * @parameter expression="${license-switcher.compileSourceRoots}" default-value="${project.compileSourceRoots}"
+ * @required
+ */
+ protected List<String> compileSourceRoots;
+
+ /**
+ * Repertoires des fichiers sources de test a traiter.
+ *
+ * @parameter expression="${license-switcher.testCompileSourceRoots}" default-value="${project.testCompileSourceRoots}"
+ * @required
+ */
+ protected List<String> testCompileSourceRoots;
+
+ /**
+ * Un resolver externe
+ *
+ * @parameter expression="${license-switcher.extraResolver}"
+ */
+ protected String[] extraResolver;
+
+ /** le header a ajouter dans chaque fichier source java */
+ protected String licenseHeaderContent;
+
+ /** la liste des chemin relatifs des sources java a traiter pour chaque repertoire contenant des sources */
+ protected Map<File, String[]> javaFilesToTreate;
+
+ protected long timestamp;
+
+ protected void init() throws IOException {
+
+ timestamp = System.nanoTime();
+
+ // obtain all java source files to be treated
+ javaFilesToTreate = getFilesToTreate();
+
+ if (javaFilesToTreate.isEmpty()) {
+ // nothing to do, since no file to treate was found
+ return;
+ }
+
+ LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
+
+ License license = factory.revolv(licenseName);
+
+ // obtain content of license header
+ licenseHeaderContent = computeHeader(license);
+
+ if (verbose) {
+ getLog().info("header to write on java source files \n" + licenseHeaderContent);
+ }
+
+ }
+
+ @Override
+ public void execute() throws MojoExecutionException, MojoFailureException {
+
+ super.execute();
+
+ 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 (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 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) {
+ File f = new File(src);
+ if (!f.exists()) {
+ // do nothing
+ continue;
+ }
+ if (verbose) {
+ getLog().info("discovering java source files in root " + src);
+ }
+ ds.setBasedir(f);
+ // scan
+ ds.scan();
+
+ // get files
+ String[] tmp = ds.getIncludedFiles();
+ if (tmp.length > 0) {
+ files.put(f, tmp);
+ }
+ }
+
+ return files;
+ }
+
+ protected String computeHeader(License license) throws IOException {
+
+
+ String tmpHeader = license.getHeaderContent(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");
+ }
+ }
+ tmpHeader = sb.toString();
+ return tmpHeader.substring(0, tmpHeader.length() - 1);
+ }
+
+ /**
+ * @param p license processor
+ * @param sourceFile the java source file where to switch (or add the licence)
+ * @throws java.io.IOException if IO pb
+ */
+ protected void processJavaSource(LicenseProcessor p, File sourceFile) throws Exception {
+
+ if (verbose) {
+ 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);
+ }
+
+ if (keepBackup) {
+ File backupFile = new File(sourceFile.getAbsolutePath() + "~");
+ if (verbose) {
+ getLog().debug("backup original file " + sourceFile);
+ }
+ sourceFile.renameTo(backupFile);
+ }
+ processFile.renameTo(sourceFile);
+
+ } catch (Exception e) {
+ getLog().error("could not process file " + sourceFile + " for reason " + e.getMessage(), e);
+ processFile.delete();
+ throw e;
+ } finally {
+ p.getLicenceFilter().reset();
+ }
+ }
+
+ 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.FOOTER + "\n */\n" + content;
+ FileUtil.writeString(processFile, content, encoding);
+ }
+
+}
Copied: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java (from rev 1165, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/ThirdPartyMojo.java)
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/ThirdPartyMojo.java 2008-10-12 21:44:25 UTC (rev 1176)
@@ -0,0 +1,300 @@
+/**
+ * *##% 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 (doGenerate) {
+
+ // prepare thirdPartyFileContent
+
+ DependencyNode dependencyTreeNode = resolveProject();
+
+ LicenseMap licenseMap = new LicenseMap();
+
+ for (Object o : dependencyTreeNode.getChildren()) {
+
+ buildLicenseMap((DependencyNode) o, licenseMap);
+ }
+
+ thirdPartyFileContent = buildGroupedLicenses(licenseMap);
+ }
+ }
+
+ @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) {
+
+ Artifact artifact = node.getArtifact();
+
+ 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
+ getLog().warn("no license found for dependency " + artifactName);
+
+ 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);
+ }
+ }
+}
1
0
[Lutinutil-commits] r1175 - maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 21:44:03 +0000 (Sun, 12 Oct 2008)
New Revision: 1175
Modified:
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java
Log:
use base test class
Modified: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java 2008-10-12 21:43:43 UTC (rev 1174)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java 2008-10-12 21:44:03 UTC (rev 1175)
@@ -1,17 +1,12 @@
package org.codelutin.license;
-import junit.framework.TestCase;
-
-import java.io.IOException;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
-import java.util.ArrayList;
/** @author chemit */
-public class JarLicenseResolverTest extends TestCase {
+public class JarLicenseResolverTest extends BaseLicenseTestCase {
- protected String encoding = "utf-8";
-
public void testResolv() throws Exception {
JarLicenseResolver resolver = new JarLicenseResolver();
@@ -56,7 +51,7 @@
public void testGetLicenseNames() throws Exception {
JarLicenseResolver resolver = new JarLicenseResolver();
- Map<String,String> licenseNames = resolver.getLicenseNames();
+ Map<String, String> licenseNames = resolver.getLicenseNames();
assertEquals(Arrays.asList("gpl_v1",
"gpl_v2",
@@ -65,12 +60,5 @@
"lgpl_v3"), new ArrayList<String>(licenseNames.keySet()));
}
- protected void assertLicenseFound(String name, License license) throws IOException {
- System.out.println(license);
- assertNotNull(license);
- assertEquals(name, license.getName());
- assertNotNull(license.getLicenseContent(encoding));
- assertNotNull(license.getHeaderContent(encoding));
- }
}
Modified: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java 2008-10-12 21:43:43 UTC (rev 1174)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java 2008-10-12 21:44:03 UTC (rev 1175)
@@ -1,43 +1,77 @@
package org.codelutin.license;
-import junit.framework.TestCase;
-
+import java.io.File;
import java.util.Map;
/** @author chemit */
-public class LicenseFactoryTest extends TestCase {
+public class LicenseFactoryTest extends BaseLicenseTestCase {
public void testNewInstance() throws Exception {
- LicenseFactory factory = LicenseFactory.newInstance(null);
+ LicenseFactory factory = LicenseFactory.newInstance();
assertNotNull(factory);
- assertEquals(1, factory.resolvers.length);
- assertNotNull(factory.resolvers[0]);
- assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass());
+ assertEquals(1, factory.resolvers.size());
+ assertNotNull(factory.resolvers.get(0));
+ assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass());
factory = LicenseFactory.newInstance("");
assertNotNull(factory);
- assertEquals(1, factory.resolvers.length);
- assertNotNull(factory.resolvers[0]);
- assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass());
+ assertEquals(1, factory.resolvers.size());
+ assertNotNull(factory.resolvers.get(0));
+ assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass());
factory = LicenseFactory.newInstance("yo");
assertNotNull(factory);
- assertEquals(2, factory.resolvers.length);
- assertNotNull(factory.resolvers[0]);
- assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass());
- assertNotNull(factory.resolvers[1]);
- assertEquals(LicenseResolver.class, factory.resolvers[1].getClass());
- assertEquals("yo", factory.resolvers[1].getBaseURL());
+
+ assertEquals(2, factory.resolvers.size());
+ assertNotNull(factory.resolvers.get(0));
+ assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass());
+ assertNotNull(factory.resolvers.get(1));
+ assertEquals(LicenseResolver.class, factory.resolvers.get(1).getClass());
+ assertEquals("yo", factory.resolvers.get(1).getBaseURL());
+
+ factory.addResolver(new LicenseResolver("yo2"));
+ assertEquals(3, factory.resolvers.size());
+ assertNotNull(factory.resolvers.get(0));
+ assertEquals(JarLicenseResolver.class, factory.resolvers.get(0).getClass());
+ assertNotNull(factory.resolvers.get(1));
+ assertEquals(LicenseResolver.class, factory.resolvers.get(1).getClass());
+ assertEquals("yo", factory.resolvers.get(1).getBaseURL());
+ assertEquals(LicenseResolver.class, factory.resolvers.get(2).getClass());
+ assertEquals("yo2", factory.resolvers.get(2).getBaseURL());
+
}
public void testGetLicenseNames() throws Exception {
- LicenseFactory factory = LicenseFactory.newInstance(null);
+ LicenseFactory factory = LicenseFactory.newInstance();
+ int expectedJarResolverLicenses = 5;
Map<String, String> licenseNames = factory.getLicenseNames();
//todo make dynamic that test
- assertEquals(5, licenseNames.size());
+ assertEquals(expectedJarResolverLicenses, licenseNames.size());
+ // create a new dummy license repository with a license
+ File repo = createLicenseRepository(true);
+
+ LicenseResolver resolver = new LicenseResolver();
+ resolver.setBaseURL(repo.toURI().toURL().toString());
+
+ factory.addResolver(resolver);
+
+ licenseNames = factory.getLicenseNames();
+ assertEquals(expectedJarResolverLicenses + 1, licenseNames.size());
+
+ // add a new license to repo
+ long timestamp = System.currentTimeMillis();
+ String licenseName = "dummy2_" + timestamp;
+ addLicenseToRepository(repo,licenseName);
+
+ factory = LicenseFactory.newInstance(repo.toURI().toURL().toString());
+
+ licenseNames = factory.getLicenseNames();
+ assertEquals(expectedJarResolverLicenses + 2, licenseNames.size());
+
}
+
}
Modified: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java 2008-10-12 21:43:43 UTC (rev 1174)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java 2008-10-12 21:44:03 UTC (rev 1175)
@@ -1,16 +1,11 @@
package org.codelutin.license;
-import junit.framework.TestCase;
-import org.codelutin.util.FileUtil;
-
import java.io.File;
import java.util.Map;
/** @author chemit */
-public class LicenseResolverTest extends TestCase {
+public class LicenseResolverTest extends BaseLicenseTestCase {
- protected String encoding = "utf-8";
-
public void testResolvIllegalArgumentException() throws Exception {
LicenseResolver resolver = new LicenseResolver();
@@ -65,6 +60,7 @@
}
+
public void testResolv() throws Exception {
LicenseResolver resolver = new LicenseResolver();
@@ -72,51 +68,45 @@
long timestamp = System.currentTimeMillis();
String licenseName = "dummy_" + timestamp;
- File file = new File(getBaseDir(), "target" + File.separator + "licenses_" + timestamp);
+ File repo = createLicenseRepository(false);
- resolver.setBaseURL(file.toURI().toURL().toString());
+ resolver.setBaseURL(repo.toURI().toURL().toString());
License license;
try {
- // no licenses.txt
+ // no licenses.properties
resolver.resolv(licenseName);
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
- FileUtil.writeString(new File(file, "licenses.properties"), licenseName + "=My dummy license\n", encoding);
- license = resolver.resolv(licenseName);
- assertNull(license);
+ addLicenseToRepository(repo, licenseName);
+ //FileUtil.writeString(new File(repo, "licenses.properties"), licenseName + "=My dummy license\n", encoding);
+ //license = resolver.resolv(licenseName);
+ //assertNull(license);
+
Map<String, String> licenseNames = resolver.getLicenseNames();
assertNotNull(licenseNames);
assertEquals(1, licenseNames.size());
assertTrue(licenseNames.containsKey(licenseName));
// create dummy licenses
- file = new File(file, licenseName);
- file.mkdirs();
- FileUtil.writeString(new File(file, "license.txt"), "license", encoding);
- FileUtil.writeString(new File(file, "header.txt"), "header", encoding);
- license = resolver.resolv(licenseName);
+ //File file = new File(repo, licenseName);
+ //file.mkdirs();
+ //FileUtil.writeString(new File(file, "license.txt"), "license", encoding);
+ //FileUtil.writeString(new File(file, "header.txt"), "header", encoding);
+ license = resolver.resolv(licenseName);
assertNotNull(license);
assertEquals(licenseName, license.getName());
assertNotNull(license.getLicenseContent(encoding));
assertNotNull(license.getHeaderContent(encoding));
- assertEquals("license", license.getLicenseContent(encoding));
- assertEquals("header", license.getHeaderContent(encoding));
+ assertEquals("license:"+licenseName, license.getLicenseContent(encoding));
+ assertEquals("header:"+licenseName, license.getHeaderContent(encoding));
}
- protected File getBaseDir() {
- String path = System.getenv("basedir");
- if (path == null) {
- path = new File("").getAbsolutePath();
- }
- return new File(path);
- }
-
}
\ No newline at end of file
1
0
[Lutinutil-commits] r1174 - maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 21:43:43 +0000 (Sun, 12 Oct 2008)
New Revision: 1174
Added:
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/BaseLicenseTestCase.java
Log:
base test class
Added: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/BaseLicenseTestCase.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/BaseLicenseTestCase.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/BaseLicenseTestCase.java 2008-10-12 21:43:43 UTC (rev 1174)
@@ -0,0 +1,60 @@
+package org.codelutin.license;
+
+import junit.framework.TestCase;
+import org.codelutin.util.FileUtil;
+
+import java.io.File;
+import java.io.IOException;
+
+/** @author chemit */
+public abstract class BaseLicenseTestCase extends TestCase {
+
+ protected static final String encoding = "utf-8";
+
+ protected File getBaseDir() {
+ String path = System.getenv("basedir");
+ if (path == null) {
+ path = new File("").getAbsolutePath();
+ }
+ return new File(path);
+ }
+
+ protected void assertLicenseFound(String name, License license) throws IOException {
+ assertNotNull(license);
+ assertEquals(name, license.getName());
+ assertNotNull(license.getLicenseContent(encoding));
+ assertNotNull(license.getHeaderContent(encoding));
+ }
+
+ protected File createLicenseRepository(boolean createLicene) throws IOException {
+ long timestamp = System.currentTimeMillis();
+
+ File repo = new File(getBaseDir(), "target" + File.separator + "licenses_" + timestamp);
+
+ if (createLicene) {
+
+ String licenseName = "dummy_" + timestamp;
+ addLicenseToRepository(repo, licenseName);
+ }
+ return repo;
+ }
+
+ protected void addLicenseToRepository(File repo, String licenseName) throws IOException {
+
+ File defFile = new File(repo, "licenses.properties");
+
+ String content = "";
+ if (defFile.exists()) {
+ content = FileUtil.readAsString(defFile, encoding) + "\n";
+ }
+
+ FileUtil.writeString(defFile, content + licenseName + "=My dummy license\n", encoding);
+ // create dummy licenses
+ File file = new File(repo, licenseName);
+ file.mkdirs();
+ FileUtil.writeString(new File(file, "license.txt"), "license:" + licenseName, encoding);
+ FileUtil.writeString(new File(file, "header.txt"), "header:" + licenseName, encoding);
+
+ }
+}
+
1
0
[Lutinutil-commits] r1173 - maven-i18n-plugin/trunk
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 20:46:16 +0000 (Sun, 12 Oct 2008)
New Revision: 1173
Modified:
maven-i18n-plugin/trunk/pom.xml
Log:
version 0.7-SNAPSHOT
Modified: maven-i18n-plugin/trunk/pom.xml
===================================================================
--- maven-i18n-plugin/trunk/pom.xml 2008-10-12 20:45:28 UTC (rev 1172)
+++ maven-i18n-plugin/trunk/pom.xml 2008-10-12 20:46:16 UTC (rev 1173)
@@ -52,7 +52,7 @@
<!-- *** Project Information ************************************* -->
<!-- ************************************************************* -->
<name>Plugin maven pour lutini18n</name>
- <version>0.6</version>
+ <version>0.7-SNAPSHOT</version>
<description>
Plugin pour maven 2 pour la génération des bundles pour l'internationnalisation des applications java fait par
lutinutil-i18n
1
0
[Lutinutil-commits] r1172 - maven-i18n-plugin/trunk
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 20:45:28 +0000 (Sun, 12 Oct 2008)
New Revision: 1172
Modified:
maven-i18n-plugin/trunk/pom.xml
Log:
license-switcher plugin can be in lutinpluginproject (use the license-switcher.version to make updrage on the plugin)
Modified: maven-i18n-plugin/trunk/pom.xml
===================================================================
--- maven-i18n-plugin/trunk/pom.xml 2008-10-12 20:43:11 UTC (rev 1171)
+++ maven-i18n-plugin/trunk/pom.xml 2008-10-12 20:45:28 UTC (rev 1172)
@@ -68,27 +68,6 @@
<build>
<plugins>
- <!-- jrst -->
- <plugin>
- <groupId>org.codelutin</groupId>
- <artifactId>maven-jrst-plugin</artifactId>
- </plugin>
-
- <!-- plugin license-switcher -->
- <plugin>
- <groupId>org.codelutin</groupId>
- <artifactId>maven-license-switcher-plugin</artifactId>
- <version>0.2</version>
- <executions>
- <execution>
- <goals>
- <goal>license</goal>
- <goal>third-party</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
-
</plugins>
</build>
@@ -113,32 +92,5 @@
<url>${maven.scm.url}</url>
</scm>
- <!-- Maven Environment : profiles -->
- <profiles>
- <profile>
- <id>license</id>
- <activation>
- <activeByDefault>false</activeByDefault>
- </activation>
- <build>
- <plugins>
- <plugin>
- <groupId>org.codelutin</groupId>
- <artifactId>maven-license-switcher-plugin</artifactId>
- <version>0.2</version>
- <executions>
- <execution>
- <goals>
- <goal>switch</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </profile>
- </profiles>
-
-
</project>
1
0
[Lutinutil-commits] r1171 - in maven-license-switcher-plugin/trunk: . src/main/java/org/codelutin/license src/test/java/org/codelutin/license
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 20:43:11 +0000 (Sun, 12 Oct 2008)
New Revision: 1171
Added:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java
Removed:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java
Modified:
maven-license-switcher-plugin/trunk/pom.xml
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java
maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java
Log:
using LicenseResolver and getLicenseNames
Modified: maven-license-switcher-plugin/trunk/pom.xml
===================================================================
--- maven-license-switcher-plugin/trunk/pom.xml 2008-10-12 20:42:31 UTC (rev 1170)
+++ maven-license-switcher-plugin/trunk/pom.xml 2008-10-12 20:43:11 UTC (rev 1171)
@@ -44,7 +44,7 @@
<!-- *** Project Information ************************************* -->
<!-- ************************************************************* -->
<name>Plugin maven pour switcher les licenses</name>
- <version>0.2</version>
+ <version>0.3-SNAPSHOT</version>
<description>Plugin pour switcher de license dans un module maven 2</description>
<inceptionYear>2008</inceptionYear>
@@ -61,11 +61,11 @@
<plugin>
<groupId>org.codelutin</groupId>
<artifactId>maven-jrst-plugin</artifactId>
- <version>0.8.2</version>
+ <!--version>0.8.2</version-->
</plugin>
<!-- plugin license switcher -->
- <plugin>
+ <!--plugin>
<groupId>org.codelutin</groupId>
<artifactId>maven-license-switcher-plugin</artifactId>
<version>0.2</version>
@@ -77,7 +77,7 @@
</goals>
</execution>
</executions>
- </plugin>
+ </plugin-->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
@@ -127,7 +127,7 @@
</scm>
<!-- Maven Environment : profiles -->
- <profiles>
+ <!--profiles>
<profile>
<id>license</id>
<activation>
@@ -150,6 +150,6 @@
</plugins>
</build>
</profile>
- </profiles>
+ </profiles-->
</project>
Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java 2008-10-12 20:42:31 UTC (rev 1170)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -17,23 +17,17 @@
*
* @author chemit
*/
-public class JarLicenseResolver implements LicenseResolver {
+public class JarLicenseResolver extends LicenseResolver {
protected static final Log log = LogFactory.getLog(JarLicenseResolver.class);
- public License resolv(String licenseName) throws IllegalArgumentException {
- if (licenseName == null) {
- throw new IllegalArgumentException("licenceName can not be null");
- }
- String prefix = File.separator + "META-INF" + File.separator + "licenses" + File.separator + licenseName.toLowerCase() + File.separator;
- URL licenseURL = getClass().getResource(prefix + "license.txt");
- URL headerURL = getClass().getResource(prefix + "header.txt");
-
- if (licenseURL != null && headerURL != null) {
- return new License(licenseName, licenseURL, headerURL);
- }
-
- return null;
+ public JarLicenseResolver() {
+ super(acquireBaseURL());
}
+ protected static String acquireBaseURL() {
+ String prefix = File.separator + "META-INF" + File.separator + "licenses";
+ URL licenseURL = JarLicenseResolver.class.getResource(prefix);
+ return licenseURL.toString();
+ }
}
Added: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -0,0 +1,63 @@
+package org.codelutin.license;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.TreeMap;
+
+/**
+ * Class responsible of instanciate {@link License}.
+ *
+ * @author chemit
+ */
+public class LicenseFactory {
+
+ protected LicenseResolver[] resolvers;
+
+ public static LicenseFactory newInstance(String extraResolver) throws IllegalArgumentException {
+ if (extraResolver == null || extraResolver.trim().isEmpty()) {
+ return new LicenseFactory(new LicenseResolver[]{
+ new JarLicenseResolver(),
+ });
+ }
+ return new LicenseFactory(new LicenseResolver[]{
+ new JarLicenseResolver(),
+ new LicenseResolver(extraResolver)
+ });
+ }
+
+ public Map<String, String> getLicenseNames() throws IllegalArgumentException {
+ Map<String, String> result = new TreeMap<String, String>();
+ for (LicenseResolver resolver : resolvers) {
+ Map<String, String> tmp = resolver.getLicenseNames();
+ for (Entry<String, String> s : tmp.entrySet()) {
+ if (!result.containsKey(s.getKey())) {
+ result.put(s.getKey(), s.getValue());
+ }
+ }
+ }
+ return result;
+ }
+
+
+ public License revolv(String licenseName) throws IllegalArgumentException {
+ if (licenseName == null) {
+ throw new IllegalArgumentException("licenseName can not be null");
+ }
+ for (LicenseResolver resolver : resolvers) {
+ License result = resolver.resolv(licenseName);
+ if (result != null) {
+ return result;
+ }
+ }
+ throw new IllegalArgumentException("could not find the license " + licenseName + " with resolvers" + Arrays.toString(resolvers));
+ }
+
+ protected LicenseFactory(LicenseResolver[] resolvers) throws IllegalArgumentException {
+ if (resolvers == null || resolvers.length == 0) {
+ throw new IllegalArgumentException("no resolver defined");
+ }
+ this.resolvers = resolvers;
+ }
+
+}
Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java 2008-10-12 20:42:31 UTC (rev 1170)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -52,16 +52,19 @@
/**
* Le type de license a appliquer.
*
- * @parameter expression="${license-switcher.licenseType}" default-value="LGPL_V3"
+ * @parameter expression="${license-switcher.licenseName}"
* @required
*/
- protected String licenseType;
+ protected String licenseName;
- /** le header a ajouter dans chaque fichier java */
- protected String licenseHeaderContent;
+ /**
+ * La baseURL d'un resolver de license supplementaire
+ *
+ * @parameter expression="${license-switcher.extraResolver}"
+ */
+ protected String extraResolver;
- /** le texte de la licence a placer dans le fichier ${basedir}/LICENSE.txt */
- protected String licenseFileContent;
+ protected License license;
protected void init() throws Exception {
@@ -70,11 +73,12 @@
if (doGenerate) {
- // prepare licenseFileContent
+ // acquire license
- LicenseType type = LicenseType.valueOf(licenseType);
+ LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
- licenseFileContent = FileUtil.readAsString(type.getLicenceFileReader(encoding));
+ license = factory.revolv(licenseName);
+
}
}
@@ -85,6 +89,12 @@
if (doGenerate) {
+ getLog().info("using licence ["+licenseName+"]");
+
+ if (verbose) {
+ getLog().info("licence : "+license);
+ }
+
if (licenseFile.exists() && keepBackup) {
if (verbose) {
getLog().info("backup " + licenseFile);
@@ -94,7 +104,7 @@
licenseFile.renameTo(backup);
}
try {
- FileUtil.writeString(licenseFile, licenseFileContent, encoding);
+ FileUtil.writeString(licenseFile, license.getLicenseContent(encoding), encoding);
} catch (IOException e) {
throw new MojoExecutionException("could not write license file " + licenseFile + " for reason : " + e.getMessage(), e);
Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 20:42:31 UTC (rev 1170)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -1,8 +0,0 @@
-package org.codelutin.license;
-
-/** @author chemit */
-public interface LicenseResolver {
-
- License resolv(String licenseName) throws IllegalArgumentException;
-
-}
Added: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -0,0 +1,133 @@
+package org.codelutin.license;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.TreeMap;
+
+/**
+ * Un resolver de license a partir d'un {@link #baseURL} .
+ * <p/>
+ * On doit avoir un fichier
+ * <pre>baseURL/licenses.txt
+ * </pre> qui contient la liste des licenses gerees par ce resolver.
+ * <p/>
+ * Chaque license est definit par deux fichiers :
+ * <pre>
+ * baseURL/licenseName/license.txt
+ * baseURL/licenseName/header.txt
+ * </pre>
+ *
+ * @author chemit
+ */
+public class LicenseResolver {
+
+ protected static final Log log = LogFactory.getLog(LicenseResolver.class);
+
+ /**
+ * BaseURL
+ *
+ * @required
+ */
+ protected String baseURL;
+
+ /** list of known licenses with this resolver */
+ protected Map<String, String> licenseNames;
+
+ public LicenseResolver() {
+ }
+
+ public LicenseResolver(String baseURL) {
+ this.baseURL = baseURL;
+ }
+
+ public String getBaseURL() {
+ return baseURL;
+ }
+
+ public void setBaseURL(String baseURL) {
+ this.baseURL = baseURL;
+ // reset licenseNames
+ licenseNames = null;
+ }
+
+ public Map<String, String> getLicenseNames() {
+ if (licenseNames == null) {
+ if (baseURL == null || baseURL.trim().isEmpty()) {
+ throw new IllegalArgumentException("no baseURL defined in " + this);
+ }
+
+ Map<String, String> list = new TreeMap<String, String>();
+ try {
+ URL licenseURL = new URL(baseURL + "/licenses.properties");
+
+ if (!checkExists(licenseURL)) {
+ throw new IllegalArgumentException("no licenses.properties found withurl [" + licenseURL + "] for resolver " + this);
+ }
+ Properties p = new Properties();
+ p.load(licenseURL.openStream());
+ for (Entry<Object, Object> entry : p.entrySet()) {
+ list.put((String) entry.getKey(), entry.getValue() + " [" + baseURL + "]");
+ }
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException(e);
+ } catch (IOException e) {
+ return null;
+ }
+
+ licenseNames = Collections.unmodifiableMap(list);
+ }
+ return licenseNames;
+ }
+
+ public License resolv(String licenseName) throws IllegalArgumentException {
+ if (baseURL == null || baseURL.trim().isEmpty()) {
+ throw new IllegalArgumentException("no baseURL defined in " + this);
+ }
+ if (licenseName == null || licenseName.trim().isEmpty()) {
+ throw new IllegalArgumentException("licenceName can not be null");
+ }
+ licenseName = licenseName.trim().toLowerCase();
+
+ if (!getLicenseNames().containsKey(licenseName)) {
+ // license not found
+ return null;
+ }
+
+ try {
+ URL licenseURL = new URL(baseURL + "/" + licenseName + "/license.txt");
+ URL headerURL = new URL(baseURL + "/" + licenseName + "/header.txt");
+
+ if (!checkExists(licenseURL) || !checkExists(headerURL)) {
+ // no content find
+ return null;
+ }
+
+ return new License(licenseName, licenseURL, headerURL);
+
+
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException(e);
+ } catch (IOException e) {
+ return null;
+ }
+
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + "<baseURL:" + baseURL + ", licenseNames:" + licenseNames + ">";
+ }
+
+ protected boolean checkExists(URL url) throws IOException {
+ return url.openConnection().getContentLength() > 0;
+ }
+
+}
\ No newline at end of file
Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-10-12 20:42:31 UTC (rev 1170)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -75,10 +75,10 @@
/**
* Le type de license a appliquer.
*
- * @parameter expression="${license-switcher.licenseType}" default-value="LGPL_V3"
+ * @parameter expression="${license-switcher.licenseName}"
* @required
*/
- protected String licenseType;
+ protected String licenseName;
/**
* Repertoires des fichiers sources a traiter.
@@ -96,6 +96,13 @@
*/
protected List<String> testCompileSourceRoots;
+ /**
+ * Un resolver externe
+ *
+ * @parameter expression="${license-switcher.extraResolver}"
+ */
+ protected String extraResolver;
+
/** le header a ajouter dans chaque fichier source java */
protected String licenseHeaderContent;
@@ -108,8 +115,6 @@
timestamp = System.nanoTime();
- LicenseType type = LicenseType.valueOf(licenseType);
-
// obtain all java source files to be treated
javaFilesToTreate = getFilesToTreate();
@@ -118,10 +123,13 @@
return;
}
+ LicenseFactory factory = LicenseFactory.newInstance(extraResolver);
+
+ License license = factory.revolv(licenseName);
+
// obtain content of license header
- licenseHeaderContent = computeHeader(type);
+ licenseHeaderContent = computeHeader(license);
-
if (verbose) {
getLog().info("header to write on java source files \n" + licenseHeaderContent);
}
@@ -190,9 +198,11 @@
return files;
}
- protected String computeHeader(LicenseType type) throws IOException {
- String tmpHeader = FileUtil.readAsString(type.getLicenceHeaderReader(encoding));
+ protected String computeHeader(License license) throws IOException {
+
+ String tmpHeader = license.getHeaderContent(encoding);
+
// defined inceptionYear (if year is older than now suffix with a - thisYear)
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Modified: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java 2008-10-12 20:42:31 UTC (rev 1170)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -3,6 +3,9 @@
import junit.framework.TestCase;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.ArrayList;
/** @author chemit */
public class JarLicenseResolverTest extends TestCase {
@@ -19,6 +22,20 @@
assertTrue(true);
}
+ try {
+ resolver.resolv("");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ try {
+ resolver.resolv(" ");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
{
String name = "dummy_" + System.currentTimeMillis();
License license = resolver.resolv(name);
@@ -37,6 +54,17 @@
}
+ public void testGetLicenseNames() throws Exception {
+ JarLicenseResolver resolver = new JarLicenseResolver();
+ Map<String,String> licenseNames = resolver.getLicenseNames();
+
+ assertEquals(Arrays.asList("gpl_v1",
+ "gpl_v2",
+ "gpl_v3",
+ "lgpl_v2_1",
+ "lgpl_v3"), new ArrayList<String>(licenseNames.keySet()));
+ }
+
protected void assertLicenseFound(String name, License license) throws IOException {
System.out.println(license);
assertNotNull(license);
Added: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -0,0 +1,43 @@
+package org.codelutin.license;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+
+/** @author chemit */
+public class LicenseFactoryTest extends TestCase {
+
+ public void testNewInstance() throws Exception {
+
+ LicenseFactory factory = LicenseFactory.newInstance(null);
+ assertNotNull(factory);
+ assertEquals(1, factory.resolvers.length);
+ assertNotNull(factory.resolvers[0]);
+ assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass());
+
+ factory = LicenseFactory.newInstance("");
+ assertNotNull(factory);
+ assertEquals(1, factory.resolvers.length);
+ assertNotNull(factory.resolvers[0]);
+ assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass());
+
+ factory = LicenseFactory.newInstance("yo");
+ assertNotNull(factory);
+ assertEquals(2, factory.resolvers.length);
+ assertNotNull(factory.resolvers[0]);
+ assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass());
+ assertNotNull(factory.resolvers[1]);
+ assertEquals(LicenseResolver.class, factory.resolvers[1].getClass());
+ assertEquals("yo", factory.resolvers[1].getBaseURL());
+ }
+
+ public void testGetLicenseNames() throws Exception {
+ LicenseFactory factory = LicenseFactory.newInstance(null);
+
+ Map<String, String> licenseNames = factory.getLicenseNames();
+ //todo make dynamic that test
+ assertEquals(5, licenseNames.size());
+
+ }
+
+}
Copied: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java (from rev 1165, maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java)
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java (rev 0)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -0,0 +1,122 @@
+package org.codelutin.license;
+
+import junit.framework.TestCase;
+import org.codelutin.util.FileUtil;
+
+import java.io.File;
+import java.util.Map;
+
+/** @author chemit */
+public class LicenseResolverTest extends TestCase {
+
+ protected String encoding = "utf-8";
+
+ public void testResolvIllegalArgumentException() throws Exception {
+
+ LicenseResolver resolver = new LicenseResolver();
+
+ try {
+ // no baseURL
+ // no licenseName
+ // no licenses.txt
+ resolver.resolv(null);
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ try {
+ // no baseURL
+ // no licenseName
+ // no licenses.txt
+ resolver.resolv("");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ try {
+ // no baseURL
+ // no licenseName
+ // no licenses.txt
+ resolver.resolv(" ");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ try {
+ // no baseURL
+ // no licenses.txt
+ resolver.resolv("dummy");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ resolver.setBaseURL("dummy");
+ try {
+ // no licenses.txt
+ resolver.resolv("dummy");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ }
+
+ public void testResolv() throws Exception {
+
+ LicenseResolver resolver = new LicenseResolver();
+
+ long timestamp = System.currentTimeMillis();
+ String licenseName = "dummy_" + timestamp;
+
+ File file = new File(getBaseDir(), "target" + File.separator + "licenses_" + timestamp);
+
+ resolver.setBaseURL(file.toURI().toURL().toString());
+
+ License license;
+
+ try {
+ // no licenses.txt
+ resolver.resolv(licenseName);
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ FileUtil.writeString(new File(file, "licenses.properties"), licenseName + "=My dummy license\n", encoding);
+ license = resolver.resolv(licenseName);
+ assertNull(license);
+
+ Map<String, String> licenseNames = resolver.getLicenseNames();
+ assertNotNull(licenseNames);
+ assertEquals(1, licenseNames.size());
+ assertTrue(licenseNames.containsKey(licenseName));
+
+ // create dummy licenses
+ file = new File(file, licenseName);
+ file.mkdirs();
+ FileUtil.writeString(new File(file, "license.txt"), "license", encoding);
+ FileUtil.writeString(new File(file, "header.txt"), "header", encoding);
+ license = resolver.resolv(licenseName);
+
+ assertNotNull(license);
+ assertEquals(licenseName, license.getName());
+ assertNotNull(license.getLicenseContent(encoding));
+ assertNotNull(license.getHeaderContent(encoding));
+ assertEquals("license", license.getLicenseContent(encoding));
+ assertEquals("header", license.getHeaderContent(encoding));
+
+ }
+
+ protected File getBaseDir() {
+ String path = System.getenv("basedir");
+ if (path == null) {
+ path = new File("").getAbsolutePath();
+ }
+ return new File(path);
+ }
+
+}
\ No newline at end of file
Property changes on: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java 2008-10-12 20:42:31 UTC (rev 1170)
+++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java 2008-10-12 20:43:11 UTC (rev 1171)
@@ -1,38 +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;
-
-/**
- * A class to test loading of license via {@link LicenseType}
- *
- * @author chemit
- */
-public class LicenseTypeTest extends junit.framework.TestCase {
-
- private static final String ENCODING = "UTF-8";
-
- public void testAcquireLicenceReaders() throws Exception {
- for (LicenseType license : LicenseType.values()) {
- license.getLicenceFileReader(ENCODING);
- license.getLicenceHeaderReader(ENCODING);
- }
- }
-
-}
1
0
[Lutinutil-commits] r1170 - maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
by tchemit@users.labs.libre-entreprise.org 12 Oct '08
12 Oct '08
Author: tchemit
Date: 2008-10-12 20:42:31 +0000 (Sun, 12 Oct 2008)
New Revision: 1170
Removed:
maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseType.java
Log:
no more license type
Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseType.java
===================================================================
--- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseType.java 2008-10-12 20:42:16 UTC (rev 1169)
+++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseType.java 2008-10-12 20:42:31 UTC (rev 1170)
@@ -1,63 +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;
-
-import java.io.File;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-
-/**
- * Une enumeration pour definir le type d'une licence a utiliser.
- * <p/>
- * Chaque licence doit definir 2 fichiers textes (licence.txt et header.txt) dans le repertoire <code>name().toLowerCase()</code>
- * du classpath.
- * <p/>
- * La methode {@link #getLicenceFileReader(String)} permet de recuperer le contenu de la licence.
- * <p/>
- * La methode {@link #getLicenceHeaderReader(String)} permet de recuperer le contenu de l'en-tete de la licence a appliquer.
- *
- * @author chemit
- */
-public enum LicenseType {
- GPL_V1,
- GPL_V2,
- GPL_V3,
- LGPL_V2_1,
- LGPL_V3;
-
- public Reader getLicenceFileReader(String encoding) throws UnsupportedEncodingException {
- String prefix = File.separator + "META-INF" + File.separator + "licenses" + File.separator + name().toLowerCase() + File.separator;
- InputStream resource = getClass().getResourceAsStream(prefix + "license.txt");
- if (resource == null) {
- throw new IllegalStateException("could not find license for " + this);
- }
- return new InputStreamReader(resource, encoding);
- }
-
- public Reader getLicenceHeaderReader(String encoding) throws UnsupportedEncodingException {
- String prefix = File.separator + "META-INF" + File.separator + "licenses" + File.separator + name().toLowerCase() + File.separator;
- InputStream resource = getClass().getResourceAsStream(prefix + "header.txt");
- if (resource == null) {
- throw new IllegalStateException("could not find license header for " + this);
- }
- return new InputStreamReader(resource, encoding);
- }
-}
1
0