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.