Skip to content

@sfussenegger /pom.xml
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simple Maven plugin example
package at.molindo.maven.plugin.beautifyeclipse;
import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
* @goal beautify
*/
public class BeautifyEclipsePlugin extends AbstractMojo {
public static class ClassPathEntry implements Comparable<ClassPathEntry> {
private enum Kind {
src, lib, output, con, var;
}
private final Kind _kind;
private final String _name;
private final Node _node;
ClassPathEntry(final String kind, final String path, final Node node) {
if (kind == null || path == null || node == null) {
throw new NullPointerException("all arguments must be != null");
}
_kind = Kind.valueOf(kind);
if (_kind == null) {
throw new IllegalArgumentException("unknown kind: " + kind);
}
_name = path.substring(path.lastIndexOf("/") + 1);
_node = node;
}
public int compareTo(final ClassPathEntry o) {
if (o == null) {
return -1;
}
final int val = _kind.compareTo(o._kind);
if (val != 0) {
return val;
}
return _name.compareToIgnoreCase(o._name);
}
@Override
public boolean equals(final Object obj) {
if (obj instanceof ClassPathEntry) {
return compareTo((ClassPathEntry) obj) == 0;
}
return false;
}
@Override
public int hashCode() {
// better weak than wrong
return _name.hashCode();
}
@Override
public String toString() {
return "kind=" + _kind + ", name=" + _name;
}
public Node getNode() {
return _node;
}
}
/**
* The project whose project files to create.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
protected MavenProject project;
// /**
// * The currently executed project (can be a reactor project).
// *
// * @parameter expression="${executedProject}"
// * @readonly
// */
// protected MavenProject executedProject;
@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("hey ho, let's go: " + project.getName());
final File directory = project.getBasedir();
beautify(new File(directory, ".classpath"));
}
void beautify(final File classpathFile) {
if (!classpathFile.exists()) {
getLog().info(".classpath file does not exist: " + classpathFile.getAbsolutePath());
return;
}
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document dotClasspath = builder.parse(classpathFile);
final NodeList entries = dotClasspath.getElementsByTagName("classpathentry");
getLog().debug("found " + entries.getLength() + " classpath entries");
final LinkedList<ClassPathEntry> list = new LinkedList<ClassPathEntry>();
// read entries
while (entries.getLength() > 0) {
Node node = entries.item(0);
node = node.getParentNode().removeChild(node);
final Node kindAttr = node.getAttributes().getNamedItem("kind");
final Node pathAttr = node.getAttributes().getNamedItem("path");
final String kind = kindAttr.getNodeValue();
final String path = pathAttr.getNodeValue();
getLog().debug("kind=" + kind + ", path=" + path);
final ClassPathEntry entry = new ClassPathEntry(kind, path, node);
list.add(entry);
}
Collections.sort(list);
getLog().debug(list.toString());
//
// final Document out = builder.newDocument();
// final Node root = out.createElement("classpath");
// out.appendChild(root);
final Node root = dotClasspath.getFirstChild();
// remove empty nodes
int i = 0;
final NodeList nodes = root.getChildNodes();
while (i < nodes.getLength()) {
final Node n = nodes.item(i);
if (n instanceof Text) {
final Text t = (Text) n;
final String val = t.getNodeValue().trim();
if ("".equals(val)) {
root.removeChild(n);
continue;
}
}
// keep
i++;
}
for (final ClassPathEntry e : list) {
root.appendChild(e.getNode());
}
writeXmlFile(dotClasspath, classpathFile);
} catch (final Exception e) {
getLog().warn(e);
}
}
public void writeXmlFile(final Document doc, final File file) throws Exception {
// Prepare the DOM document for writing
final Source source = new DOMSource(doc);
final Result result = new StreamResult(file);
// Write the DOM document to the file
final Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
xformer.transform(source, result);
}
}
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-beautifyeclipse-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0</version>
<name>
Beautify the Eclipse .project file of a multimodule project
</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.