/**
 * This build.gradle exists to publish our capsule (executable fat jar) to maven. It cannot be placed in the
 * node project because the bintray plugin cannot publish two modules from one project.
 */
apply plugin: 'us.kirchmeier.capsule'
apply plugin: 'corda.common-publishing'

description 'Corda standalone node'

evaluationDependsOn(':node')

configurations {
    runtimeArtifacts.extendsFrom runtimeClasspath
    capsuleRuntime
}

dependencies {
    testRuntimeOnly project(":node")

    // TypeSafe Config: for simple and human friendly config files.
    capsuleRuntime "com.typesafe:config:$typesafe_config_version"
    compileOnly "com.typesafe:config:$typesafe_config_version"
    testRuntimeOnly "com.typesafe:config:$typesafe_config_version"

    // Capsule is a library for building independently executable fat JARs.
    // We only need this dependency to compile our Caplet against.
    compileOnly "co.paralleluniverse:capsule:$capsule_version"
    testImplementation "co.paralleluniverse:capsule:$capsule_version"

    testImplementation "org.junit.jupiter:junit-jupiter-api:${junit_jupiter_version}"
    testImplementation "junit:junit:$junit_version"
}

jar.enabled = false

capsule {
    version capsule_version
}

def nodeProject = project(':node')

configurations.runtimeOnly.canBeResolved = true
tasks.register('buildCordaJAR', FatCapsule) {
    dependsOn(nodeProject.tasks.named('jar'))
    applicationClass 'net.corda.node.Corda'
    archiveBaseName = 'corda'
    archiveClassifier = ''
    archiveVersion = corda_release_version
    archiveName = archiveFileName.get()
    applicationSource = files(
            nodeProject.configurations.runtimeClasspath,
            nodeProject.tasks.jar,
            nodeProject.buildDir.toString() + '/resources/main/corda-reference.conf',
            "$rootDir/config/dev/log4j2.xml",
            'NOTICE' // Copy CDDL notice
    )
    from configurations.capsuleRuntime.files.collect { zipTree(it) }
    with jar

    manifest {
        attributes('Add-Opens': 'java.management/com.sun.jmx.mbeanserver')
    }

    capsuleManifest {
        applicationVersion = corda_release_version
        applicationId = "net.corda.node.Corda"
        // See experimental/quasar-hook/README.md for how to generate.
        def quasarExcludeExpression = "x(antlr**;bftsmart**;co.paralleluniverse**;com.codahale**;com.esotericsoftware**;com.fasterxml**;com.google**;com.ibm**;com.intellij**;com.jcabi**;org.mockito**;com.opengamma**;com.typesafe**;com.zaxxer**;de.javakaffee**;groovy**;groovyjarjarantlr**;groovyjarjarasm**;io.atomix**;io.github**;io.netty**;jdk**;kotlin**;net.bytebuddy**;org.apache**;org.bouncycastle**;org.codehaus**;org.crsh**;org.dom4j**;org.fusesource**;org.h2**;org.hibernate**;org.jboss**;org.jcp**;org.joda**;org.objectweb**;org.objenesis**;org.slf4j**;org.w3c**;org.xml**;org.yaml**;reflectasm**;rx**;org.jolokia**;com.lmax**;picocli**;liquibase**;com.github.benmanes**;org.json**;org.postgresql**;nonapi.io.github.classgraph**;io.opentelemetry**)"
        def quasarClassLoaderExclusion = "l(net.corda.core.serialization.internal.**)"
        def quasarOptions = "m"
        javaAgents = quasar_classifier ? ["quasar-core-${quasar_version}-${quasar_classifier}.jar=${quasarOptions}${quasarExcludeExpression}${quasarClassLoaderExclusion}"] : ["quasar-core-${quasar_version}.jar=${quasarExcludeExpression}${quasarClassLoaderExclusion}"]
        systemProperties['visualvm.display.name'] = 'Corda'
        caplets = ['CordaCaplet']

        // JVM configuration:
        // - Constrain to small heap sizes to ease development on low end devices.
        // NOTE: these can be overridden in node.conf.
        //
        // If you change these flags, please also update Driver.kt
        jvmArgs = ['-Xmx512m']
        jvmArgs += ['-Djdk.attach.allowAttachSelf=true']
    }
}

tasks.whenTaskAdded { task ->
    if (task.name.contains("generateMetadataFileForCordaJARPublication")) {
        task.enabled = false
    }
}

javadoc.dependsOn ':testing:testserver:testcapsule:buildWebserverJar'
javadoc.dependsOn buildCordaJAR
compileTestJava.dependsOn ':testing:testserver:testcapsule:buildWebserverJar'
compileTestJava.dependsOn buildCordaJAR

artifacts {
    runtimeArtifacts buildCordaJAR
}

publishing {
    publications {
        cordaJAR(MavenPublication) {
            artifactId 'corda'
            artifact(buildCordaJAR)
            artifact(javadocJar)
            artifact(sourcesJar)
        }
    }
}
