shashantrika
shashantrika

Reputation: 1089

Common version management for Gradle and maven

We have a spring-boot java project which has both maven and Gradle. The problem is with dependencies, each has its own version of dependencies. For example, in maven MySQL dependency is

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>

Whereas in Gradle, having below:

dependencies {
    implementation(project(':demo'))
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.31'
}

Is there a way to maintain versions for both maven and Gradle in some commonplace? Like from an external property file inside the project? Manually syncing versions for both Maven and Gradle is getting cumbersome.

Upvotes: 1

Views: 292

Answers (1)

Pexers
Pexers

Reputation: 1200

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

Upvotes: 1

Related Questions