Skip to content

Instantly share code, notes, and snippets.

@asdf913
Created June 18, 2026 02:57
Show Gist options
  • Select an option

  • Save asdf913/96173803a61e7dcd552a56919d891b13 to your computer and use it in GitHub Desktop.

Select an option

Save asdf913/96173803a61e7dcd552a56919d891b13 to your computer and use it in GitHub Desktop.
UpdateVersion - Program to update maven dependency version for a specified pom.xml file
<dependencies>
<dependency>
<artifactId>commons-lang3</artifactId>
<groupId>org.apache.commons</groupId>
<version>3.20.0</version>
</dependency>
</dependencies>
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import javax.xml.stream.Location;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
public class UpdateVersion {
private static class Dependency {
private String groupId, artifactId, version = null;
private int versionIndexStart, versionIndexEnd = 0;
}
public static void main(final String[] args) throws XMLStreamException, IOException {
//
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
//
final Map<String, String> map = toMap(args);
//
final Path path = Path.of(map != null ? map.get("file") : null);
//
final String string = Files.readString(path);
//
final XMLStreamReader xmlStreamReader = xmlInputFactory != null
? xmlInputFactory.createXMLStreamReader(Files.newInputStream(path))
: null;
//
String localName = null;
//
boolean dependencies = false, exclusions = false;
//
int event = 0, characterOffset = 0;
//
Dependency dependency = null;
//
String version = null;
//
Location location = null;
//
while (xmlStreamReader != null && xmlStreamReader.hasNext()) {
//
if ((event = xmlStreamReader.next()) == XMLStreamConstants.START_ELEMENT) {
//
if ((Objects.equals(localName = xmlStreamReader.getLocalName(), "dependencies") && !dependencies
&& (dependencies = true)) || !dependencies || Objects.equals(localName, "dependency")
|| Objects.equals(localName, "scope")
|| (Objects.equals(localName, "exclusions") && !exclusions && (exclusions = true)) || exclusions
|| (location = xmlStreamReader.getLocation()) == null) {
//
continue;
//
} // if
//
if (contains(Arrays.asList("groupId", "artifactId", "version"), localName)) {
//
characterOffset = location.getCharacterOffset();
//
} // if
//
} else if (event == XMLStreamConstants.END_ELEMENT) {
//
location = xmlStreamReader.getLocation();
//
if ((Objects.equals(localName = xmlStreamReader.getLocalName(), "dependencies") && dependencies
&& !(dependencies = false))) {
//
break;
//
} else if (Objects.equals(localName, "scope")
|| (Objects.equals(localName, "exclusions") && exclusions && !(exclusions = false))) {
//
continue;
//
} else if (Objects.equals(localName, "dependency")) {
//
if (dependency != null) {
//
if (map != null && Objects.equals(dependency.groupId, map.get("groupId"))
&& Objects.equals(dependency.artifactId, map.get("artifactId"))) {
//
if (map.containsKey("version")
&& !Objects.equals(version = map.get("version"), dependency.version)) {
//
final StringBuilder sb = new StringBuilder(ObjectUtils.getIfNull(string, ""));
//
sb.delete(dependency.versionIndexStart, dependency.versionIndexEnd);
//
sb.insert(dependency.versionIndexStart, version);
//
System.out.println(String.format("groupId=%1$s,artifactId=%2$s,version=[%3$s->%4$s]",
dependency.groupId, dependency.artifactId, dependency.version, version));
//
Files.writeString(path, sb);
//
} // if
//
} // if
//
dependency = null;
//
} // if
//
continue;
//
} else if (Objects.equals(localName, "groupId") && dependencies && !exclusions
&& (dependency = ObjectUtils.getIfNull(dependency, Dependency::new)) != null) {
//
dependency.groupId = StringUtils.substring(string, characterOffset,
location.getCharacterOffset() - 10);
//
} else if (Objects.equals(localName, "artifactId") && dependencies && !exclusions
&& (dependency = ObjectUtils.getIfNull(dependency, Dependency::new)) != null) {
//
dependency.artifactId = StringUtils.substring(string, characterOffset,
location.getCharacterOffset() - 13);
//
} else if (Objects.equals(localName, "version") && dependencies && !exclusions
&& (dependency = ObjectUtils.getIfNull(dependency, Dependency::new)) != null) {
//
dependency.version = StringUtils.substring(string, dependency.versionIndexStart = characterOffset,
dependency.versionIndexEnd = location.getCharacterOffset() - 10);
//
} // if
//
} // if
//
} // while
//
if (xmlStreamReader != null) {
//
xmlStreamReader.close();
//
} // if
//
}
private static boolean contains(final Collection<?> instance, final Object item) {
return instance != null && instance.contains(item);
}
private static Map<String, String> toMap(final String... ss) {
//
String s = null;
//
Map<String, String> map = null;
//
for (int i = 0; i < length(ss); i++) {
//
if (Objects.equals(s = ArrayUtils.get(ss, i), "=")) {
//
put(map = ObjectUtils.getIfNull(map, LinkedHashMap::new), "", "");
//
} else if (s != null && s.length() == 2 && s.charAt(0) == '=') {
//
put(map = ObjectUtils.getIfNull(map, LinkedHashMap::new), "", s.substring(1, s.length()));
//
} else if (s != null && s.length() == 2 && s.charAt(s.length() - 1) == '=') {
//
put(map = ObjectUtils.getIfNull(map, LinkedHashMap::new), s.substring(0, s.length() - 1), "");
//
} else if (s != null && s.indexOf('=') >= 0 && s.indexOf('=') == s.lastIndexOf('=')) {
//
put(map = ObjectUtils.getIfNull(map, LinkedHashMap::new), StringUtils.substringBefore(s, '='),
StringUtils.substringAfter(s, '='));
//
} else if (s != null && s.length() > 2 && s.indexOf('=') != s.lastIndexOf('=')) {
//
put(map = ObjectUtils.getIfNull(map, LinkedHashMap::new), StringUtils.substring(s, 0, s.indexOf('=')),
StringUtils.substring(s, s.indexOf('=') + 1));
//
} // if
//
} // for
//
return map;
//
}
private static <K, V> void put(final Map<K, V> instance, final K key, final V value) {
if (instance != null) {
instance.put(key, value);
}
}
private static int length(final Object[] instance) {
return instance != null ? instance.length : 0;
}
}
@asdf913

asdf913 commented Jun 18, 2026

Copy link
Copy Markdown
Author

Sample Command Arguments

file=pom.xml
groupId=org.apache.commons
artifactId=commons-lang3
version=3.20.0

Sample Output

groupId=org.apache.commons,artifactId=commons-lang3,version=[3.19.0->3.20.0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment