|
| 1 | +import org.apache.commons.lang.StringUtils |
| 2 | + |
| 3 | +import javax.xml.parsers.DocumentBuilderFactory |
| 4 | +import javax.xml.xpath.XPathFactory |
| 5 | + |
| 6 | +class Checker |
| 7 | +{ |
| 8 | + def result = true; |
| 9 | + |
| 10 | + def basedir; |
| 11 | + |
| 12 | + public Checker(File basedir) { |
| 13 | + this.basedir = basedir; |
| 14 | + } |
| 15 | + |
| 16 | + def readXPath( String pom, String xPathExpression ) |
| 17 | + { |
| 18 | + def stream = new FileInputStream( new File( basedir, pom ) ); |
| 19 | + try |
| 20 | + { |
| 21 | + return XPathFactory.newInstance() |
| 22 | + .newXPath() |
| 23 | + .evaluate( xPathExpression, DocumentBuilderFactory.newInstance() |
| 24 | + .newDocumentBuilder() |
| 25 | + .parse( stream ).documentElement ); |
| 26 | + } |
| 27 | + finally |
| 28 | + { |
| 29 | + stream.close(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + Checker check( String message, String pom, String xpath, String expected ) |
| 34 | + { |
| 35 | + if ( result ) |
| 36 | + { |
| 37 | + try |
| 38 | + { |
| 39 | + def actual = readXPath( pom, xpath ) |
| 40 | + if ( !StringUtils.equals( expected, actual ) ) |
| 41 | + { |
| 42 | + System.out.println( pom + " [xpath:" + xpath + "] expected '" + expected + "' found '" + actual + "' : " + message ); |
| 43 | + result = false; |
| 44 | + } |
| 45 | + } |
| 46 | + catch ( Throwable t ) |
| 47 | + { |
| 48 | + t.printStackTrace(); |
| 49 | + result = false; |
| 50 | + } |
| 51 | + } |
| 52 | + return this; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +return new Checker(basedir) |
| 57 | + .check( "root pom unchanged", "pom.xml", "/project/version", "1.2.0-SNAPSHOT" ) |
| 58 | + .check( "module-a1 changed", "module-a1/pom.xml", "/project/version", "2.5.0-SNAPSHOT" ) |
| 59 | + .check( "module-a1/module-b1 parent changed", "module-a1/module-b1/pom.xml", "/project/parent/version", "2.5.0-SNAPSHOT" ) |
| 60 | + .check( "module-a1/module-b1 remains unspecified", "module-a1/module-b1/pom.xml", "/project/version", "" ) |
| 61 | + .check( "module-a1/module-b2 parent changed", "module-a1/module-b2/pom.xml", "/project/parent/version", "2.5.0-SNAPSHOT" ) |
| 62 | + .check( "module-a1/module-b2 version changed", "module-a1/module-b2/pom.xml", "/project/version", "2.5.0-SNAPSHOT" ) |
| 63 | + .check( "module-a1/module-b3 parent changed", "module-a1/module-b3/pom.xml", "/project/parent/version", "2.5.0-SNAPSHOT" ) |
| 64 | + .check( "module-a1/module-b3 version changed", "module-a1/module-b3/pom.xml", "/project/version", "2.5.0-SNAPSHOT" ) |
| 65 | + .check( "module-a2 unchanged", "module-a2/pom.xml", "/project/version", "1.0.3-SNAPSHOT" ) |
| 66 | + .check( "module-a2 dependency unchanged", "module-a2/pom.xml", "/project/dependencies/dependency/version", "2.0.7-SNAPSHOT" ) |
| 67 | + .result; |
0 commit comments