r1632 - in trunk/src: main/java/org/nuiton/license/header main/resources site site/rst test/java/org/nuiton/license/header test/resources/org/nuiton/license/header/UpdateHeaderPluginTest
Author: tchemit Date: 2009-08-28 12:14:10 +0200 (Fri, 28 Aug 2009) New Revision: 1632 Added: trunk/src/main/resources/defaultHeader.vm Modified: trunk/src/main/java/org/nuiton/license/header/UpdateHeaderPlugin.java trunk/src/site/rst/usage.rst trunk/src/site/site.xml trunk/src/test/java/org/nuiton/license/header/UpdateHeaderPluginTest.java trunk/src/test/resources/org/nuiton/license/header/UpdateHeaderPluginTest/bug_28.xml Log: - can customize header template (with velocity) - begin of usage docs Modified: trunk/src/main/java/org/nuiton/license/header/UpdateHeaderPlugin.java =================================================================== --- trunk/src/main/java/org/nuiton/license/header/UpdateHeaderPlugin.java 2009-08-26 20:26:44 UTC (rev 1631) +++ trunk/src/main/java/org/nuiton/license/header/UpdateHeaderPlugin.java 2009-08-28 10:14:10 UTC (rev 1632) @@ -24,11 +24,15 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.net.URL; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; import java.util.ServiceLoader; import java.util.TreeMap; import org.apache.maven.project.MavenProject; @@ -37,6 +41,7 @@ import org.nuiton.license.LicenseFactory; import org.nuiton.license.header.generator.HeaderGenerator; import org.nuiton.util.PluginHelper; +import org.nuiton.util.VelocityTemplateGenerator; /** * The goal to update (or add) the licence header on some files. @@ -167,6 +172,22 @@ */ protected String licenseResolver; /** + * La template (velocity) a utiliser pour construire le header. + * + * Cette template doit être dans le class-path ou être un fichier existant + * + * @parameter expression="${license.template}" default-value="/defaultHeader.vm" + * @since 1.0.1 + */ + protected String template; + /** + * Des paramètres supplémentaires à utiliser dans la template du header. + * + * @parameter + * @since 1.0.1 + */ + protected Map<String, String> templateParameters; + /** * Un flag pour conserver un backup des fichiers modifies. * * @parameter expression="${license.keepBackup}" default-value="false" @@ -220,7 +241,7 @@ } @Override - public boolean init() throws IOException, MojoExecutionException { + public boolean init() throws Exception { timestamp = System.nanoTime(); @@ -244,16 +265,26 @@ if (doGenerate) { + File templateFile = new File(template); + + // verifie que la template existe (dans le class-path ou en tant que fichier) + checkResource(templateFile); + + URL templateURL = getTemplate(templateFile); + + // recuperation de la license a utiliser LicenseFactory factory = LicenseFactory.newInstance(licenseResolver); License license = factory.revolv(licenseName); // obtain content of license header - licenseHeaderContent = computeHeader(license); + licenseHeaderContent = computeHeader(license, templateURL); + // recuperation du generateur de header a utiliser generator = availableGenerators.get(generatorName); if (verbose) { - getLog().info("config - will use the generator " + generator); + getLog().info("config - use the generator " + generator); + getLog().info("config - use template " + templateURL); getLog().info("config - header to use \n" + licenseHeaderContent); } } else { @@ -301,7 +332,7 @@ if (!foundLicenseHeader) { if (p.getLicenceFilter().isDetectHeader()) { - getLog().warn("license footer tag was not find on file " + file); + getLog().warn("skip file " + file + " (no license footer tag found : '##%*' !)"); } else { // no license header found in file, add it getLog().info("adding license header on file " + file); @@ -350,9 +381,9 @@ return files; } - protected String computeHeader(License license) throws IOException { + protected String computeHeader(License license, URL templateURL) throws Exception { - String tmpHeader = license.getHeaderContent(encoding); + String licenseContent = license.getHeaderContent(encoding); // defined inceptionYear (if year is older than now suffix with a - thisYear) Calendar cal = Calendar.getInstance(); @@ -362,11 +393,31 @@ inceptionYear = inceptionYear + " - " + thisYear; } - // format header with projet informations - tmpHeader = String.format(tmpHeader, projectName, inceptionYear, organizationName); + Properties env = new Properties(); + env.put("inceptionYear", inceptionYear); + env.put("projectName", projectName); + env.put("organizationName", organizationName); + env.put("licenseContent", licenseContent); + if (templateParameters != null) { + for (Entry<String, String> e : templateParameters.entrySet()) { + env.put(e.getKey(), e.getValue()); + } + } + if (verbose) { + getLog().info("will use parameters in template : "); + for (Entry<Object, Object> e : env.entrySet()) { + getLog().info("<" + e.getKey() + " : " + e.getValue() + ">"); + } + } + StringWriter f = new StringWriter(); + VelocityTemplateGenerator gen = new VelocityTemplateGenerator(project, templateURL); + gen.generate(env, f); + + String r = f.getBuffer().toString(); + // add " * " before each line - BufferedReader reader = new BufferedReader(new java.io.StringReader(tmpHeader)); + BufferedReader reader = new BufferedReader(new java.io.StringReader(r)); StringBuilder sb = new StringBuilder(); String line = reader.readLine(); @@ -379,7 +430,36 @@ sb.append(" * ").append(line).append("\n"); } } - tmpHeader = sb.toString(); - return tmpHeader.substring(0, tmpHeader.length() - 1); + + String result = sb.toString(); + + return result.substring(0, result.length() - 1); } + + protected URL getTemplate(File f) throws IOException { + URL r = null; + if (f.exists()) { + r = f.toURI().toURL(); + } else { + r = getClass().getResource(f.toString()); + } + return r; + } + + protected void checkResource(File f) throws IOException { + if (!f.exists()) { + // test in classPath + InputStream r = null; + try { + r = getClass().getResourceAsStream(f.toString()); + if (r == null) { + throw new IOException("could not find ressource " + f); + } + } finally { + if (r != null) { + r.close(); + } + } + } + } } Added: trunk/src/main/resources/defaultHeader.vm =================================================================== --- trunk/src/main/resources/defaultHeader.vm (rev 0) +++ trunk/src/main/resources/defaultHeader.vm 2009-08-28 10:14:10 UTC (rev 1632) @@ -0,0 +1,4 @@ +$projectName +Copyright (C) $inceptionYear $organizationName + +$licenseContent Modified: trunk/src/site/rst/usage.rst =================================================================== --- trunk/src/site/rst/usage.rst 2009-08-26 20:26:44 UTC (rev 1631) +++ trunk/src/site/rst/usage.rst 2009-08-28 10:14:10 UTC (rev 1632) @@ -1,4 +1,104 @@ Usage ----- -To be done soon... \ No newline at end of file +Update-header goal +================== + +This goal adds or updates the license header on some files. + +How does it work ? +~~~~~~~~~~~~~~~~~~ + +The plugin is looking in the included files for the text between the tags : + +:: + + *##% My LICENSE ##%* + +If the pattern is not found, it will add a license header, otherwise it will +update the content of the text between tags (here *MY LICENSE*). + +Note : to detect the license header we use the nuiton-processor library. + +Update java files +~~~~~~~~~~~~~~~~~ + +The usual files to use such license header are java sources, that's why it is +configure to do this task and should ask you the minimum config + +:: + + <plugin> + <groupId>org.nuiton</groupId> + <artifactId>maven-license-plugin</artifactId> + <version>XXX</version> + <configuration> + <licenseName>lgpl_v3</licenseName> + </configuration> + </plugin> + +will add or update for all java source files of the module (and java test files). + +The command can be launch on the commandline : + +:: + + mvn license:update-header -Dlicense.licenseName=lgpl_v3 + +It is possible to use it with other files (see next section) + +The generatorName property +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The *generatorName** property allows you to change the type of header to add or +update. At the moment there is two king of generator : + + * *java* generator, which box the license header in a java comment style + * *xml* generator, which box the license header in a xml comment style + +Example of java license header : + +:: + + /** + * *##% Plugin maven de changement de license + * Copyright (C) 2008 - 2009 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>. ##%* + */ + +Example of xml license header : + +:: + + <!-- + * *##% Plugin maven de changement de license + * Copyright (C) 2008 - 2009 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>. ##%* + --> + Modified: trunk/src/site/site.xml =================================================================== --- trunk/src/site/site.xml 2009-08-26 20:26:44 UTC (rev 1631) +++ trunk/src/site/site.xml 2009-08-28 10:14:10 UTC (rev 1632) @@ -22,9 +22,7 @@ <menu name="Utilisateur"> <item name="Introduction" href="index.html"/> <item name="Goals" href="plugin-info.html"> - <item name="java-update-header" href="java-update-header-mojo.html"/> - <item name="jaxx-update-header" href="jaxx-update-header-mojo.html"/> - <item name="switch" href="switch-mojo.html"/> + <item name="update-header" href="update-header-mojo.html"/> <item name="help" href="help-mojo.html"/> </item> </menu> Modified: trunk/src/test/java/org/nuiton/license/header/UpdateHeaderPluginTest.java =================================================================== --- trunk/src/test/java/org/nuiton/license/header/UpdateHeaderPluginTest.java 2009-08-26 20:26:44 UTC (rev 1631) +++ trunk/src/test/java/org/nuiton/license/header/UpdateHeaderPluginTest.java 2009-08-28 10:14:10 UTC (rev 1632) @@ -59,6 +59,8 @@ assertNotNull(mojo); UpdateHeaderPlugin p = (UpdateHeaderPlugin) mojo; + p.projectName = "projectName"; + p.organizationName = "organizationName"; p.execute(); for (Entry<File, String[]> entry : p.filesToTreate.entrySet()) { Modified: trunk/src/test/resources/org/nuiton/license/header/UpdateHeaderPluginTest/bug_28.xml =================================================================== --- trunk/src/test/resources/org/nuiton/license/header/UpdateHeaderPluginTest/bug_28.xml 2009-08-26 20:26:44 UTC (rev 1631) +++ trunk/src/test/resources/org/nuiton/license/header/UpdateHeaderPluginTest/bug_28.xml 2009-08-28 10:14:10 UTC (rev 1632) @@ -20,6 +20,7 @@ <licenseName>lgpl_v3</licenseName> <generatorName>java</generatorName> <includes>**/*.java2</includes> + <template>/defaultHeader.vm</template> <compileSourceRoots> <dir>target/test-classes/org/nuiton/license/header/UpdateHeaderPluginTest/bug_28</dir> </compileSourceRoots>
participants (1)
-
tchemit@users.nuiton.org