PomUtil { public static Node createPluginExecution(Node plugin, String id, Optional<String> phase, String... goals) { Document document = plugin.getOwnerDocument(); Node executions = null; NodeList children = plugin.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (Objects.equal(NODE_NAME_EXECUTIONS, child.getNodeName())) { executions = child; break; } } if (executions == null) { executions = document.createElement(NODE_NAME_EXECUTIONS); plugin.appendChild(executions); } Element execution = document.createElement(NODE_NAME_EXECUTION); executions.appendChild(execution); Element idNode = document.createElement(NODE_NAME_ID); idNode.setTextContent(id); execution.appendChild(idNode); if (phase.isPresent()) { Element phaseNode = document.createElement(NODE_NAME_PHASE); phaseNode.setTextContent(phase.get()); execution.appendChild(phaseNode); } if (goals.length > 0) { Element goalsNode = document.createElement(NODE_NAME_GOALS); execution.appendChild(goalsNode); for (String goal : goals) { Element goalNode = document.createElement(NODE_NAME_GOAL); goalNode.setTextContent(goal); goalsNode.appendChild(goalNode); } } return execution; } 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 testCreatePluginExecution() throws Exception { URL url = getClass().getResource(getClass().getSimpleName() + "/pom4.xml"); File source; try { source = new File(url.toURI()); } catch (URISyntaxException e) { source = new File(url.getPath()); } DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(source); Node plugin = PomUtil.getPlugin(document, "org.apache.maven.plugins", "maven-javadoc-plugin"); if (plugin == null) { Assert.fail("Expected the plugin node to be present."); } PomUtil.createPluginExecution(plugin, "123", Optional.of("verify"), "test"); Node executions = getNode(plugin, PomUtil.NODE_NAME_EXECUTIONS); Assert.assertNotNull(executions); Node execution = executions.getFirstChild(); Assert.assertNotNull(execution); Node id = getNode(execution, PomUtil.NODE_NAME_ID); Assert.assertNotNull(id); Assert.assertEquals("123", id.getTextContent()); Node phase = getNode(execution, PomUtil.NODE_NAME_PHASE); Assert.assertNotNull(phase); Assert.assertEquals("verify", phase.getTextContent()); Node goals = getNode(execution, PomUtil.NODE_NAME_GOALS); Assert.assertNotNull(goals); Assert.assertEquals(1, goals.getChildNodes().getLength()); Node goal = goals.getFirstChild(); Assert.assertNotNull(goal); Assert.assertEquals("test", goal.getTextContent()); plugin = PomUtil.getPlugin(document, "x.y.z", "test"); if (plugin == null) { Assert.fail("Expected the plugin node to be present."); } PomUtil.createPluginExecution(plugin, "123", Optional.of("verify"), "test"); executions = getNode(plugin, PomUtil.NODE_NAME_EXECUTIONS); Assert.assertNotNull(executions); execution = executions.getLastChild(); Assert.assertNotNull(execution); id = getNode(execution, PomUtil.NODE_NAME_ID); Assert.assertNotNull(id); Assert.assertEquals("123", id.getTextContent()); phase = getNode(execution, PomUtil.NODE_NAME_PHASE); Assert.assertNotNull(phase); Assert.assertEquals("verify", phase.getTextContent()); goals = getNode(execution, PomUtil.NODE_NAME_GOALS); Assert.assertNotNull(goals); Assert.assertEquals(1, goals.getChildNodes().getLength()); goal = goals.getFirstChild(); Assert.assertNotNull(goal); Assert.assertEquals("test", goal.getTextContent()); PomUtil.createPluginExecution(plugin, "123", Optional.<String> absent(), "test"); executions = getNode(plugin, PomUtil.NODE_NAME_EXECUTIONS); Assert.assertNotNull(executions); execution = executions.getLastChild(); Assert.assertNotNull(execution); id = getNode(execution, PomUtil.NODE_NAME_ID); Assert.assertNotNull(id); Assert.assertEquals("123", id.getTextContent()); phase = getNode(execution, PomUtil.NODE_NAME_PHASE); Assert.assertNull(phase); goals = getNode(execution, PomUtil.NODE_NAME_GOALS); Assert.assertNotNull(goals); Assert.assertEquals(1, goals.getChildNodes().getLength()); goal = goals.getFirstChild(); Assert.assertNotNull(goal); Assert.assertEquals("test", goal.getTextContent()); PomUtil.createPluginExecution(plugin, "123", Optional.of("verify")); executions = getNode(plugin, PomUtil.NODE_NAME_EXECUTIONS); Assert.assertNotNull(executions); execution = executions.getLastChild(); Assert.assertNotNull(execution); id = getNode(execution, PomUtil.NODE_NAME_ID); Assert.assertNotNull(id); Assert.assertEquals("123", id.getTextContent()); phase = getNode(execution, PomUtil.NODE_NAME_PHASE); Assert.assertNotNull(phase); Assert.assertEquals("verify", phase.getTextContent()); Assert.assertNull(getNode(execution, PomUtil.NODE_NAME_GOALS)); }