Skip to content

Commit

Permalink
#13: Accept version from parent project in file pom.xml, too. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckunki authored Sep 27, 2022
1 parent 396c505 commit 1bc9154
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 14 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ venv/
*.orig
*.old
*.md.html
*.flattened-pom.xml
*.flattened-pom.xml
/.apt_generated/
/.apt_generated_tests/
5 changes: 5 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
1 change: 1 addition & 0 deletions doc/changes/changelog.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions doc/changes/changes_1.2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Maven Project Version Getter 1.2.0, released 2022-09-27

Code name: Version from parent project in file `pom.xml`

## Summary

Enhanced Maven Project Version Getter to accept version from parent project in file `pom.xml`, too.

## Features

* #13: Accept version from parent project in file `pom.xml`, too.

## Dependency Updates

### Plugin Dependency Updates

* Updated `com.exasol:project-keeper-maven-plugin:2.6.2` to `2.8.0`
2 changes: 1 addition & 1 deletion error_code_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ error-tags:
MPVG:
packages:
- com.exasol.mavenprojectversiongetter
highest-index: 1
highest-index: 2
2 changes: 1 addition & 1 deletion pk_generated_parent.pom

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.exasol</groupId>
<artifactId>maven-project-version-getter</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
<name>Maven Project Version Getter</name>
<description>Java helper for getting the current project's version from test code.</description>
<url>https://github.com/exasol/maven-project-version-getter/</url>
Expand Down Expand Up @@ -50,7 +50,7 @@
<plugin>
<groupId>com.exasol</groupId>
<artifactId>project-keeper-maven-plugin</artifactId>
<version>2.6.2</version>
<version>2.8.0</version>
<executions>
<execution>
<goals>
Expand All @@ -64,7 +64,7 @@
<parent>
<artifactId>maven-project-version-getter-generated-parent</artifactId>
<groupId>com.exasol</groupId>
<version>1.1.1</version>
<version>1.2.0</version>
<relativePath>pk_generated_parent.pom</relativePath>
</parent>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,44 @@ private MavenProjectVersionGetter() {

/**
* Get the version of the current project.
*
*
* @return version string
*/
public static String getCurrentProjectVersion() {
return getProjectVersion(Path.of("pom.xml"));
}

/**
* Get the version of the current project as specified for the project itself or for its parent.
*
* @return version string
*/
public static String getVersionOfCurrentProjectOrParent() {
return getVersionOfProjectOrParent(Path.of("pom.xml"));
}

/**
* Get the version as specified for the project itself or for its parent
*
* @param pomFile path to {@code pom.xml} to get the version from
* @return version string
*/
public static String getVersionOfProjectOrParent(final Path pomFile) {
final String version = getProjectVersion(pomFile);
final String parentVersion = getParentVersion(pomFile);
if (version.isEmpty()) {
return parentVersion;
}
if (parentVersion.isEmpty() || parentVersion.equals(version)) {
return version;
}
throw new VersionGetterException(ExaError.messageBuilder("E-MPVG-2")
.message("Inconsistent version information in file {{pom file}}:" //
+ " project version is {{project version}}, while parent version is {{parent version}}.", //
pomFile, version, parentVersion)
.toString());
}

/**
* Get the version of a given project.
*
Expand All @@ -51,7 +82,17 @@ public static String getProjectRevision(final Path pomFile) {
return getPropertyOfXmlFile(pomFile, "/project/properties/revision");
}

private static String getPropertyOfXmlFile(Path pomFile, String propertyXPath) {
/**
* Get the version of the parent project.
*
* @param pomFile path to {@code pom.xml} to get the parent version from
* @return version string
*/
public static String getParentVersion(final Path pomFile) {
return getPropertyOfXmlFile(pomFile, "/project/parent/version");
}

private static String getPropertyOfXmlFile(final Path pomFile, final String propertyXPath) {
try {
final var documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
Expand All @@ -60,9 +101,24 @@ private static String getPropertyOfXmlFile(Path pomFile, String propertyXPath) {
final var xPath = XPathFactory.newInstance().newXPath();
return xPath.compile(propertyXPath).evaluate(pom);
} catch (final XPathExpressionException | SAXException | ParserConfigurationException | IOException exception) {
throw new IllegalStateException(ExaError.messageBuilder("E-MPVG-1")
throw new VersionGetterException(ExaError.messageBuilder("E-MPVG-1")
.message("Failed to get current project version from pom file {{file}}.", pomFile).toString(),
exception);
}
}

/**
* Exception thrown by MavenProjectVersionGetter
*/
public static class VersionGetterException extends RuntimeException {
private static final long serialVersionUID = 1L;

VersionGetterException(final String message, final Exception exception) {
super(message, exception);
}

VersionGetterException(final String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.exasol.mavenprojectversiongetter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
Expand All @@ -12,6 +11,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import com.exasol.mavenprojectversiongetter.MavenProjectVersionGetter.VersionGetterException;

class MavenProjectVersionGetterTest {

@Test
Expand All @@ -21,6 +22,24 @@ void testReadVersion(@TempDir final Path tempDir) throws IOException {
assertThat(MavenProjectVersionGetter.getProjectVersion(pomFile), equalTo("1.2.3"));
}

@Test
void getVersionOfCurrentProjectOrParent(@TempDir final Path tempDir) throws IOException {
final Path pomFile = tempDir.resolve("pom.xml");
Files.writeString(pomFile, "<project><parent><version>2.3.4</version></parent></project>");
assertThat(MavenProjectVersionGetter.getVersionOfProjectOrParent(pomFile), equalTo("2.3.4"));
}

@Test
void inconsistentVersionsOfProjectAndParent(@TempDir final Path tempDir) throws IOException {
final Path pomFile = tempDir.resolve("pom.xml");
Files.writeString(pomFile,
"<project><version>1.2.3</version><parent><version>2.3.4</version></parent></project>");
final Exception exception = assertThrows(VersionGetterException.class,
() -> MavenProjectVersionGetter.getVersionOfProjectOrParent(pomFile));
assertThat(exception.getMessage(), matchesRegex("E-MPVG-2: Inconsistent version information in file .*pom.xml:"
+ " project version is '1.2.3', while parent version is '2.3.4'."));
}

@Test
void testGetVersionOfCurrentProject() {
assertThat(MavenProjectVersionGetter.getCurrentProjectVersion(),
Expand All @@ -30,7 +49,7 @@ void testGetVersionOfCurrentProject() {
@Test
void testGettingVersionFails(@TempDir final Path tempDir) {
final Path nonExistingPomFile = tempDir.resolve("nonExistingPom.xml");
final IllegalStateException exception = assertThrows(IllegalStateException.class,
final Exception exception = assertThrows(VersionGetterException.class,
() -> MavenProjectVersionGetter.getProjectVersion(nonExistingPomFile));
assertThat(exception.getMessage(), startsWith("E-MPVG-1: Failed to get current project version from pom file"));
}
Expand Down

0 comments on commit 1bc9154

Please sign in to comment.