Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(updater): add maven pom.xml file support (#33) #109

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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. 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).


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 @@ -16,10 +15,11 @@ A utility for versioning using [semver](https://semver.org/) and CHANGELOG gener
_Having problems? Want to contribute? Join us on the [node-tooling community Slack](http://devtoolscommunity.herokuapp.com)_.

- [Commit and Tag Version](#commit-and-tag-version)
- [How It Works:](#how-it-works)
- [`bumpFiles`, `packageFiles` and `updaters`](#bumpfiles-packagefiles-and-updaters)
- [Gradle Support (Java/Kotlin)](#gradle-support-javakotlin)
- [.NET Support](#net-support)
- [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)
- [As a local `npm run` script](#as-a-local-npm-run-script)
- [As global `bin`](#as-global-bin)
Expand Down Expand Up @@ -52,8 +52,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
- [`writeVersion(contents = string, version: string): string`](#writeversioncontents--string-version-string-string)
- [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 @@ -81,6 +80,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 @@ -93,6 +100,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 @@ -146,7 +154,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 @@ -306,7 +314,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 (filename === 'pom.xml') {
return getUpdaterByType('maven')
}
if (/build.gradle/.test(filename)) {
return getUpdaterByType('gradle')
}
Expand Down
42 changes: 42 additions & 0 deletions lib/updaters/types/maven.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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
Loading