PomUtil { public static Node createPlugin(Document document, String groupId, String artifactId, String version) { Preconditions.checkArgument(hasChildNode(document, NODE_NAME_PROJECT), "The document doesn't seem to be a POM model, project element is missing."); Node plugins = getOrCreatePluginsNode(document, true); Node existingPlugin = getPlugin(document, groupId, artifactId); if (existingPlugin != null) { if (!hasChildNode(existingPlugin, NODE_NAME_VERSION)) { Element ver = document.createElement(NODE_NAME_VERSION); ver.setTextContent(version); existingPlugin.appendChild(ver); } return existingPlugin; } else { Element plugin = document.createElement(NODE_NAME_PLUGIN); plugins.appendChild(plugin); Element gid = document.createElement(NODE_NAME_GROUP_ID); gid.setTextContent(groupId); plugin.appendChild(gid); Element aid = document.createElement(NODE_NAME_ARTIFACT_ID); aid.setTextContent(artifactId); plugin.appendChild(aid); Element ver = document.createElement(NODE_NAME_VERSION); ver.setTextContent(version); plugin.appendChild(ver); return plugin; } } 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 testCreatePlugin() throws Exception { String pluginsNodePath = "project/build/plugins"; String gid = "org.apache.maven.plugins"; String aid = "maven-compiler-plugin"; String version = "3.5.1"; URL url = getClass().getResource(getClass().getSimpleName() + "/pom1.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); Assert.assertNull(getNode(document, pluginsNodePath)); Node plugin = PomUtil.createPlugin(document, gid, aid, version); Assert.assertNotNull(plugin); Node plugins = getNode(document, pluginsNodePath); Node p = null; for (int i = 0; i < plugins.getChildNodes().getLength(); i++) { Node child = plugins.getChildNodes().item(i); if (Objects.equal(PomUtil.NODE_NAME_PLUGIN, child.getNodeName())) { if (Objects.equal(gid, getNode(child, PomUtil.NODE_NAME_GROUP_ID).getTextContent()) && Objects.equal(aid, getNode(child, PomUtil.NODE_NAME_ARTIFACT_ID).getTextContent())) { p = child; break; } } } Assert.assertNotNull(p); Assert.assertEquals(gid, getNode(p, PomUtil.NODE_NAME_GROUP_ID).getTextContent()); Assert.assertEquals(aid, getNode(p, PomUtil.NODE_NAME_ARTIFACT_ID).getTextContent()); Assert.assertEquals(version, getNode(p, PomUtil.NODE_NAME_VERSION).getTextContent()); url = getClass().getResource(getClass().getSimpleName() + "/pom4.xml"); try { source = new File(url.toURI()); } catch (URISyntaxException e) { source = new File(url.getPath()); } builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); document = builder.parse(source); plugins = getNode(document, pluginsNodePath); Assert.assertNotNull(plugins); plugin = null; for (int i = 0; i < plugins.getChildNodes().getLength(); i++) { Node child = plugins.getChildNodes().item(i); if (Objects.equal(PomUtil.NODE_NAME_PLUGIN, child.getNodeName())) { if (Objects.equal(gid, getNode(child, PomUtil.NODE_NAME_GROUP_ID).getTextContent()) && Objects.equal(aid, getNode(child, PomUtil.NODE_NAME_ARTIFACT_ID).getTextContent())) { plugin = child; break; } } } Assert.assertNotNull(plugin); Assert.assertNull(getNode(plugin, PomUtil.NODE_NAME_VERSION)); int numPlugins = plugins.getChildNodes().getLength(); Node pluginFromUtil = PomUtil.createPlugin(document, gid, aid, version); Assert.assertNotNull(pluginFromUtil); Assert.assertEquals(gid, getNode(pluginFromUtil, PomUtil.NODE_NAME_GROUP_ID).getTextContent()); Assert.assertEquals(aid, getNode(pluginFromUtil, PomUtil.NODE_NAME_ARTIFACT_ID).getTextContent()); Assert.assertEquals(version, getNode(pluginFromUtil, PomUtil.NODE_NAME_VERSION).getTextContent()); plugins = getNode(document, pluginsNodePath); Assert.assertEquals(numPlugins, plugins.getChildNodes().getLength()); }