PomUtil { public static final Optional<Document> parsePOM(MavenProject project) { try { return parsePOM(project.getFile()); } catch (RuntimeException e) { throw new RuntimeException( "Could not load the project object model of the following module: " + ProjectToString.INSTANCE.apply(project), e); } } private  PomUtil(); static final Optional<Document> parsePOM(MavenProject project); static final Optional<Document> parsePOM(File pomFile); static final Document parsePOM(InputStream in); static final void writePOM(Document document, MavenProject project); static final void writePOM(Document document, OutputStream out, boolean closeOut); static void setProjectVersion(Model model, Document document, String newVersion); static void setParentVersion(Model model, Document document, String newParentVersion); static void setDependencyVersion(Dependency dependency, Document document, String dependenciesPath,
      String newVersion); static Node getOrCreateBuildNode(Document document, boolean createOnDemand); static Node getOrCreatePluginsNode(Document document, boolean createOnDemand); static Node getPlugin(Document document, String groupId, String artifactId); static Node createPlugin(Document document, String groupId, String artifactId, String version); static Node createPluginExecution(Node plugin, String id, Optional<String> phase, String... goals); static Node getOrCreateScmNode(Document document, boolean createOnDemand); static void setNodeTextContent(Node parentNode, String nodeName, String content, boolean createOnDemand); static void deleteNode(Node parentNode, String nodeName); static boolean hasChildNode(Node parentNode, String nodeName); static Optional<String> getChildNodeTextContent(Node parentNode, String nodeName); static final String ARTIFACT_TYPE_JAR; static final String ARTIFACT_TYPE_POM; static final String VERSION_QUALIFIER_SNAPSHOT; static final String VERSION_LATEST; static final String NODE_NAME_ARTIFACT_ID; static final String NODE_NAME_BUILD; static final String NODE_NAME_EXECUTION; static final String NODE_NAME_EXECUTIONS; static final String NODE_NAME_GOAL; static final String NODE_NAME_GOALS; static final String NODE_NAME_GROUP_ID; static final String NODE_NAME_ID; static final String NODE_NAME_PARENT; static final String NODE_NAME_PHASE; static final String NODE_NAME_PLUGIN; static final String NODE_NAME_PLUGINS; static final String NODE_NAME_PROJECT; static final String NODE_NAME_SCM; static final String NODE_NAME_SCM_CONNECTION; static final String NODE_NAME_SCM_DEV_CONNECTION; static final String NODE_NAME_SCM_TAG; static final String NODE_NAME_SCM_URL; static final String NODE_NAME_VERSION; static final String NODE_NAME_DEPENDENCY; static final String NODE_NAME_DEPENDENCIES; static final String NODE_NAME_DEPENDENCY_MANAGEMENT; }
@Test public void testParsePOM() { URL url = getClass().getResource(getClass().getSimpleName() + "/pom1.xml"); File f; try { f = new File(url.toURI()); } catch (URISyntaxException e) { f = new File(url.getPath()); } Optional<Document> parsedPOM = PomUtil.parsePOM(f); Assert.assertTrue(parsedPOM.isPresent()); Document document = parsedPOM.get(); Assert.assertNotNull(document); Element root = document.getDocumentElement(); Assert.assertEquals("project", root.getTagName()); Node gid = getNode(root, PomUtil.NODE_NAME_GROUP_ID); Assert.assertNotNull(gid); Assert.assertEquals("com.itemis.maven.plugins", gid.getTextContent()); Assert.assertEquals("test", document.getElementsByTagName("finalName").item(0).getTextContent()); FileInputStream is; try { is = new FileInputStream(f); document = PomUtil.parsePOM(is); Assert.assertNotNull(document); root = document.getDocumentElement(); Assert.assertEquals("project", root.getTagName()); gid = getNode(root, PomUtil.NODE_NAME_GROUP_ID); Assert.assertNotNull(gid); Assert.assertEquals("com.itemis.maven.plugins", gid.getTextContent()); Assert.assertEquals("test", document.getElementsByTagName("finalName").item(0).getTextContent()); } catch (IOException e) { Assert.fail("Could not open Stream from POM file"); } MavenProject project = new MavenProject(); project.setFile(f); parsedPOM = PomUtil.parsePOM(project); Assert.assertTrue(parsedPOM.isPresent()); document = parsedPOM.get(); Assert.assertNotNull(document); root = document.getDocumentElement(); Assert.assertEquals("project", root.getTagName()); gid = getNode(root, PomUtil.NODE_NAME_GROUP_ID); Assert.assertNotNull(gid); Assert.assertEquals("com.itemis.maven.plugins", gid.getTextContent()); Assert.assertEquals("test", document.getElementsByTagName("finalName").item(0).getTextContent()); }
@Test public void testParsePOM_Invalid() { this.exception.expect(RuntimeException.class); this.exception.expectCause(new BaseMatcher<Throwable>() { @Override public boolean matches(Object item) { return item instanceof SAXParseException; } @Override public void describeTo(Description description) { } }); URL url = getClass().getResource(getClass().getSimpleName() + "/pom2.xml"); File f; try { f = new File(url.toURI()); } catch (URISyntaxException e) { f = new File(url.getPath()); } PomUtil.parsePOM(f); Assert.fail("Parser should throw an exception since the document is not well formed!"); }