Created
October 1, 2013 16:16
-
-
Save pforhan/6781037 to your computer and use it in GitHub Desktop.
Selectively build maven modules based on the changes in the git workspace.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git status --porcelain | ExtractMavenModules.java | xargs mvn clean install -pl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*bin/echo Running $0 > /dev/null | |
filename=`basename "$0"` | |
mytmpdir=/tmp/$filename | |
classname=${filename%.*} | |
if [ $0 -nt $mytmpdir/$classname.class ] | |
then | |
echo Compiling $0 > /dev/null | |
mkdir $mytmpdir 2> /dev/null | |
javac -d $mytmpdir $0 || exit | |
fi | |
java -cp $mytmpdir $classname "$@" | |
exit | |
*/ | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.InputStreamReader; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Unmarshaller; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
public class ExtractMavenModules { | |
public static void main(String[] args) throws Exception { | |
if (args.length != 0 || System.console() != null) { | |
System.out.println(); | |
System.out.println("Description: reads filenames from stdin, writes the pom module names to"); | |
System.out | |
.println(" stdout in a format compatable with mvn's -pl flag, :module1,:module2..."); | |
System.out.println("Suppoorts git short / porcelain format"); | |
System.out.println(); | |
System.out.println("Error: Arguments specified, or stdin not redirected"); | |
System.exit(1); | |
} | |
// Read from stdin line-by-line. | |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); | |
while (true) { | |
String line = in.readLine(); | |
if (line == null) break; | |
String[] filenames = getFileNames(line); | |
for (String filename : filenames) { | |
File file = new File(filename); | |
File pomFile = findPom(file); | |
if (pomFile != null) { | |
String moduleName = readArtifactId(pomFile); | |
System.out.print(":" + moduleName + ","); | |
} | |
} | |
} | |
} | |
private static String readArtifactId(File pomFile) throws JAXBException { | |
// JAXB trivia: | |
JAXBContext jc = JAXBContext.newInstance(Pom.class); | |
Unmarshaller ju = jc.createUnmarshaller(); | |
Pom pom = (Pom) ju.unmarshal(pomFile); | |
return pom.artifactId; | |
} | |
/** Given a file, finds the nearest pom.xml file. */ | |
private static File findPom(File file) { | |
while (file != null) { | |
File parent = file.getParentFile(); | |
File maybePom = new File(parent, "pom.xml"); | |
if (maybePom.canRead()) return maybePom; | |
file = parent; | |
} | |
return null; | |
} | |
/** | |
* Parses lines in one of these formats. Leading, trailing spaces are ignored. Status code may be | |
* multiple characters but is ignored. | |
* | |
* <pre> | |
* filename | |
* c filename (status code, space, filename) | |
* c filename -> newfilename (status code, space, filename, renamed filename) | |
* </pre> | |
*/ | |
private static String[] getFileNames(String line) { | |
String[] split = line.trim().split(" "); | |
switch (split.length) { | |
case 1: // Just a filename. | |
return new String[] { split[0] }; | |
case 2: // status-filename | |
return new String[] { split[1] }; | |
case 4: // status-filename-renamed | |
return new String[] { split[1], split[3] }; | |
case 5: // status-space-filename-renamed | |
return new String[] { split[2], split[4] }; | |
default: | |
throw new IllegalArgumentException("Could not parse line: '" + line + "'"); | |
} | |
} | |
@XmlRootElement(name = "project", namespace = "http://maven.apache.org/POM/4.0.0") | |
public static class Pom { | |
@XmlElement(name = "artifactId", namespace = "http://maven.apache.org/POM/4.0.0") | |
public String artifactId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment