void bumpMajorVersion(Document pom) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes project = pom.query("/mvn:project", context); if (project.size() == 0) throw new MojoExecutionException("Pom is broken"); Nodes nodes = pom.query("/mvn:project/mvn:version", context); if (nodes.size() != 1) throw new MojoExecutionException("Version is not declared correctly"); Element e = (Element) nodes.get(0); String[] components = e.getValue().split("\\."); components[0] = Integer.toString(Integer.valueOf(components[0]) + 1); String bumpedVersion = String.join(".", components); Element newVersion = new Element("version", "http://maven.apache.org/POM/4.0.0"); newVersion.appendChild(bumpedVersion); ((Element) project.get(0)).replaceChild(e, newVersion); // TODO check that the next version does not already exist }
XPathContext xc = XPathContext.makeNamespaceContext(doc.getRootElement());
private void updateDependency(Document pom, String dependencyPath, String oldVersion, Artifact artifact) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes nodes = pom.query(dependencyPath, context);
private void updateDependency(Document pom, String dependencyPath, String oldVersion, Artifact artifact) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes nodes = pom.query(dependencyPath, context);
void updateProperty(Document pom, String propertyName, String newVersion) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes nodes = pom.query("//mvn:properties", context); if (nodes.size() > 0) { final Element propertiesElement = (Element) nodes.get(0); Elements properties = propertiesElement.getChildElements(); for (int i = 0; i < properties.size(); i++) { Element property = properties.get(i); if (property.getLocalName().equals(propertyName)) { Element newRange = new Element(propertyName, "http://maven.apache.org/POM/4.0.0"); newRange.appendChild(newVersion); propertiesElement.replaceChild(property, newRange); } } } }
void updateProperty(Document pom, String propertyName, String newVersion) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes nodes = pom.query("//mvn:properties", context); if (nodes.size() > 0) { final Element propertiesElement = (Element) nodes.get(0); Elements properties = propertiesElement.getChildElements(); for (int i = 0; i < properties.size(); i++) { Element property = properties.get(i); if (property.getLocalName().equals(propertyName)) { Element newRange = new Element(propertyName, "http://maven.apache.org/POM/4.0.0"); newRange.appendChild(newVersion); propertiesElement.replaceChild(property, newRange); } } } }
@Test public void bump() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder().build(new File(new File("src/it/update"), "pom.xml")); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("/mvn:project/mvn:version", context); assertThat(versions.size()).isEqualTo(1); assertThat(pom.query("/mvn:project/mvn:version[text()='1.1-SNAPSHOT']", context).size()).isEqualTo(1); assertThat(pom.query("/mvn:project/mvn:version[text()='2.1-SNAPSHOT']", context).size()).isEqualTo(0); new StickyBoundsUpgradeMojo().bumpMajorVersion(pom); assertThat(pom.query("/mvn:project/mvn:version[text()='1.1-SNAPSHOT']", context).size()).isEqualTo(0); assertThat(pom.query("/mvn:project/mvn:version[text()='2.1-SNAPSHOT']", context).size()).isEqualTo(1); } @Test
@Test public void updateWithClassifier() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder() .build(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("classifiers.xml")))); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "", "[2.6,3)"); new StickyBoundsMojo().updateDependency(pom, artifact, "[2.1,3)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(4); Nodes nodes = pom.query("//mvn:version[text()='[2.6,3)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[2.6,3)"); }
@Test public void updateWithClassifier() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder() .build(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("classifiers.xml")))); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "", "[2.6,3)"); new StickyBoundsUpgradeMojo().updateDependency(pom, artifact, "[2.1,3)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(4); Nodes nodes = pom.query("//mvn:version[text()='[2.6,3)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[2.6,3)"); }
@Test public void updateTheClassifier() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder() .build(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("classifiers.xml")))); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "test-jar", "[2.6,3)"); new StickyBoundsUpgradeMojo().updateDependency(pom, artifact, "[2.6,3)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(4); Nodes nodes = pom.query("//mvn:version[text()='[2.6,3)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[2.6,3)"); }
@Test public void updateTheClassifier() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder() .build(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("classifiers.xml")))); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "test-jar", "[2.6,3)"); new StickyBoundsMojo().updateDependency(pom, artifact, "[2.1,3)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(4); Nodes nodes = pom.query("//mvn:version[text()='[2.6,3)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[2.6,3)"); }
@Test public void update() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder().build(new File(new File("src/it/upgrade"), "pom.xml")); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "", "[3.6,4)"); new StickyBoundsUpgradeMojo().updateDependency(pom, artifact, "[3.1,4)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(3); Nodes nodes = pom.query("//mvn:version[text()='[3.6,4)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[3.6,4)"); }
@Test public void update() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder().build(new File(new File("src/it/update"), "pom.xml")); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "", "[3.6,4)"); new StickyBoundsMojo().updateDependency(pom, artifact, "[3.1,4)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(3); Nodes nodes = pom.query("//mvn:version[text()='[3.6,4)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[3.6,4)"); }