plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8

sourceSets {
    main {
        java {
            srcDirs('src/main/java')
        }
        resources {
            srcDirs('src/main/resources')
        }
    }
}

test {
    if (project.hasProperty('excludeTests')) {
        exclude project.property('excludeTests')
    }
}

// Reproducible builds.
tasks.withType(AbstractArchiveTask) {
    preserveFileTimestamps = false
    reproducibleFileOrder = true
}

dependencies {
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.3'
    implementation 'com.sun.xml.bind:jaxb-impl:2.3.3'
    implementation 'org.eclipse.persistence:eclipselink:2.4.2'
    implementation 'org.json:json:20201115'
    implementation 'com.google.guava:guava:30.1-jre'

    testImplementation(
        'com.google.code.gson:gson:2.8.6',
        'junit:junit:4.12',
        'org.junit.jupiter:junit-jupiter-api:5.4.2',
        'org.skyscreamer:jsonassert:1.5.0'
    )
    testRuntimeOnly(
        'org.junit.jupiter:junit-jupiter-engine:5.4.2',
        'org.junit.vintage:junit-vintage-engine:5.4.2'
    )
}

compileJava {
    dependsOn 'generateXJCClasses'
}

task generateXJCClasses() {
    doLast {
        generateXJCClassesCDARev2()
    }
}

void generateXJCClassesCDARev2() {
    // Only regenerate classes if the main generated class POCDMT000040ClinicalDocument.java does not exist.
    if (file("./src/main/java/com/google/cloud/healthcare/etl/xmltojson/xjcgen/ccdarev2/org/hl7/v3/POCDMT000040ClinicalDocument.java").exists()) {
        return;
    }
    exec {
        workingDir './'
        commandLine 'sh','./genccdarev2classes.sh'
    }
}

//=============================================================================
// Clean - Must remove generated sources.
//=============================================================================
clean {
    delete 'build'
    delete 'lib'
    delete 'src/main/java/com/google/cloud/healthcare/etl/xmltojson/xjcgen/ccdarev2'
    delete fileTree(dir: 'src/main/java/com/google/cloud/healthcare/etl/xmltojson/schemas' , include: '**/*.xsd')
    delete files('gradle/wrapper')
}
