plugins {
    id 'java'
    id 'idea'
    id 'java-library'
    id 'org.ajoberstar.grgit' version '4.0.1'
    id 'com.github.sherter.google-java-format' version '0.8'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'

repositories {
    mavenCentral()
    maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

//configurations.all.resolutionStrategy {
//    // 版本冲突时直接构建失败
//    // failOnVersionConflict()
//
//    // 缓存有效时间
//    // cacheChangingModulesFor 0,'seconds'
//}

googleJavaFormat {
    options style: 'AOSP'
    source = sourceSets*.allJava
    include '**/*.java'
    exclude '**/temp/*.java'
}

verifyGoogleJavaFormat {
    source = sourceSets*.allJava
    include '**/*.java'
    exclude '**/temp/*.java'
}

def spring_version = "4.3.27.RELEASE"

List logger = [
        'org.slf4j:slf4j-log4j12:1.7.30'
        //'org.slf4j:slf4j-api:1.7.30'
]

List spring = [
        "org.springframework:spring-core:$spring_version",
        "org.springframework:spring-beans:$spring_version",
        "org.springframework:spring-context:$spring_version",
        "org.springframework:spring-tx:$spring_version",
]

configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}


// In this section you declare the dependencies for your production and test code
dependencies {

    compile spring, logger
    compile 'org.apache.commons:commons-lang3:3.1'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'

    // compile 'io.netty:netty-tcnative:2.0.25.Final'
    // compile 'io.netty:netty-tcnative-boringssl-static:2.0.27.Final'
    // compile 'org.fisco-bcos:netty-sm-ssl-context:1.0.0-SNAPSHOT'
    compile ('org.fisco-bcos:netty-sm-ssl-context:1.5.2') {
        exclude module:"log4j"
    }

    // compile 'org.ethereum:solcJ-all:0.4.25'
    compile 'com.google.guava:guava:29.0-jre'
    compile 'commons-configuration:commons-configuration:1.10'
    compile 'org.apache.httpcomponents:httpclient:4.5.5'
    compile 'org.bouncycastle:bcprov-jdk15on:1.60'
    compile 'org.apache.commons:commons-collections4:4.4'
    compile 'commons-io:commons-io:2.4'
    compile 'org.projectlombok:lombok:1.18.2'
    compile 'com.lambdaworks:scrypt:1.4.0'
    compile 'com.squareup:javapoet:1.7.0'
    compile 'io.reactivex:rxjava:1.2.4'
    compile 'io.reactivex.rxjava2:rxjava:2.2.2'
    compile 'com.github.jnr:jnr-unixsocket:0.15'
    compile 'info.picocli:picocli:3.6.0'
    compile 'org.java-websocket:Java-WebSocket:1.5.1'

    compile 'com.github.stefanbirkner:system-rules:1.18.0'
    compile 'de.vandermeer:asciitable:0.3.2'

    compile 'junit:junit:4.12'
    compile 'org.mockito:mockito-core:2.23.0'

    integrationTestCompile 'junit:junit:4.12'
    integrationTestCompile 'org.mockito:mockito-core:2.23.0'
}

archivesBaseName = 'web3sdk'

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'src/test/java'
        }

        resources {
            srcDir 'src/test/resources'
        }
    }
    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/integration-test/java')
        }
        resources.srcDir file('src/integration-test/resources')
    }
}

task integrationTest(type: Test) {
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    outputs.upToDateWhen { false }
}

// clean dist and log dir
task cleanBuildArtifacts(type: Delete) {
    delete 'dist'
    delete 'log'
    delete 'out'
}

tasks.clean.dependsOn(tasks.cleanBuildArtifacts)

check.dependsOn integrationTest
integrationTest.mustRunAfter test

//check.dependsOn.remove(test)
//check.dependsOn.remove(integrationTest)
//check.dependsOn.remove(verifyGoogleJavaFormat)

jar {
    // destinationDir file('dist/apps')
    // archiveName project.name + '-' + project.version + '.jar'
    exclude '**/*.xml'
    exclude '**/*.properties'

    manifest {
        try {
            def repo = grgit.open()
            if (repo != null) {
                def user = System.getProperty("user.name")
                def date = new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
                def branch = repo.branch.getCurrent().getName()
                def commit = repo.head().getAbbreviatedId(40)

                attributes(["Built-By"                : user,
                            "Implementation-Timestamp": date,
                            "Git-Branch"              : branch,
                            "Git-Commit"              : commit
                ])

                logger.info("  Commit : ")
                logger.info("  => user: {}", user)
                logger.info("  => date: {}", date)
                logger.info("  => branch: {}", branch)
                logger.info("  => commit: {}", commit)
            }
        } catch (Exception e) {
            // logger.warn(' .git not exist, cannot found commit info')
        }
    } from sourceSets.main.output

    doLast {
        copy {
            from destinationDir
            into 'dist/apps'
        }
        copy {
            from configurations.runtime
            into 'dist/lib'
        }
        copy {
            from file('src/test/resources/')
            into 'dist/conf'
        }
    }
}

test {
    testLogging {
        showStandardStreams = true
        // events "started","passed","skipped","failed"
        events "passed","skipped","failed"
    }
}
