Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Simple Maven plugin example

View pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
<?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>
View pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
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);
}
}
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.