0

I have two gradle task genJaxb and genJaxb2 to generate java code from xsd. They both do the same thing only difference is they generate stub from different files. Is there any to have one task with parameters and call them to avoid duplication.

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "io.freefair.lombok" version "6.4.2"
    id 'jacoco'

}

group = 'com.abc'
sourceCompatibility = '11'

repositories {
    maven {
        url "https://artifactory.three.com/artifactory/libs-release"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
}

// tag::xsd[]
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'build/generated-sources/jaxb'
            srcDir 'build/invoice/generated-sources/jaxb'
        }
    }
}

task genJaxb {
    ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
    ext.schema = "src/main/resources/text.xsd"

    outputs.dir sourcesDir

    doLast() {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                    classpath: configurations.jaxb.asPath
            mkdir(dir: sourcesDir)

            xjc(destdir: sourcesDir, schema: schema) {
                arg(value: "-wsdl")
                produces(dir: sourcesDir, includes: "**/*.java")
            }
        }
    }
}

task genJaxb2 {
    ext.sourcesDir = "${buildDir}/test1/generated-sources/jaxb"
    ext.schema = "src/main/resources/test1.xsd"

    outputs.dir sourcesDir

    doLast() {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                    classpath: configurations.jaxb.asPath
            mkdir(dir: sourcesDir)

            xjc(destdir: sourcesDir, schema: schema) {
                arg(value: "-wsdl")
                produces(dir: sourcesDir, includes: "**/*.java")
            }
        }
    }
}



compileJava.dependsOn genJaxb,genJaxb2
// end::xsd[]

// tag::jaxb[]
configurations {
    jaxb
    
}

bootJar {
    archiveBaseName = 'invoicedelivery/201306'
    archiveVersion =  '0.1.0'
}
// end::jaxb[]


dependencies {
     implementation 'org.springframework.boot:spring-boot-starter-web'
     implementation 'org.springframework.boot:spring-boot-starter-web-services'
     implementation 'wsdl4j:wsdl4j'
     jaxb("org.glassfish.jaxb:jaxb-xjc")
     implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.1'
     testImplementation 'org.springframework.boot:spring-boot-starter-test'
     implementation 'com.github.ben-manes.caffeine:caffeine:3.0.6'
     implementation 'org.springdoc:springdoc-openapi-ui:1.6.6'
     implementation 'com.github.dozermapper:dozer-spring-boot-starter:6.2.0'
     implementation 'net.logstash.logback:logstash-logback-encoder:7.0.1'
     testImplementation 'org.mockito:mockito-inline:4.4.0'
}


tasks.withType(Test) {
    useJUnitPlatform()
}

tasks.named('test') {

    useJUnitPlatform()
}

plugins.withType(JacocoPlugin) {
    tasks["test"].finalizedBy 'jacocoTestReport'
}

jacocoTestReport {
    reports {
        xml.required = true
    }
}

The end goal is to have one gradle task instead of two to avoid code duplication.

1 Answers1

0

You can pass the parameters to Gradle task using -P (project property) and -D (System property) options. These properties can be read in Gradle script using project.getProperty() and system.getProperty()

e.g. Gradle task is as mentioned below

task firstTask {
    doFirst {
       println myArg
     }
}

and then execute the task using below command

./gradlew firstTask -PmyArg=hi

I also found another thread explaining this; please check How to pass parameters or arguments into a gradle task

Other more complicated option would be to create custom tasktype and then use -q option to execute task with custom parameters. Please check below URL for this.

https://docs.gradle.org/current/userguide/custom_tasks.html#sec:declaring_and_using_command_line_options

pratap
  • 538
  • 1
  • 5
  • 11
  • Thanks for your reply All the parameters are known before running the task so I am looking for simple way to invoke the gradle task can you please provide more hint with property – akashsharma3030 Apr 26 '22 at 08:08
  • @akashsharma3030 I have updated above answer. – pratap Apr 26 '22 at 13:12