This is possible using the gradle.properties
file for both Gradle and Maven.
For instance, you can have these properties:
MYSQL_CONNECTOR_VERSION=8.0.31
ANOTHER_PROPERTY=something
Gradle looks for gradle.properties
files in these places (source):
- The root project directory - maybe the best option for you
- The sub-project directory
- Under gradle user home directory defined by the
GRADLE_USER_HOME
environment variable, which if not set defaults to USER_HOME/.gradle
Then, in build.gradle
, you can read these properties like so:
task printProps {
doFirst {
println MYSQL_CONNECTOR_VERSION
println ANOTHER_PROPERTY
}
}
As for Maven, you can access this file using the Properties Maven Plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>path/to/gradle.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
And use them just like you would with the usual Maven properties.
UPDATE
It looks like the documentation for the Properties Maven Plugin is misleading, or not accurate enough. For the time being, it's not possible to read version properties from an external properties file, even though it's stated:
The read-project-properties goal reads property files and URLs and
stores the properties as project properties. It serves as an alternate
to specifying properties in pom.xml
For more information regarding this issue, follow this GitHub discussion:
https://github.com/mojohaus/properties-maven-plugin/issues/26