Skip to content

Commit 8a9e181

Browse files
feat(updater): add maven pom.xml file support (absolute-version#33)
1 parent 625d69d commit 8a9e181

11 files changed

+976
-23
lines changed

README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
> **`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. To migrate, you can drop in `commit-and-tag-version` in place of `standard-version`. There are no changes in 9.5.0, other than to add the package.json config key `commit-and-tag-version` (the previous configuration key `standard-version` will still work).
66
7-
87
A utility for versioning using [semver](https://semver.org/) and CHANGELOG generation powered by [Conventional Commits](https://conventionalcommits.org).
98

109
![ci](https://github.com/absolute-version/commit-and-tag-version/workflows/ci/badge.svg)
@@ -16,10 +15,11 @@ A utility for versioning using [semver](https://semver.org/) and CHANGELOG gener
1615
_Having problems? Want to contribute? Join us on the [node-tooling community Slack](http://devtoolscommunity.herokuapp.com)_.
1716

1817
- [Commit and Tag Version](#commit-and-tag-version)
19-
- [How It Works:](#how-it-works)
20-
- [`bumpFiles`, `packageFiles` and `updaters`](#bumpfiles-packagefiles-and-updaters)
21-
- [Gradle Support (Java/Kotlin)](#gradle-support-javakotlin)
22-
- [.NET Support](#net-support)
18+
- [How It Works:](#how-it-works)
19+
- [`bumpFiles`, `packageFiles` and `updaters`](#bumpfiles-packagefiles-and-updaters)
20+
- [Maven Support (Java/Kotlin)](#maven-support-javakotlin)
21+
- [Gradle Support (Java/Kotlin)](#gradle-support-javakotlin)
22+
- [.NET Support](#net-support)
2323
- [Installing `commit-and-tag-version`](#installing-commit-and-tag-version)
2424
- [As a local `npm run` script](#as-a-local-npm-run-script)
2525
- [As global `bin`](#as-global-bin)
@@ -52,8 +52,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
5252
- [`writeVersion(contents = string, version: string): string`](#writeversioncontents--string-version-string-string)
5353
- [License](#license)
5454

55-
56-
### How It Works:
55+
### How It Works
5756

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

8281
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!
8382

83+
### Maven Support (Java/Kotlin)
84+
85+
If you are using Maven, then just point to your `pom.xml` file.
86+
87+
```sh
88+
commit-and-tag-version --packageFiles pom.xml --bumpFiles pom.xml
89+
```
90+
8491
### Gradle Support (Java/Kotlin)
8592

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

94101
If you are using .NET with `.csproj` files.
95102
This is going to read and update only the `<Version>` tag in the file.
103+
96104
```sh
97105
commit-and-tag-version --packageFiles <YOUR-PROJECT-NAME>.csproj --bumpFiles <YOUR-PROJECT-NAME>.csproj
98106
```
@@ -146,7 +154,7 @@ You can configure `commit-and-tag-version` either by:
146154
1. Placing a `commit-and-tag-version` stanza in your `package.json` (assuming
147155
your project is JavaScript).
148156

149-
> Note for users who have migrated to
157+
> Note for users who have migrated to
150158
`commit-and-tag-version` from `standard-version`: the previous package.json configuration key of `standard-version` will still work.
151159

152160
2. Creating a `.versionrc`, `.versionrc.json` or `.versionrc.js`.
@@ -306,7 +314,7 @@ Simply add the following to your package.json to configure lifecycle scripts:
306314
```
307315

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

312320
```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 (filename === 'pom.xml') {
28+
return getUpdaterByType('maven')
29+
}
2630
if (/build.gradle/.test(filename)) {
2731
return getUpdaterByType('gradle')
2832
}

lib/updaters/types/maven.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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('Failed to read the version field in your pom file - is it present?')
18+
}
19+
20+
return versionElement
21+
}
22+
23+
module.exports.readVersion = function (contents) {
24+
const document = pomDocument(contents)
25+
return pomVersionElement(document).textContent
26+
}
27+
28+
module.exports.writeVersion = function (contents, version) {
29+
const newline = detectNewline(contents)
30+
const document = pomDocument(contents)
31+
const versionElement = pomVersionElement(document)
32+
33+
versionElement.textContent = version
34+
35+
const xml = serialize(document)
36+
37+
if (newline === CRLF) {
38+
return xml.replace(/\n/g, CRLF) + CRLF
39+
}
40+
41+
return xml + LF
42+
}

0 commit comments

Comments
 (0)