Stephen, The order of the project-elements differs from the maven-convention[1]. Any reason? -Robert [1] http://maven.apache.org/developers/conventions/code.html
From: [email protected] To: [email protected] Date: Tue, 17 May 2011 10:51:49 -0500 Subject: [mojo-scm] [14065] trunk/sandbox/tidy-maven-plugin: initial commit Revision 14065 Author stephenconnolly Date 2011-05-17 10:51:48 -0500 (Tue, 17 May 2011) Log Messageinitial commit Added Paths trunk/sandbox/tidy-maven-plugin/pom.xml trunk/sandbox/tidy-maven-plugin/src/ trunk/sandbox/tidy-maven-plugin/src/main/ trunk/sandbox/tidy-maven-plugin/src/main/java/ trunk/sandbox/tidy-maven-plugin/src/main/java/org/ trunk/sandbox/tidy-maven-plugin/src/main/java/org/codehaus/ trunk/sandbox/tidy-maven-plugin/src/main/java/org/codehaus/mojo/ trunk/sandbox/tidy-maven-plugin/src/main/java/org/codehaus/mojo/tidy/ trunk/sandbox/tidy-maven-plugin/src/main/java/org/codehaus/mojo/tidy/PomMojo.java Property Changed trunk/sandbox/tidy-maven-plugin/ Diff Property changes: trunk/sandbox/tidy-maven-pluginName: svn:ignore + *.ipr *.iml *.iws target .project .classpath .settings Added: trunk/sandbox/tidy-maven-plugin/pom.xml (0 => 14065)--- trunk/sandbox/tidy-maven-plugin/pom.xml (rev 0) +++ trunk/sandbox/tidy-maven-plugin/pom.xml 2011-05-17 15:51:48 UTC (rev 14065) @@ -0,0 +1,208 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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> + + <parent> + <groupId>org.codehaus.mojo</groupId> + <artifactId>mojo-sandbox-parent</artifactId> + <version>9</version> + <relativePath/> + </parent> + <groupId>org.codehaus.mojo</groupId> + <artifactId>tidy-maven-plugin</artifactId> + <version>1.0-alpha-1-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-repository-metadata</artifactId> + <version>2.0.6</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <version>2.0.6</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-model</artifactId> + <version>2.0.6</version> + </dependency> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-common-artifact-filters</artifactId> + <version>1.2</version> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + <version>1.5.9</version> + </dependency> + <dependency> + <groupId>backport-util-concurrent</groupId> + <artifactId>backport-util-concurrent</artifactId> + <version>3.1</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <version>2.0.9</version> + </dependency> + <dependency> + <groupId>org.codehaus.woodstox</groupId> + <artifactId>wstx-asl</artifactId> + <version>3.2.7</version> + </dependency> + <dependency> + <groupId>stax</groupId> + <artifactId>stax-api</artifactId> + <version>1.0.1</version> + </dependency> + </dependencies> + + <name>Tidy Maven Plugin</name> + <description> + Tidy Plugin for Maven. The Tidy plugin provides goals for tidying up + your source code. + </description> + <inceptionYear>2011</inceptionYear> + + <developers> + <developer> + <name>Stephen Connolly</name> + <email>[email protected]</email> + <roles> + <role>Lead Developer</role> + </roles> + <timezone>0</timezone> + </developer> + </developers> + + <licenses> + <license> + <name>The Apache Software License, Version 2.0</name> + <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> + <distribution>repo</distribution> + </license> + </licenses> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-enforcer-plugin</artifactId> + <version>1.0</version> + <executions> + <execution> + <id>enforce-java</id> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <requireJavaVersion> + <version>1.4.2</version> + </requireJavaVersion> + </rules> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.4</source> + <target>1.4</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-invoker-plugin</artifactId> + <version>1.5</version> + <configuration> + <projectsDirectory>src/it</projectsDirectory> + <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo> + <showErrors>true</showErrors> + <streamLogs>true</streamLogs> + <settingsFile>src/it/settings.xml</settingsFile> + <pomIncludes> + <pomInclude>*/pom.xml</pomInclude> + </pomIncludes> + <postBuildHookScript>verify.bsh</postBuildHookScript> + </configuration> + </plugin> + </plugins> + </build> + + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>2.4.3</version> + <configuration> + <requirements> + <maven>2.0.6</maven> + <jdk>1.4.2</jdk> + <memory>256m</memory> + </requirements> + </configuration> + </plugin> + </plugins> + </reporting> + + <issueManagement> + <system>jira</system> + <url>http://jira.codehaus.org/browse/MOJO</url> + </issueManagement> + + <scm> + <connection>scm:svn:https://svn.codehaus.org/mojo/trunk/sandbox/tidy-maven-plugin</connection> + <developerConnection>scm:svn:https://svn.codehaus.org/mojo/trunk/sandbox/tidy-maven-plugin</developerConnection> + <url>http://fisheye.codehaus.org/browse/mojo/trunk/sandbox/tidy-maven-plugin</url> + </scm> + + <prerequisites> + <maven>2.0.6</maven> + </prerequisites> + + <profiles> + <profile> + <!-- run integration tests + to use this profile: + or run integration tests: mvn -Prun-its + --> + <id>run-its</id> + <build> + <defaultGoal>verify</defaultGoal> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-invoker-plugin</artifactId> + <executions> + <execution> + <id>integration-test</id> + <goals> + <goal>install</goal> + <goal>run</goal> + </goals> + <configuration> + <streamLogs>true</streamLogs> + <debug>false</debug> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project> Added: trunk/sandbox/tidy-maven-plugin/src/main/java/org/codehaus/mojo/tidy/PomMojo.java (0 => 14065)--- trunk/sandbox/tidy-maven-plugin/src/main/java/org/codehaus/mojo/tidy/PomMojo.java (rev 0) +++ trunk/sandbox/tidy-maven-plugin/src/main/java/org/codehaus/mojo/tidy/PomMojo.java 2011-05-17 15:51:48 UTC (rev 14065) @@ -0,0 +1,196 @@ +package org.codehaus.mojo.tidy; + +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.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.WriterFactory; +import org.codehaus.stax2.XMLInputFactory2; + +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.XMLEvent; +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.io.Writer; +import java.util.Stack; +import java.util.regex.Pattern; + +/** + * Tidy up the <code>pom.xml</code> into the canonical order. + * + * @goal pom + */ +public class PomMojo extends AbstractMojo { + + /** + * The Maven Project. + * + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + private static final String[][] sequence = { + {"modelVersion", ""}, + {"parent", "\n"}, + {"groupId", ""}, + {"artifactId", ""}, + {"version", ""}, + {"packaging", ""}, + {"modules", ""}, + {"properties", ""}, + {"dependencyManagement", "\n"}, + {"dependencies", ""}, + {"name", "\n"}, + {"description", ""}, + {"url", ""}, + {"inceptionYear", ""}, + {"developers", "\n"}, + {"contributors", ""}, + {"licenses", "\n"}, + {"organization", "\n"}, + {"build", "\n"}, + {"reporting", "\n"}, + {"issueManagement", "\n"}, + {"ciManagement", "\n"}, + {"mailingLists", "\n"}, + {"scm", "\n"}, + {"prerequisites", "\n"}, + {"repositories", "\n"}, + {"pluginRepositories", "\n"}, + {"distributionManagement", "\n"}, + {"profiles", "\n"}, + }; + + public void execute() throws MojoExecutionException, MojoFailureException { + try { + XMLInputFactory inputFactory = XMLInputFactory2.newInstance(); + inputFactory.setProperty(XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE); + + StringBuffer input = readXmlFile(project.getFile()); + String inputStr = input.toString(); + int first = Integer.MAX_VALUE, last = Integer.MIN_VALUE; + int[] starts = new int[sequence.length]; + int[] ends = new int[sequence.length]; + for (int i = 0; i < sequence.length; i++) { + Pattern matchScopeRegex = Pattern.compile("/project"); + Pattern matchTargetRegex = Pattern.compile("/project/\\Q" + sequence[i][0] + "\\E"); + + Stack stack = new Stack(); + String path = ""; + boolean inMatchScope = false; + int start = -1; + starts[i] = ends[i] = -1; + + XMLEventReader pom = inputFactory.createXMLEventReader(new StringReader(inputStr)); + + while (pom.hasNext()) { + XMLEvent event = pom.nextEvent(); + if (event.isStartElement()) { + stack.push(path); + final String elementName = event.asStartElement().getName().getLocalPart(); + path = new StringBuffer().append(path).append("/").append(elementName).toString(); + + if (matchScopeRegex.matcher(path).matches()) { + // we're in a new match scope + // reset any previous partial matches + inMatchScope = true; + start = -1; + } else if (inMatchScope && matchTargetRegex.matcher(path).matches()) { + start = event.getLocation().getCharacterOffset(); + } + } + if (event.isEndElement()) { + if (matchTargetRegex.matcher(path).matches() && start != -1) { + starts[i] = start; + ends[i] = pom.peek().getLocation().getCharacterOffset(); + first = Math.min(first, starts[i]); + last = Math.max(last, ends[i]); + break; + } else if (matchScopeRegex.matcher(path).matches()) { + inMatchScope = false; + start = -1; + } + path = (String) stack.pop(); + } + } + + } + input = new StringBuffer(input.length() + 1024); + input.append(inputStr.substring(0, first).trim()); + String lastSep = null; + for (int i = 0; i < sequence.length; i++) { + if (lastSep == null || !StringUtils.isWhitespace(sequence[i][1]) || lastSep.length() < sequence[i][1].length()) { + input.append(lastSep = sequence[i][1]); + } + if (starts[i] != -1) { + int l = -1; + for (int k = 0; k < sequence.length; k++) { + if (ends[k] != -1 && (l == -1 || ends[l] < ends[k]) && ends[k] < starts[i]) { + l = k; + } + } + if (l != -1) { + input.append(inputStr.substring(ends[l], starts[i]).trim()); + lastSep = null; + } + input.append("\n "); + input.append(inputStr.substring(starts[i], ends[i]).trim()); + } + } + input.append(inputStr.substring(last)); + + writeFile( project.getFile(), input ); + } catch (IOException e) { + getLog().error(e); + } catch (XMLStreamException e) { + getLog().error(e); + } + } + + + /** + * Reads a file into a String. + * + * @param outFile The file to read. + * @return String The content of the file. + * @throws java.io.IOException when things go wrong. + */ + public static StringBuffer readXmlFile(File outFile) + throws IOException { + Reader reader = ReaderFactory.newXmlReader(outFile); + + try { + return new StringBuffer(IOUtil.toString(reader)); + } finally { + IOUtil.close(reader); + } + } + + /** + * Writes a StringBuffer into a file. + * + * @param outFile The file to read. + * @param input The contents of the file. + * @throws IOException when things go wrong. + */ + protected final void writeFile(File outFile, StringBuffer input) + throws IOException { + Writer writer = WriterFactory.newXmlWriter(outFile); + try { + IOUtil.copy(input.toString(), writer); + } finally { + IOUtil.close(writer); + } + } + + +} To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email
