Author: [email protected] Date: Tue Apr 12 11:46:45 2011 New Revision: 959
Log: [sandbox] simple mojo that updates classheaders on java files Added: sandbox/bdekruijff/classheader-maven-plugin/ sandbox/bdekruijff/classheader-maven-plugin/README.txt sandbox/bdekruijff/classheader-maven-plugin/classheader.txt sandbox/bdekruijff/classheader-maven-plugin/pom.xml sandbox/bdekruijff/classheader-maven-plugin/src/ sandbox/bdekruijff/classheader-maven-plugin/src/main/ sandbox/bdekruijff/classheader-maven-plugin/src/main/java/ sandbox/bdekruijff/classheader-maven-plugin/src/main/java/org/ sandbox/bdekruijff/classheader-maven-plugin/src/main/java/org/amdatu/ sandbox/bdekruijff/classheader-maven-plugin/src/main/java/org/amdatu/tools/ sandbox/bdekruijff/classheader-maven-plugin/src/main/java/org/amdatu/tools/mojo/ sandbox/bdekruijff/classheader-maven-plugin/src/main/java/org/amdatu/tools/mojo/ClassHeaderMojo.java Added: sandbox/bdekruijff/classheader-maven-plugin/README.txt ============================================================================== --- (empty file) +++ sandbox/bdekruijff/classheader-maven-plugin/README.txt Tue Apr 12 11:46:45 2011 @@ -0,0 +1,23 @@ +About: + +Simple Mojo that adds/updates classheaders to *.java in sourceDirectory/testSourceDirectory + +Usage: + + <plugin> + <groupId>org.amdatu.tools.mojo</groupId> + <artifactId>classheader-maven-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <executions> + <execution> + <id>update-classheaders</id> + <phase>validate</phase> + <goals> + <goal>classheaders</goal> + </goals> + </execution> + </executions> + <configuration> + <headerFile>..\..\classheader.txt</headerFile> + </configuration> + </plugin> Added: sandbox/bdekruijff/classheader-maven-plugin/classheader.txt ============================================================================== --- (empty file) +++ sandbox/bdekruijff/classheader-maven-plugin/classheader.txt Tue Apr 12 11:46:45 2011 @@ -0,0 +1,16 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU 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 Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ Added: sandbox/bdekruijff/classheader-maven-plugin/pom.xml ============================================================================== --- (empty file) +++ sandbox/bdekruijff/classheader-maven-plugin/pom.xml Tue Apr 12 11:46:45 2011 @@ -0,0 +1,32 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.amdatu.tools.mojo</groupId> + <artifactId>classheader-maven-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + <name>Classheader Maven Plugin</name> + <description>Replaces or updates source headers for all files found in the specified directory</description> + <url>http://www.amdatu.org</url> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-io</artifactId> + <version>1.3.2</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.8.2</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> Added: sandbox/bdekruijff/classheader-maven-plugin/src/main/java/org/amdatu/tools/mojo/ClassHeaderMojo.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/classheader-maven-plugin/src/main/java/org/amdatu/tools/mojo/ClassHeaderMojo.java Tue Apr 12 11:46:45 2011 @@ -0,0 +1,127 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU 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 Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.tools.mojo; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.Iterator; + +import org.apache.commons.io.FileUtils; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Adds/updates sourceheaders from a file on all .java files found in the + * specified (test)sourceDirectories of project. + * + * @author <a href="http://www.amdatu.org">Amdatu</a> + * @goal classheaders + */ +public class ClassHeaderMojo extends AbstractMojo { + + /** + * @parameter expression="${project.build.sourceDirectory}" + */ + private File sourceDirectory; + + /** + * @parameter expression="${project.build.testSourceDirectory}" + */ + private File testSourceDirectory; + + /** + * @parameter expression="${classheaders.headerFile}" + * @required + */ + private File headerFile; + + /* + * (non-Javadoc) + * + * @see org.apache.maven.plugin.AbstractMojo#execute() + */ + public void execute() throws MojoExecutionException { + + if (!headerFile.exists() || !headerFile.isFile() || !headerFile.canRead()) { + throw new MojoExecutionException("Configured headerfile not accessible: " + headerFile.getAbsolutePath()); + } + + String headerString; + try { + headerString = FileUtils.readFileToString(headerFile, "UTF-8"); + } + catch (IOException e) { + throw new MojoExecutionException("Failed to read configured headerFile", e); + } + + if (sourceDirectory != null + && (!sourceDirectory.exists() || !sourceDirectory.isDirectory() || !sourceDirectory.canRead())) { + getLog() + .warn( + "Configured sourceDirectory not accessible... skipping: " + sourceDirectory.getAbsolutePath()); + } + else { + processDirectory(headerString, sourceDirectory); + } + + if (testSourceDirectory != null + && (!testSourceDirectory.exists() || !testSourceDirectory.isDirectory() || !testSourceDirectory + .canRead())) { + getLog().warn( + "Configured testSourceDirectory not accessible... skipping: " + + testSourceDirectory.getAbsolutePath()); + } + else { + processDirectory(headerString, testSourceDirectory); + } + } + + private void processDirectory(String headerString, File directory) throws MojoExecutionException { + + Collection sourceFiles = FileUtils.listFiles(directory, new String[] { "java" }, true); + Iterator sourceFilesIterator = sourceFiles.iterator(); + + getLog().info("Processing " + sourceFiles.size() + " sourcefiles in directory " + directory.getAbsolutePath()); + + while (sourceFilesIterator.hasNext()) { + File sourceFile = (File) sourceFilesIterator.next(); + String sourceString; + try { + sourceString = FileUtils.readFileToString(sourceFile, "UTF-8"); + // Note that the string "package" is split so that JXR 2.0 + // doesn't interpret it as a real package token. This bug was + // fixed in JXR in revision 516976 + // see: http://svn.apache.org/viewvc?view=rev&revision=516976 + int index = sourceString.indexOf("pac" + "kage "); + if (index == -1) { + index = sourceString.indexOf("im" + "port "); + } + if (index == -1) { + getLog().info("Couldn't locate package or import keyword in file: " + sourceFile.getAbsolutePath()); + } + else { + FileUtils.writeStringToFile(sourceFile, headerString + sourceString.substring(index), "UTF-8"); + } + } + catch (IOException e) { + throw new MojoExecutionException("Exception while processing sourceFile: " + + sourceFile.getAbsolutePath(), e); + } + } + } +} _______________________________________________ Amdatu-commits mailing list [email protected] http://lists.amdatu.org/mailman/listinfo/amdatu-commits
