PomUtil { public static void setDependencyVersion(Dependency dependency, Document document, String dependenciesPath, String newVersion) { Preconditions.checkArgument(hasChildNode(document, NODE_NAME_PROJECT), "The document doesn't seem to be a POM model, project element is missing."); dependency.setVersion(newVersion); NodeList dependencyNodes; try { List<String> xPathParts = new ArrayList<>(); xPathParts.add(NODE_NAME_PROJECT); if (dependenciesPath.startsWith("/")) { dependenciesPath = dependenciesPath.substring("/".length()); } if (!dependenciesPath.isEmpty()) { xPathParts.add(dependenciesPath); } xPathParts.add(NODE_NAME_DEPENDENCIES); xPathParts.add(NODE_NAME_DEPENDENCY + "[groupId[text()='" + dependency.getGroupId() + "']" + " and artifactId[text()='" + dependency.getArtifactId() + "']]"); String expression = "/" + Joiner.on("/").skipNulls().join(xPathParts); XPath xPath = XPathFactory.newInstance().newXPath(); dependencyNodes = (NodeList) xPath.evaluate(expression, document.getDocumentElement(), XPathConstants.NODESET); } catch (XPathExpressionException e) { String message = "Cannot evaluate xPath against '" + dependenciesPath + "'."; throw new RuntimeException(message, e); } for (int i = 0; i < dependencyNodes.getLength(); i++) { Node dependencyNode = dependencyNodes.item(i); NodeList childNodes = dependencyNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (Objects.equal(childNode.getNodeName(), PomUtil.NODE_NAME_VERSION)) { childNode.setTextContent(newVersion); } } } } 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 testSetDependencyVersion() throws Exception { URL url = getClass().getResource(getClass().getSimpleName() + "/pom5-reactor-dependencies.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); Model model = new Model(); model.setGroupId("com.itemis.maven.plugins"); model.setArtifactId("test-project-1"); model.setVersion("1"); String oldDependencyVersion = "0.0.1-SNAPSHOT"; String newDependencyVersion = "0.0.1"; { Dependency projectDependency = new Dependency(); projectDependency.setGroupId("com.cht.test"); projectDependency.setArtifactId("test-dep"); projectDependency.setVersion(oldDependencyVersion); model.addDependency(projectDependency); PomUtil.setDependencyVersion(projectDependency, document, "/", newDependencyVersion); Assert.assertEquals(newDependencyVersion, getNode(document.getDocumentElement(), "dependencies/dependency/version").getTextContent()); Assert.assertEquals(oldDependencyVersion, getNode(document.getDocumentElement(), "dependencyManagement/dependencies/dependency/version") .getTextContent()); Assert.assertEquals(oldDependencyVersion, getNode(document.getDocumentElement(), "profiles/profile/dependencies/dependency/version").getTextContent()); Assert.assertEquals(oldDependencyVersion, getNode(document.getDocumentElement(), "profiles/profile/dependencyManagement/dependencies/dependency/version").getTextContent()); Assert.assertEquals(newDependencyVersion, model.getDependencies().get(0).getVersion()); } { Dependency projectManagedDependency = new Dependency(); projectManagedDependency.setGroupId("com.cht.test"); projectManagedDependency.setArtifactId("test-dep"); projectManagedDependency.setVersion(oldDependencyVersion); DependencyManagement projectDependencyManagement = new DependencyManagement(); projectDependencyManagement.addDependency(projectManagedDependency); model.setDependencyManagement(projectDependencyManagement); PomUtil.setDependencyVersion(projectManagedDependency, document, "/dependencyManagement", newDependencyVersion); Assert.assertEquals(newDependencyVersion, getNode(document.getDocumentElement(), "dependencyManagement/dependencies/dependency/version") .getTextContent()); Assert.assertEquals(oldDependencyVersion, getNode(document.getDocumentElement(), "profiles/profile/dependencies/dependency/version").getTextContent()); Assert.assertEquals(oldDependencyVersion, getNode(document.getDocumentElement(), "profiles/profile/dependencyManagement/dependencies/dependency/version").getTextContent()); Assert.assertEquals(newDependencyVersion, model.getDependencyManagement().getDependencies().get(0).getVersion()); } Profile testProfile = new Profile(); testProfile.setId("test"); model.getProfiles().add(testProfile); { Dependency profileDependency = new Dependency(); profileDependency.setGroupId("com.cht.test"); profileDependency.setArtifactId("test-dep"); profileDependency.setVersion(oldDependencyVersion); testProfile.getDependencies().add(profileDependency); PomUtil.setDependencyVersion(profileDependency, document, "/profiles/profile[id[text()='test']]", newDependencyVersion); Assert.assertEquals(newDependencyVersion, getNode(document.getDocumentElement(), "profiles/profile/dependencies/dependency/version").getTextContent()); Assert.assertEquals(oldDependencyVersion, getNode(document.getDocumentElement(), "profiles/profile/dependencyManagement/dependencies/dependency/version").getTextContent()); Assert.assertEquals(newDependencyVersion, model.getProfiles().get(0).getDependencies().get(0).getVersion()); } { Dependency profileManagedDependency = new Dependency(); profileManagedDependency.setGroupId("com.cht.test"); profileManagedDependency.setArtifactId("test-dep"); profileManagedDependency.setVersion(oldDependencyVersion); DependencyManagement profileDependencyManagement = new DependencyManagement(); profileDependencyManagement.addDependency(profileManagedDependency); testProfile.setDependencyManagement(profileDependencyManagement); PomUtil.setDependencyVersion(profileManagedDependency, document, "/profiles/profile[id[text()='test']]/dependencyManagement", newDependencyVersion); Assert.assertEquals(newDependencyVersion, getNode(document.getDocumentElement(), "profiles/profile/dependencyManagement/dependencies/dependency/version").getTextContent()); Assert.assertEquals(newDependencyVersion, model.getProfiles().get(0).getDependencies().get(0).getVersion()); } }