[Lutinutil-commits] r1534 - in maven-license-switcher-plugin/trunk: . src/main/java/org src/main/java/org/nuiton src/main/java/org/nuiton/license src/main/java/org/nuiton/license/plugin src/test/java/org src/test/java/org/nuiton src/test/java/org/nuiton/license
Author: tchemit Date: 2009-05-13 15:31:20 +0000 (Wed, 13 May 2009) New Revision: 1534 Added: maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/ maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/ maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/JavaLicenseGenerator.java maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/XmlLicenseGenerator.java maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/plugin/ maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/plugin/SwitchLicensePlugin.java maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/ maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/ maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/JavaLicenseGeneratorTest.java maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/XmlLicenseGeneratorTest.java maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/plugin/ Modified: maven-license-switcher-plugin/trunk/pom.xml Log: migrate to nuiton Modified: maven-license-switcher-plugin/trunk/pom.xml =================================================================== --- maven-license-switcher-plugin/trunk/pom.xml 2009-05-13 15:31:01 UTC (rev 1533) +++ maven-license-switcher-plugin/trunk/pom.xml 2009-05-13 15:31:20 UTC (rev 1534) @@ -10,8 +10,13 @@ <groupId>org.codelutin</groupId> <artifactId>lutinproject</artifactId> <version>3.5.4</version> + <!--groupId>org.nuiton</groupId> + <artifactId>mavenpom</artifactId> + <version>1.0.0-SNAPSHOT</version--> </parent> + <groupId>org.nuiton</groupId> + <artifactId>maven-license-switcher-plugin</artifactId> <version>1.0.0-SNAPSHOT</version> @@ -26,35 +31,50 @@ </dependency> <dependency> - <groupId>org.codelutin</groupId> - <artifactId>lutinpluginutil</artifactId> - <version>${lutinpluginutil.version}</version> + <groupId>org.nuiton</groupId> + <artifactId>maven-helper-plugin</artifactId> + <version>${helper.version}</version> <scope>compile</scope> </dependency> - <dependency> + <!--dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-dependency-tree</artifactId> <version>1.2</version> - </dependency> + </dependency--> <!-- tests dependencies --> - <dependency> + <!--dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-plugin-testing-harness</artifactId> <version>1.1</version> <scope>test</scope> - </dependency> + </dependency--> - <dependency> + <!--dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-verifier</artifactId> <version>1.0</version> <scope>test</scope> + </dependency--> + + <dependency> + <groupId>org.nuiton</groupId> + <artifactId>maven-helper-plugin</artifactId> + <version>${helper.version}</version> + <scope>test</scope> + <classifier>tests</classifier> </dependency> <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.6</version> + <scope>test</scope> + </dependency> + + <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>${maven.version}</version> @@ -85,14 +105,19 @@ <packaging>maven-plugin</packaging> <properties> - <labs.id>12</labs.id> - <labs.project>lutinutil</labs.project> - <processor.version>0.18</processor.version> - <lutinpluginutil.version>0.5</lutinpluginutil.version> + <nuiton.project>nuitonbuilder</nuiton.project> + <processor.version>0.18</processor.version> + + <!-- assuprimer du de l'utilisation de mavenpom --> + <labs.id>12</labs.id> + <labs.project>lutinutil</labs.project> + <maven.version>2.0.10</maven.version> + <helper.version>1.0.0-SNAPSHOT</helper.version> + </properties> <build> @@ -113,10 +138,10 @@ </executions> </plugin> - <plugin> + <!--plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> - <!--Copy licenses files for site--> + <execution> <id>CopySiteFiles2</id> <phase>pre-site</phase> @@ -135,10 +160,10 @@ </goals> </execution> </executions> - </plugin> + </plugin--> <!-- plugin site --> - <plugin> - <artifactId>maven-site-plugin</artifactId> + <!--plugin> + <artifactId>maven-site-plugin</artifactId> <dependencies> <dependency> <groupId>org.codelutin</groupId> @@ -146,14 +171,14 @@ <version>1.0.0</version> </dependency> </dependencies> - </plugin> + </plugin--> </plugins> + </build> <!--Site--> <reporting> - <plugins> - <!--Site report's plugin--> + <plugins> <plugin> <artifactId>maven-plugin-plugin</artifactId> </plugin> @@ -188,36 +213,4 @@ </repository> </repositories> - <profiles> - <!-- perform only on a release stage when using the maven-release-plugin --> - <profile> - <id>release-profile</id> - <activation> - <property> - <name>performRelease</name> - <value>true</value> - </property> - </activation> - <build> - <plugins> - <!-- always add license and third-party files to classpath --> - <plugin> - <groupId>org.codelutin</groupId> - <artifactId>maven-license-switcher-plugin</artifactId> - <version>0.8</version> - <executions> - <execution> - <id>attach-licenses</id> - <goals> - <goal>license</goal> - <goal>third-party</goal> - </goals> - </execution> - </executions> - </plugin> - </plugins> - </build> - </profile> - </profiles> - </project> Added: maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/JavaLicenseGenerator.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/JavaLicenseGenerator.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/JavaLicenseGenerator.java 2009-05-13 15:31:20 UTC (rev 1534) @@ -0,0 +1,13 @@ +package org.nuiton.license; + +import org.codelutin.processor.filters.LicenseFilter; + +/** @author chemit */ +public class JavaLicenseGenerator implements LicenseGenerator { + + @Override + public String getHeader(String licenseHeaderContent) { + return "/**\n * " + LicenseFilter.HEADER + " " + licenseHeaderContent + " " + LicenseFilter.FOOTER + "\n */\n"; + } + +} Added: maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/XmlLicenseGenerator.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/XmlLicenseGenerator.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/XmlLicenseGenerator.java 2009-05-13 15:31:20 UTC (rev 1534) @@ -0,0 +1,13 @@ +package org.nuiton.license; + +import org.codelutin.processor.filters.LicenseFilter; + +/** @author chemit */ +public class XmlLicenseGenerator implements LicenseGenerator { + + @Override + public String getHeader(String licenseHeaderContent) { + return "<!--\n\n/**\n * " + LicenseFilter.HEADER + " " + licenseHeaderContent + " " + LicenseFilter.FOOTER + "\n */\n\n-->\n"; + } + +} \ No newline at end of file Copied: maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/plugin/SwitchLicensePlugin.java (from rev 1522, maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/impl/SwitchLicensePlugin.java) =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/plugin/SwitchLicensePlugin.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/plugin/SwitchLicensePlugin.java 2009-05-13 15:31:20 UTC (rev 1534) @@ -0,0 +1,312 @@ +/** + * *##% 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.nuiton.license.plugin; + +import org.apache.maven.plugin.MojoExecutionException; + +import org.codelutin.processor.LicenseProcessor; +import org.codelutin.processor.filters.LicenseFilter; +import org.nuiton.util.FileUtil; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import org.nuiton.AbstractPlugin; +import org.nuiton.license.License; +import org.nuiton.license.LicenseFactory; +import org.nuiton.license.LicenseGenerator; +import org.nuiton.util.FileUpdaterHelper; +import org.nuiton.util.MirroredFileUpdater; + +/** + * 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 SwitchLicensePlugin extends AbstractPlugin { + + /** + * Encoding a utiliser pour lire et ecrire les fichiers. + * + * @parameter expression="${helper.encoding}" default-value="${project.build.sourceEncoding}" + * @required + * @since 1.0.0 + */ + protected String encoding; + /** + * Un flag pour forcer la generation. + * + * @parameter expression="${helper.force}" default-value="false" + * @since 1.0.0 + */ + protected boolean force; + /** + * l'annee de creation du module (sera place dans le header) + * + * @parameter default-value="${project.inceptionYear}" + * @required + * @since 1.0.0 + */ + protected String inceptionYear; + /** + * le nom de l'organisation (sera place dans le header) + * + * @parameter default-value="${project.organization.name}" + * @required + * @readonly + * @since 1.0.0 + */ + protected String organizationName; + /** + * le nom du projet (sera place dans le header) + * + * @parameter default-value="${project.name}" + * @required + * @readonly + * @since 1.0.0 + */ + protected String projectName; + /** + * Le type de license a appliquer. + * + * @parameter expression="${helper.licenseName}" + * @required + * @since 1.0.0 + */ + protected String licenseName; + /** + * Repertoires des fichiers sources a traiter. + * + * @parameter expression="${helper.compileSourceRoots}" default-value="${project.compileSourceRoots}" + * @required + * @since 1.0.0 + */ + protected List<String> compileSourceRoots; + /** + * Repertoires des fichiers sources de test a traiter. + * + * @parameter expression="${helper.testCompileSourceRoots}" default-value="${project.testCompileSourceRoots}" + * @required + * @since 1.0.0 + */ + protected List<String> testCompileSourceRoots; + /** + * Repertoire de sortie des classes (classpath). + * + * @parameter expression="${helper.outputDirectory}" default-value="${project.build.outputDirectory}" + * @required + * @since 1.0.0 + */ + protected File outputDirectory; + /** + * Repertoire du build des tests + * + * @parameter expression="${helper.testOutputDirectory}" default-value="${project.build.testOutputDirectory}" + * @required + * @since 1.0.0 + */ + protected File testOutputDirectory; + /** + * Un resolver externe + * + * @parameter expression="${helper.extraResolver}" + * @since 1.0.0 + */ + protected String[] extraResolver; + /** + * Un flag pour conserver un backup des fichiers modifies. + * + * @parameter expression="${helper.keepBackup}" default-value="false" + * @since 1.0.0 + */ + protected boolean keepBackup; + /** 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 Map<LicenseGenerator, List<String>> generators; + protected long timestamp; + + public SwitchLicensePlugin() { + super("all files are up-to-date."); + } + + @Override + public boolean ensurePackaging() { + return "pom".equals(project.getPackaging()) || "site".equals(project.getPackaging()); + } + + @Override + public boolean init() throws IOException { + + boolean doGenerate = force; + + timestamp = System.nanoTime(); + + // obtain all java source files to be treated + javaFilesToTreate = getFilesToTreate(); + + doGenerate &= !javaFilesToTreate.isEmpty(); + + if (doGenerate) { + + 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); + } + } + + return doGenerate; + } + + @Override + protected void doAction() throws Exception { + // 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() { + + Map<File, String[]> files = new java.util.HashMap<File, String[]>(); + + MirroredFileUpdater updater = FileUpdaterHelper.newJavaFileUpdater(); + + updater.setDestinationDirectory(outputDirectory); + getFilesToTreateForRoots(new String[]{"**\\/*.java"}, null, compileSourceRoots, files, updater); + + updater.setDestinationDirectory(testOutputDirectory); + getFilesToTreateForRoots(new String[]{"**\\/*.java"}, null, testCompileSourceRoots, files, updater); + + 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 Exception 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); + } + + protected void addLicenseToXmlSourceFile(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\n/**\n * " + LicenseFilter.HEADER + " " + licenseHeaderContent + " " + LicenseFilter.FOOTER + "\n */\n" + content + " \n\n-->"; + FileUtil.writeString(processFile, content, encoding); + } +} Property changes on: maven-license-switcher-plugin/trunk/src/main/java/org/nuiton/license/plugin/SwitchLicensePlugin.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/JavaLicenseGeneratorTest.java (from rev 1522, maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JavaLicenseGeneratorTest.java) =================================================================== --- maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/JavaLicenseGeneratorTest.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/JavaLicenseGeneratorTest.java 2009-05-13 15:31:20 UTC (rev 1534) @@ -0,0 +1,24 @@ +package org.nuiton.license; + +import org.junit.Before; +import org.junit.After; +import org.junit.Test; + +/** @author chemit */ +public class JavaLicenseGeneratorTest { + + @Before + public void setUp() { + // Add your code here + } + + @After + public void tearDown() { + // Add your code here + } + + @Test + public void testGetHeader() { + // Add your code here + } +} Property changes on: maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/JavaLicenseGeneratorTest.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/XmlLicenseGeneratorTest.java (from rev 1522, maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/XmlLicenseGeneratorTest.java) =================================================================== --- maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/XmlLicenseGeneratorTest.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/XmlLicenseGeneratorTest.java 2009-05-13 15:31:20 UTC (rev 1534) @@ -0,0 +1,23 @@ +package org.nuiton.license; + +import org.junit.Before; +import org.junit.After; +import org.junit.Test; + +/** @author chemit */ +public class XmlLicenseGeneratorTest { + @Before + public void setUp() { + // Add your code here + } + + @After + public void tearDown() { + // Add your code here + } + + @Test + public void testGetHeader() { + // Add your code here + } +} Property changes on: maven-license-switcher-plugin/trunk/src/test/java/org/nuiton/license/XmlLicenseGeneratorTest.java ___________________________________________________________________ Name: svn:mergeinfo +
participants (1)
-
tchemit@users.labs.libre-entreprise.org