Review of build.gradle request


(Ron) #1

I am new to Gradle and managed to create a build file that works as intended:

  • Generate SOAP-WS classes from a WSDL
  • Build a Spring Boot application

I would love to hear some feedback about the build file itself, as I’m sure there are things that could be done easier and possibly improve build performance. This is the build.gradle I’m talking about, afterwards I’ll have some specific questions:

buildscript {
    ext {
        appName = 'webservice'
        appVersion = '1.0.0-SNAPSHOT'
        springBootVersion = '1.4.3.RELEASE'
        cxfVersion = '3.1.9'
        globalSourceCompatibility = 1.8
        globalTargetCompatibility = 1.8
        generatedSources = "${buildDir}/generated-sources/"
        cxfOutputDir = "${generatedSources}/cxf"
        resourcesDir = "src/main/resources"
        wsdlDir = file("${resourcesDir}/wsdl")
        bindingFile = file("${wsdlDir}/binding.xml")
    }
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

configurations {
    cxfTool
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = appVersion

sourceSets {
    main.java.srcDirs cxfOutputDir
}

task updateApplicationProperties {
    doLast {
        def configFile = new File('src/main/resources/application.yml')
        println "updating version to '${appVersion}' in ${configFile}"
        String configContent = configFile.getText('UTF-8')
        configContent = configContent.replaceAll(/info:\n  build:\n    version: .*/, "info:\n  build:\n    version: ${appVersion}")
        configFile.write(configContent, 'UTF-8')
    }
}

task updateManifest {
    doLast {
        def ymlFiles = new FileNameByRegexFinder().getFileNames(".", /manifest.*\.yml/)
        ymlFiles.each { String filePath ->
            def manifest = new File(filePath)
            println "updating ${manifest.name}"
            String manifestContent = manifest.getText('UTF-8')
            manifestContent = manifestContent.replaceAll(/  path: build\/libs\/.*\.jar/, "  path: build/libs/${appName}-${appVersion}.jar")
            manifest.write(manifestContent, 'UTF-8')
        }
    }
}

task wsdl2java(type: JavaExec) {
    ext {
        outputDir = file(cxfOutputDir)
        wsdlFiles = new FileNameByRegexFinder().getFileNames("${wsdlDir}", /.*\.wsdl/)
    }

    wsdlFiles.each { String wsdlFile ->
        outputs.upToDateWhen { false }
        outputs.dir outputDir
        main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
        classpath = configurations.cxfTool
        args '-d', outputDir
        args '-b', bindingFile
        args '-verbose'
        args '-validate'
        args wsdlFile
    }
}

tasks.withType(JavaCompile) {
    sourceCompatibility = globalSourceCompatibility
    targetCompatibility = globalTargetCompatibility
    dependsOn updateApplicationProperties
    dependsOn updateManifest
    dependsOn wsdl2java
}

jar {
    baseName = "${appName}"
}

repositories {
    mavenCentral()
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.16.10'

    compile 'org.springframework.cloud:spring-cloud-spring-service-connector'
    compile 'org.springframework.cloud:spring-cloud-cloudfoundry-connector'
    compile 'org.springframework.cloud:spring-cloud-starter-zipkin'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-web-services'
    compile 'org.springframework.boot:spring-boot-configuration-processor'
    compile 'com.h2database:h2:1.4.192'
    compile 'io.springfox:springfox-swagger2:2.6.0'
    compile 'io.springfox:springfox-swagger-ui:2.6.0'
    compile 'com.github.ulisesbocchio:jasypt-spring-boot-starter:1.8'
    compile 'org.modelmapper:modelmapper:0.7.7'
    compile 'io.prometheus:simpleclient:0.0.18'
    compile 'io.prometheus:simpleclient_common:0.0.18'
    compile 'io.prometheus:simpleclient_hotspot:0.0.18'
    compile 'io.prometheus:simpleclient_servlet:0.0.18'
    compile 'io.prometheus:simpleclient_pushgateway:0.0.18'
    compile 'io.prometheus:simpleclient_spring_boot:0.0.18'

    //compile group: 'org.apache.ws.commons.axiom', name: 'axiom-api', version: '1.2.20' 
    
    compile 'wsdl4j:wsdl4j:1.6.3'

    testCompile 'org.springframework.boot:spring-boot-starter-test'

    cxfTool "org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:$cxfVersion"
    cxfTool "org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:$cxfVersion"
    cxfTool "org.apache.cxf:cxf-tools-common:$cxfVersion"
    cxfTool "org.apache.cxf:cxf-tools-wsdlto-core:$cxfVersion"
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR1"
    }
}

More questions:

  1. Why are two “repositories” needed: One in ‘buildscript’ and one on root level?
  2. Should I use “compile group: …, name:…, version:…” instead of ‘group:name:version’?
  3. How could I manage to ensure updateApplicationProperties and updateManifest only run when the information change?
  4. Can I optimize the build file anyhow?

Thank you very much!