Author: tchemit Date: 2008-10-12 16:46:35 +0000 (Sun, 12 Oct 2008) New Revision: 1163 Added: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/License.java maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java Removed: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenceMojo.java 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/LicenseType.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: rename base mojo introduce License object and LicenseResolver Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenceMojo.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenceMojo.java 2008-10-08 16:02:05 UTC (rev 1162) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenceMojo.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -1,107 +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 AbstractLicenceMojo extends AbstractMojo { - - /** - * la methode qui est lancee au debut de la methode {@link #execute()} pour preparer l'init du goal. - * - * @throws Exception if any - */ - protected abstract void init() throws Exception; - - /** - * Dependance du projet. - * - * @parameter default-value="${project}" - * @required - * @readonly - */ - protected MavenProject project; - - /** - * Repertoire de sortie des classes (classpath). - * - * @parameter expression="${licence-switcher.outputDirectory}" default-value="${project.build.outputDirectory}" - * @required - */ - protected File projectOutputDirectory; - - /** - * Encoding a utiliser pour lire et ecrire les fichiers. - * - * @parameter expression="${licence-switcher.encoding}" default-value="${maven.compile.encoding}" - * @required - */ - protected String encoding; - - /** - * Un flag pour conserver un backup des fichiers modifies. - * - * @parameter expression="${license-switcher.keepBackup}" default-value="false" - */ - protected boolean keepBackup; - /** - * Un flag pour activer le mode verbeux. - * - * @parameter expression="${license-switcher.verbose}" default-value="${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; - } - 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/AbstractLicenseMojo.java (from rev 1162, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenceMojo.java) =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/AbstractLicenseMojo.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -0,0 +1,108 @@ +/** + * *##% Plugin maven pour switcher les licenses + * Copyright (C) 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.codelutin.license; + +import org.apache.maven.plugin.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); + } + } +} Added: 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 (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -0,0 +1,39 @@ +package org.codelutin.license; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; +import java.net.URL; + +/** + * Un resolver de license dans le classpath. + * <p/> + * On recherche les fichiers dans les répertoires : + * <pre> + * /META-INF/licenses/licenseName/license.txt + * /META-INF/licenses/licenseName/header.txt + * </pre> + * + * @author chemit + */ +public class JarLicenseResolver implements 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; + } + +} Added: 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 (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/License.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -0,0 +1,100 @@ +package org.codelutin.license; + +import org.codelutin.util.FileUtil; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.InputStream; +import java.net.URL; + +/** + * The model of a license. + * + * @author chemit + */ +public class License { + + /** the name of the licenses (ex lgpl-3.0) */ + protected String name; + + /** url of the license's content */ + protected URL licenseURL; + + /** url of the header's template */ + protected URL headerURL; + + 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; + } + + public URL getLicenseURL() { + return licenseURL; + } + + public URL getHeaderURL() { + return headerURL; + } + + public String getLicenseContent(String encoding) throws IOException { + if (licenseURL == null) { + throw new IllegalStateException("no licenseURL defined in " + this); + } + + InputStream inputStream = licenseURL.openStream(); + Reader r = new BufferedReader(new InputStreamReader(inputStream, encoding)); + try { + return FileUtil.readAsString(r); + } finally { + r.close(); + } + } + + public String getHeaderContent(String encoding) throws IOException { + if (headerURL == null) { + throw new IllegalStateException("no headerURL defined in " + this); + } + Reader r = new BufferedReader(new InputStreamReader(headerURL.openStream(), encoding)); + try { + return FileUtil.readAsString(r); + } finally { + r.close(); + } + } + + public void setName(String name) { + this.name = name; + } + + public void setLicenseURL(URL licenseURL) { + this.licenseURL = licenseURL; + } + + public void setHeaderURL(URL headerURL) { + this.headerURL = headerURL; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(super.toString()); + sb.append("<name:").append(name); + sb.append(", licenseURL:").append(licenseURL.toString()); + sb.append(", headerURL:").append(headerURL.toString()); + sb.append(">"); + return sb.toString(); + } +} 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-08 16:02:05 UTC (rev 1162) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -33,7 +33,7 @@ * @phase process-classes * @requiresProject true */ -public class LicenseMojo extends AbstractLicenceMojo { +public class LicenseMojo extends AbstractLicenseMojo { /** * Un flag pour indiquer la regeneration des fichiers. * 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 16:46:35 UTC (rev 1163) @@ -0,0 +1,8 @@ +package org.codelutin.license; + +/** @author chemit */ +public interface LicenseResolver { + + License resolv(String licenseName) throws IllegalArgumentException; + +} Modified: 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-08 16:02:05 UTC (rev 1162) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseType.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -44,7 +44,8 @@ LGPL_V3; public Reader getLicenceFileReader(String encoding) throws UnsupportedEncodingException { - InputStream resource = getClass().getResourceAsStream(File.separator + name().toLowerCase() + File.separator + "license.txt"); + 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); } @@ -52,7 +53,8 @@ } public Reader getLicenceHeaderReader(String encoding) throws UnsupportedEncodingException { - InputStream resource = getClass().getResourceAsStream(File.separator + name().toLowerCase() + File.separator + "header.txt"); + 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); } 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-08 16:02:05 UTC (rev 1162) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -44,7 +44,7 @@ * @phase process-resources * @requiresProject true */ -public class SwitchLicenseMojo extends AbstractLicenceMojo { +public class SwitchLicenseMojo extends AbstractLicenseMojo { /** * l'annee de creation du module (sera place dans le header) Modified: 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-08 16:02:05 UTC (rev 1162) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/ThirdPartyMojo.java 2008-10-12 16:46:35 UTC (rev 1163) @@ -52,7 +52,7 @@ * @requiresDependencyResolution test * @requiresProject true */ -public class ThirdPartyMojo extends AbstractLicenceMojo { +public class ThirdPartyMojo extends AbstractLicenseMojo { private static final String unknownLicenseMessage = "Unknown license";