Skip to content

Commit efae0a4

Browse files
authored
[MNG-8586] Expose Maven version segments as properties (#2116)
Expose version segments. Also add the properties to generated doco. --- https://issues.apache.org/jira/browse/MNG-8586
1 parent 69980ee commit efae0a4

File tree

6 files changed

+349
-194
lines changed

6 files changed

+349
-194
lines changed

api/maven-api-annotations/src/main/java/org/apache/maven/api/annotations/Config.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,21 @@
3838

3939
boolean readOnly() default false;
4040

41+
/**
42+
* Property source.
43+
*/
4144
enum Source {
45+
/**
46+
* Maven system properties.
47+
*/
48+
SYSTEM_PROPERTIES,
49+
/**
50+
* Maven user properties.
51+
*/
4252
USER_PROPERTIES,
53+
/**
54+
* Project properties.
55+
*/
4356
MODEL
4457
}
4558
}

api/maven-api-core/src/main/java/org/apache/maven/api/Constants.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,57 @@ public final class Constants {
3030
*
3131
* @since 3.0.0
3232
*/
33-
@Config(readOnly = true)
33+
@Config(readOnly = true, source = Config.Source.SYSTEM_PROPERTIES)
3434
public static final String MAVEN_HOME = "maven.home";
3535

36+
/**
37+
* Maven version.
38+
*
39+
* @since 3.0.0
40+
*/
41+
@Config(readOnly = true, source = Config.Source.SYSTEM_PROPERTIES)
42+
public static final String MAVEN_VERSION = "maven.version";
43+
44+
/**
45+
* Maven major version: contains the major segment of this Maven version.
46+
*
47+
* @since 4.0.0
48+
*/
49+
@Config(readOnly = true, source = Config.Source.SYSTEM_PROPERTIES)
50+
public static final String MAVEN_VERSION_MAJOR = "maven.version.major";
51+
52+
/**
53+
* Maven minor version: contains the minor segment of this Maven version.
54+
*
55+
* @since 4.0.0
56+
*/
57+
@Config(readOnly = true, source = Config.Source.SYSTEM_PROPERTIES)
58+
public static final String MAVEN_VERSION_MINOR = "maven.version.minor";
59+
60+
/**
61+
* Maven patch version: contains the patch segment of this Maven version.
62+
*
63+
* @since 4.0.0
64+
*/
65+
@Config(readOnly = true, source = Config.Source.SYSTEM_PROPERTIES)
66+
public static final String MAVEN_VERSION_PATCH = "maven.version.patch";
67+
68+
/**
69+
* Maven snapshot: contains "true" if this Maven is a snapshot version.
70+
*
71+
* @since 4.0.0
72+
*/
73+
@Config(readOnly = true, source = Config.Source.SYSTEM_PROPERTIES)
74+
public static final String MAVEN_VERSION_SNAPSHOT = "maven.version.snapshot";
75+
76+
/**
77+
* Maven build version: a human-readable string containing this Maven version, buildnumber, and time of its build.
78+
*
79+
* @since 3.0.0
80+
*/
81+
@Config(readOnly = true, source = Config.Source.SYSTEM_PROPERTIES)
82+
public static final String MAVEN_BUILD_VERSION = "maven.build.version";
83+
3684
/**
3785
* Maven installation configuration directory.
3886
*

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,26 @@ protected Map<String, String> populateSystemProperties(LocalContext context) {
350350
Properties buildProperties = CLIReportingUtils.getBuildProperties();
351351

352352
String mavenVersion = buildProperties.getProperty(CLIReportingUtils.BUILD_VERSION_PROPERTY);
353-
systemProperties.setProperty("maven.version", mavenVersion);
353+
systemProperties.setProperty(Constants.MAVEN_VERSION, mavenVersion);
354+
355+
boolean snapshot = mavenVersion.endsWith("SNAPSHOT");
356+
if (snapshot) {
357+
mavenVersion = mavenVersion.substring(0, mavenVersion.length() - "SNAPSHOT".length());
358+
if (mavenVersion.endsWith("-")) {
359+
mavenVersion = mavenVersion.substring(0, mavenVersion.length() - 1);
360+
}
361+
}
362+
String[] versionElements = mavenVersion.split("\\.");
363+
if (versionElements.length != 3) {
364+
throw new IllegalStateException("Maven version is expected to have 3 segments: '" + mavenVersion + "'");
365+
}
366+
systemProperties.setProperty(Constants.MAVEN_VERSION_MAJOR, versionElements[0]);
367+
systemProperties.setProperty(Constants.MAVEN_VERSION_MINOR, versionElements[1]);
368+
systemProperties.setProperty(Constants.MAVEN_VERSION_PATCH, versionElements[2]);
369+
systemProperties.setProperty(Constants.MAVEN_VERSION_SNAPSHOT, Boolean.toString(snapshot));
354370

355371
String mavenBuildVersion = CLIReportingUtils.createMavenVersionString(buildProperties);
356-
systemProperties.setProperty("maven.build.version", mavenBuildVersion);
372+
systemProperties.setProperty(Constants.MAVEN_BUILD_VERSION, mavenBuildVersion);
357373

358374
Map<String, String> result = toMap(systemProperties);
359375
result.putAll(context.systemPropertiesOverrides);

0 commit comments

Comments
 (0)