///////////////////////////////////////////////////////////////////////////////////////
//
//                 Gradle Template Summary
//
//   Frequently Used Command:
//      [ gradle ]          Default task. same with 'gradle build'.
//      [ gradle build ]    Compile/test your code, and create a war file containing your main classes and resources.
//
////////////////////////////////////////////////////////////////////////////////////////

//Definition apply plugin
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'

//--------------------------------- Based setting  for current project ---------------------------------

// Definition sourceCompatibility and targetCompatibility.
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

buildscript {
    ext {
        MODULE                      = "goodutils"
        MAVEN_GROUP_ID              = "goodutils"
        MAVEN_ARTIFACT_ID           = "goodutils"
        MAVEN_VERSION               = "0.1"
    }

    repositories {
        mavenCentral()
    }

    configurations.all {
        resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
    }
}

// Access maven center.
repositories {
    mavenCentral()
}


// Definition source which should include.
sourceSets {
    main {
        java {
            srcDir 'src'
        }
        resources {
            srcDir 'src'
        }
    }

    test {
        java {
            srcDir 'test'
        }
    }
}


//--------------------------------- Dependencies definition ---------------------------------

dependencies {
    compile "org.bouncycastle:bcprov-jdk16:1.46"
    //compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.13'
    compile "com.google.guava:guava:18.0"
    compile "commons-io:commons-io:2.2"
    compile "commons-cli:commons-cli:1.4"
    compile "commons-lang:commons-lang:2.6"
    compile "org.jsoup:jsoup:1.11.2"
    compile "org.apache.httpcomponents:httpclient:4.5.5"
    compile "org.apache.logging.log4j:log4j-api:2.5"
    compile "org.apache.logging.log4j:log4j-core:2.5"
    // https://mvnrepository.com/artifact/dom4j/dom4j
    compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
    compile group: 'org.json', name: 'json', version: '20180130'
    compile 'com.github.javaparser:javaparser-symbol-solver-core:3.6.5'
    compile group: 'com.sun.tools.btrace', name: 'btrace-boot', version: '1.2.3'
    compile group: 'com.sun.tools.btrace', name: 'btrace-agent', version: '1.2.3'
    compile group: 'com.sun.tools.btrace', name: 'btrace-client', version: '1.2.3'
    compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
    compile "org.apache.httpcomponents:httpasyncclient:4.1.4"
    compile "org.apache.httpcomponents:httpcore-nio:4.4.10"
    // Include non maven standard layout file into war
    testCompile(
            "junit:junit:4.11",
    )
}

// Define manifest
ext.sharedManifest = manifest {
    attributes(
            'Modlue': "${MODULE}",
            'Implementation-Title': "${project.name}",
            'Built-Date': new Date().getDateTimeString(),
            'Built-With': "gradle-${project.getGradle().getGradleVersion()}, groovy-${GroovySystem.getVersion()}",
            'Created-By': 'Java ' + System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')'
    )
}


//--------------------------------- Specific Tasks ---------------------------------
/*
*  Override jar task addition attributes
*/
jar {
    manifest = project.manifest {
        from sharedManifest
    }
}

task printDependencies << {
    String projectName =  project.getProperties().get("name")
    BufferedWriter bw = null
    try {
        File file = new File(projectName + "-dependencies.txt")
        bw = new BufferedWriter(new FileWriter(file))
        configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { artifact ->
            def id = artifact.moduleVersion.id
            bw.writeLine(String.format("%-30s\t%-30s\t%s", id.group, id.name, id.version))
            println "group: ${id.group}, name: ${id.name}, version: ${id.version}"
        }
        println "Dependencies written to " + file.getCanonicalPath()
    }
    finally {
        if (bw != null) {
            bw.close()
        }
    }

}

/**
 * fatjar demo
 */
task fatjar(type: Jar) {

    manifest {
        attributes 'Implementation-Title': project.name,
                'Implementation-Version': '1.0'
    }
    baseName = project.name
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}



defaultTasks 'clean','test','jar'

task build (overwrite: true ) {
    dependsOn = ['clean','test','jar']

    test.mustRunAfter clean
    jar.mustRunAfter test
}

task devbuild {
    dependsOn = ['clean','jar']

    jar.mustRunAfter clean
}

//Define the test task
test {
    ignoreFailures = true
    reports.html.destination = file("$buildDir/test-reports")
}


task createJavaProject << {
    sourceSets*.java.srcDirs*.each { it.mkdirs() }
    sourceSets*.resources.srcDirs*.each { it.mkdirs()}
}

