[JAVA] Gradle Dependent Package Version Control Best Practices

Overview

As a result of examining each of the following, it seems that managing the target with the property outside + array is transparent and easy to maintain. I referred to various open source projects and the opinions of other engineers.

--Solid writing --Variable --Outside the property -** Manage the target with an array while going out to the property **

Solid writing

build.gradle


dependencies {
    // JUnit
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0'
}

Variableization

build.gradle


def junitVersion = '5.2.0'

dependencies {
    // JUnit
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitVersion
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junitVersion
}

Out to the property

For homemade properties, I decided to manage the keys in uppercase snake cases, as Gradle may actually suffer from property names that are already in use. Other than that, it's about half and half to manage lowercase letters separated by dots. I thought that if I put it in a capital snake case, it wouldn't wear anything inside Gradle at least, so I did this.

gradle.properties


LIBRARY_VERSION_JUNIT=5.2.0

build.gradle


def junitVersion = (String) project.LIBRARY_VERSION_JUNIT

dependencies {
    // JUnit
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitVersion
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junitVersion
}

Manage the target with an array while going out to the property

gradle.properties


LIBRARY_VERSION_JUNIT=5.2.0

build.gradle


def libraryVersions = [
        junit: (String) project.LIBRARY_VERSION_JUNIT,
]

dependencies {
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: libraryVersions.junit
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: libraryVersions.junit
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: libraryVersions.junit
}

The advantage is that the source can be stored in libraryVersions. , and by grouping, it can be handled as a list of what packages are available. When adding a package, it is troublesome because it is necessary to write the property and the array respectively, but when updating only the version, only the property needs to be changed, so the trouble later should be reduced.

Recommended Posts

Gradle Dependent Package Version Control Best Practices
# Java: vol1: [java / JDK version control best practice]
[Rails version control] rails version downgrade