diff --git a/README.md b/README.md index 87ee121..cbff5cb 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,9 @@ package is put there before packaging. The default is `dist/`. The package file "fileEncoding" : "utf-8", // file encoding when traversing the file system, default is UTF-8 "generatePom" : true, // generate a POM based on the configuration "pomFile" : "pom.xml", // use this existing pom.xml instead of generating one (generatePom must be false) + "version" : "{version}", // sets the final version of the release + "semver" : "minor", // increases package.json's version. Values here can be any semver.inc release + // type strings, or an array of the release type and prerelease components. "repositories" : [ // array of repositories, each with id and url to a Maven repository. { "id": "example-internal-snapshot", @@ -81,6 +84,43 @@ Usage: `maven.deploy( repositoryId, [snapshot = false], [callback])` maven.config(config); maven.deploy('example-internal-release', 'file.jar'); +## Versioning +By default, an artifact's version is based on the version in package.json (ie. config.version==='{version}'). To control the version in package.json, you can specify the semver release type param and any prerelease params. +All prerelease components in the resulting version will be turned into maven qualifiers and separated by '-' instead of '.'. For example 1.2.3-alpha.3 will be 1.2.3-alpha-3 + +Note: semver is not configured by default for release artifacts. + +### Snapshots + +All snapshot builds will have the '-SNAPSHOT' qualifier appended to them. +As well, if a version has prerelease components then the last build number will be dropped in place of the SNAPSHOT qualifier. + +Note: semver is set to 'patch' by default for snapshot artifacts. + +### Example: bumping the patch version + + console.log(packageJson.version); //1.2.3 + config.semver = 'patch'; + maven.config(config); + maven.deploy('example-internal-release'); + //artifact version will be 1.2.4 + +### Example: bumping the release candidate version + + console.log(packageJson.version); //1.2.3-alpha.3 + config.semver = ['prerelease', 'alpha']; + maven.config(config); + maven.deploy('example-internal-release'); + //artifact version will be 1.2.3-alpha-4 + +### Example: replacing build numbers with SNAPSHOT + + console.log(packageJson.version); //1.2.3-alpha.3 + config.semver = ['prerelease', 'alpha']; + maven.config(config); + maven.deploy('example-internal-release', true); + //artifact version will be 1.2.3-alpha-SNAPSHOT + ## Contributing We would love your contribution, please consult the [contributing](CONTRIBUTE.md) page for how to make your contributions land into the project as easily as possible. diff --git a/index.js b/index.js index 35e12c3..874eadc 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,8 @@ const DEFAULT_CONFIG = { type: 'war', fileEncoding: 'utf-8', version: '{version}', - generatePom: true + generatePom: true, + semver: null }; validateConfig = defineOpts({ @@ -129,8 +130,47 @@ function getConfig (isSnapshot) { var pkg = readPackageJSON(configTmpl.fileEncoding); + if (!configTmpl.semver && isSnapshot) { + //for backwards compat inc 'patch' version when making snapshots + configTmpl.semver = 'patch'; + } + + if (configTmpl.semver) { + if (Array.isArray(configTmpl.semver)) { + //interpret semver config as params to semver.inc + pkg.version = semver.inc.apply(null, [pkg.version].concat(configTmpl.semver)); + } + else if (semver.valid(configTmpl.semver)) { + //semver is a valid version to use + pkg.version = configTmpl.semver + } + else { + //interpret semver as the second option to semver.inc + pkg.version = semver.inc(pkg.version, configTmpl.semver); + } + } + + //convert prerelease components into proper qualifiers + var components = semver.prerelease(pkg.version); + if (components) { + //check if we should drop the last build number component + if (isSnapshot && isFinite(components[components.length - 1])) { + components.pop(); + } + + //replace the '.' between prerelease components with '-' to be proper qualifiers + if (components.length > 0) { + pkg.version = [semver.major(pkg.version), semver.minor(pkg.version), semver.patch(pkg.version)].join('.') + '-' + components.join('-'); + } + else { + //should only get here if we popped off the last component, so we don't want to put an extra '-' + pkg.version = [semver.major(pkg.version), semver.minor(pkg.version), semver.patch(pkg.version)].join('.') + } + } + + //pkg version should be modified above but still need to add snapshot when appropriate if (isSnapshot) { - pkg.version = semver.inc(pkg.version, 'patch') + '-SNAPSHOT'; + pkg.version += '-SNAPSHOT'; } return filterConfig(configTmpl, pkg); diff --git a/test.js b/test.js index 5823c83..0411503 100644 --- a/test.js +++ b/test.js @@ -142,6 +142,56 @@ describe('maven-deploy', function () { }); }); + describe('versioning', function() { + it('semver release type passed in for releases', function () { + const EXPECTED_VERSION = '1.0.1'; + + maven.config( extend({finalName: '{name}-{version}', semver: 'patch'}, TEST_CONFIG) ); + maven.package(); + + assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war'); + }); + + it('semver release type passed in for snapshots', function () { + const EXPECTED_VERSION = '1.1.0-SNAPSHOT'; + + maven.config( extend({finalName: '{name}-{version}', semver: 'minor'}, TEST_CONFIG) ); + maven.package(true); + + assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war'); + }); + + it('semver prerelease types for releases', function () { + const EXPECTED_VERSION = '1.0.1-beta-0'; + + maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease', 'beta']}, TEST_CONFIG) ); + maven.package(); + + assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war'); + }); + + it('semver prerelease build number qualifiers', function () { + const EXPECTED_VERSION = '1.2.3-6'; + + npmVersion('1.2.3-5'); + maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease']}, TEST_CONFIG) ); + maven.package(); + + assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war'); + }); + + it('semver replacing prerelease build numbers with snapshots', function () { + const EXPECTED_VERSION = '1.2.3-alpha-SNAPSHOT'; + + npmVersion('1.2.3-alpha.5'); + maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease', 'alpha']}, TEST_CONFIG) ); + maven.package(true); + + assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war'); + }); + + }); + describe('package', function () { it('should create an archive based on defaults', function () { maven.config(TEST_CONFIG);