Skip to content

Commit 6466beb

Browse files
authored
feat(updater): add maven pom.xml file support (#33, #109) (#123)
* feat: added maven support * fix: correct updater for pom.xml files
1 parent c137339 commit 6466beb

10 files changed

+681
-11
lines changed

README.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
> **`Why was it renamed commit-and-tag-version?`**. I didn't want to scope the package or name it `standard-version-fork`, and it was a good opportunity to make the purpose of the tool clearer. I also wanted to distinguish it from the other tool in this organisation, [`absolute-version`](https://github.com/absolute-version/absolute-version-js), which just prints version information for pre-releases.
88
9-
109
A utility for versioning using [semver](https://semver.org/) and CHANGELOG generation powered by [Conventional Commits](https://conventionalcommits.org).
1110

1211
![ci](https://github.com/absolute-version/commit-and-tag-version/workflows/ci/badge.svg)
@@ -20,6 +19,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
2019
- [Commit and Tag Version](#commit-and-tag-version)
2120
- [How It Works:](#how-it-works)
2221
- [`bumpFiles`, `packageFiles` and `updaters`](#bumpfiles-packagefiles-and-updaters)
22+
- [Maven Support (Java/Kotlin)](#maven-support-javakotlin)
2323
- [Gradle Support (Java/Kotlin)](#gradle-support-javakotlin)
2424
- [.NET Support](#net-support)
2525
- [Installing `commit-and-tag-version`](#installing-commit-and-tag-version)
@@ -55,7 +55,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
5555
- [License](#license)
5656

5757

58-
### How It Works:
58+
### How It Works
5959

6060
1. Follow the [Conventional Commits Specification](https://conventionalcommits.org) in your repository.
6161
2. When you're ready to release, run `commit-and-tag-version`.
@@ -83,6 +83,14 @@ By default, `commit-and-tag-version` assumes you're working in a NodeJS based pr
8383

8484
That said, if you find your self asking [How can I use commit-and-tag-version for additional metadata files, languages or version files?](#can-i-use-commit-and-tag-version-for-additional-metadata-files-languages-or-version-files) – these configuration options will help!
8585

86+
### Maven Support (Java/Kotlin)
87+
88+
If you are using Maven, then just point to your `pom.xml` file.
89+
90+
```sh
91+
commit-and-tag-version --packageFiles pom.xml --bumpFiles pom.xml
92+
```
93+
8694
### Gradle Support (Java/Kotlin)
8795

8896
If you are using Gradle, then just point to your `build.gradle` file (or `build.gradle.kts` if using Kotlin DSL).
@@ -95,6 +103,7 @@ commit-and-tag-version --packageFiles build.gradle --bumpFiles build.gradle
95103

96104
If you are using .NET with `.csproj` files.
97105
This is going to read and update only the `<Version>` tag in the file.
106+
98107
```sh
99108
commit-and-tag-version --packageFiles <YOUR-PROJECT-NAME>.csproj --bumpFiles <YOUR-PROJECT-NAME>.csproj
100109
```
@@ -148,7 +157,7 @@ You can configure `commit-and-tag-version` either by:
148157
1. Placing a `commit-and-tag-version` stanza in your `package.json` (assuming
149158
your project is JavaScript).
150159

151-
> Note for users who have migrated to
160+
> Note for users who have migrated to
152161
`commit-and-tag-version` from `standard-version`: the previous package.json configuration key of `standard-version` will still work.
153162

154163
2. Creating a `.versionrc`, `.versionrc.json` or `.versionrc.js`.
@@ -312,7 +321,7 @@ Simply add the following to your package.json to configure lifecycle scripts:
312321
```
313322

314323
As an example to change from using GitHub to track your items to using your projects Jira use a
315-
`postchangelog` script to replace the url fragment containing 'https://github.com/`myproject`/issues/'
324+
`postchangelog` script to replace the url fragment containing '<https://github.com/`myproject`/issues/>'
316325
with a link to your Jira - assuming you have already installed [replace](https://www.npmjs.com/package/replace)
317326

318327
```json

lib/updaters/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const JSON_BUMP_FILES = require('../../defaults').bumpFiles;
33
const updatersByType = {
44
json: require('./types/json'),
55
'plain-text': require('./types/plain-text'),
6+
maven: require('./types/maven'),
67
gradle: require('./types/gradle'),
78
csproj: require('./types/csproj'),
89
};
@@ -23,6 +24,9 @@ function getUpdaterByFilename(filename) {
2324
if (PLAIN_TEXT_BUMP_FILES.includes(filename)) {
2425
return getUpdaterByType('plain-text');
2526
}
27+
if (/pom.xml/.test(filename)) {
28+
return getUpdaterByType('maven');
29+
}
2630
if (/build.gradle/.test(filename)) {
2731
return getUpdaterByType('gradle');
2832
}

lib/updaters/types/maven.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const jsdom = require('jsdom');
2+
const serialize = require('w3c-xmlserializer');
3+
const detectNewline = require('detect-newline');
4+
const CRLF = '\r\n';
5+
const LF = '\n';
6+
7+
function pomDocument(contents) {
8+
const dom = new jsdom.JSDOM('');
9+
const parser = new dom.window.DOMParser();
10+
return parser.parseFromString(contents, 'application/xml');
11+
}
12+
13+
function pomVersionElement(document) {
14+
const versionElement = document.querySelector('project > version');
15+
16+
if (!versionElement) {
17+
throw new Error(
18+
'Failed to read the version field in your pom file - is it present?',
19+
);
20+
}
21+
22+
return versionElement;
23+
}
24+
25+
module.exports.readVersion = function (contents) {
26+
const document = pomDocument(contents);
27+
return pomVersionElement(document).textContent;
28+
};
29+
30+
module.exports.writeVersion = function (contents, version) {
31+
const newline = detectNewline(contents);
32+
const document = pomDocument(contents);
33+
const versionElement = pomVersionElement(document);
34+
35+
versionElement.textContent = version;
36+
37+
const xml = serialize(document);
38+
39+
if (newline === CRLF) {
40+
return xml.replace(/\n/g, CRLF) + CRLF;
41+
}
42+
43+
return xml + LF;
44+
};

0 commit comments

Comments
 (0)