Skip to content

Commit

Permalink
feat(updater): add maven pom.xml file support (#33, #109) (#123)
Browse files Browse the repository at this point in the history
* feat: added maven support

* fix: correct updater for pom.xml files
  • Loading branch information
TaylorHo authored Jan 15, 2024
1 parent c137339 commit 6466beb
Show file tree
Hide file tree
Showing 10 changed files with 681 additions and 11 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
> **`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.

A utility for versioning using [semver](https://semver.org/) and CHANGELOG generation powered by [Conventional Commits](https://conventionalcommits.org).

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


### How It Works:
### How It Works

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

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!

### Maven Support (Java/Kotlin)

If you are using Maven, then just point to your `pom.xml` file.

```sh
commit-and-tag-version --packageFiles pom.xml --bumpFiles pom.xml
```

### Gradle Support (Java/Kotlin)

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

If you are using .NET with `.csproj` files.
This is going to read and update only the `<Version>` tag in the file.

```sh
commit-and-tag-version --packageFiles <YOUR-PROJECT-NAME>.csproj --bumpFiles <YOUR-PROJECT-NAME>.csproj
```
Expand Down Expand Up @@ -148,7 +157,7 @@ You can configure `commit-and-tag-version` either by:
1. Placing a `commit-and-tag-version` stanza in your `package.json` (assuming
your project is JavaScript).

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

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

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

```json
Expand Down
4 changes: 4 additions & 0 deletions lib/updaters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const JSON_BUMP_FILES = require('../../defaults').bumpFiles;
const updatersByType = {
json: require('./types/json'),
'plain-text': require('./types/plain-text'),
maven: require('./types/maven'),
gradle: require('./types/gradle'),
csproj: require('./types/csproj'),
};
Expand All @@ -23,6 +24,9 @@ function getUpdaterByFilename(filename) {
if (PLAIN_TEXT_BUMP_FILES.includes(filename)) {
return getUpdaterByType('plain-text');
}
if (/pom.xml/.test(filename)) {
return getUpdaterByType('maven');
}
if (/build.gradle/.test(filename)) {
return getUpdaterByType('gradle');
}
Expand Down
44 changes: 44 additions & 0 deletions lib/updaters/types/maven.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const jsdom = require('jsdom');
const serialize = require('w3c-xmlserializer');
const detectNewline = require('detect-newline');
const CRLF = '\r\n';
const LF = '\n';

function pomDocument(contents) {
const dom = new jsdom.JSDOM('');
const parser = new dom.window.DOMParser();
return parser.parseFromString(contents, 'application/xml');
}

function pomVersionElement(document) {
const versionElement = document.querySelector('project > version');

if (!versionElement) {
throw new Error(
'Failed to read the version field in your pom file - is it present?',
);
}

return versionElement;
}

module.exports.readVersion = function (contents) {
const document = pomDocument(contents);
return pomVersionElement(document).textContent;
};

module.exports.writeVersion = function (contents, version) {
const newline = detectNewline(contents);
const document = pomDocument(contents);
const versionElement = pomVersionElement(document);

versionElement.textContent = version;

const xml = serialize(document);

if (newline === CRLF) {
return xml.replace(/\n/g, CRLF) + CRLF;
}

return xml + LF;
};
Loading

0 comments on commit 6466beb

Please sign in to comment.