1

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.

shashantrika
  • 1,039
  • 1
  • 9
  • 29
  • 1
    "which has both" is already bad/probably wrong... Better: have only one(!) (and automatically export/deploy/publish the other.. i think Gradle>maven is "more mature" ...see e.g. https://github.com/spring-projects/spring-boot ..it "maintains" only a gradle build, but exports also to maven – xerx593 Oct 30 '22 at 16:25
  • *export to maven: easily `$ ./gradlew publishToMavenLocal` – xerx593 Oct 30 '22 at 16:32
  • You *cannot have* maven-lovers & gradle-fans united in one (build) project! Where should that lead? Atm. gradle2maven seems superior, since it is a 1-liner ...whereas maven2gradle needs like studies & years of XP:) – xerx593 Oct 30 '22 at 16:40
  • @xerx593 : Thanks for the information. I will explore exporting to maven from gradle build. – shashantrika Oct 31 '22 at 14:10

1 Answers1

1

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

Pexers
  • 953
  • 1
  • 7
  • 20
  • Unfortunately "properties-maven-plugin" won't work for versions. https://github.com/mojohaus/properties-maven-plugin/issues/26. – shashantrika Oct 30 '22 at 15:59
  • 1
    Thank you for the comment @shashantrika. I've updated my answer with this information. – Pexers Oct 30 '22 at 16:25