<%#
 Copyright 2013-2017 the original author or authors from the JBooter project.

 This file is part of the JBooter project, see https://jbooter.github.io/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import groovy.json.JsonSlurper
import org.gradle.internal.os.OperatingSystem

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        maven { url "http://repo.spring.io/plugins-release" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.1"
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.9"
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${spring_boot_version}"
        classpath "org.springframework.build.gradle:propdeps-plugin:0.0.7"
        <%_ if (!skipClient) { _%>
        classpath "com.moowork.gradle:gradle-node-plugin:1.1.1"
        <%_ } _%>
        classpath "io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE"
        //jbooter-needle-gradle-buildscript-dependency - JBooter will add additional gradle build script plugins here
    }
}

apply plugin: 'java'
sourceCompatibility=1.<%= javaVersion %>
targetCompatibility=1.<%= javaVersion %>
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'propdeps'
<%_ if (!skipClient) { _%>
apply plugin: 'com.moowork.node'
    <%_ if (clientFramework === 'angular1') { _%>
apply plugin: 'com.moowork.gulp'
    <%_ } _%>
<%_ } _%>
apply plugin: 'io.spring.dependency-management'
<%_ if (serviceDiscoveryType || applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa' || messageBroker === 'kafka') { _%>
dependencyManagement {
  imports {
    <%_ if (messageBroker === 'kafka') { _%>
    mavenBom 'org.springframework.cloud:spring-cloud-stream-dependencies:' + spring_cloud_stream_version
    <%_ } _%>
    <%_ if (serviceDiscoveryType || applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa') { _%>
    mavenBom 'org.springframework.cloud:spring-cloud-dependencies:' + spring_cloud_version
    <%_ } _%>
  }
}
<%_ } _%>
defaultTasks 'bootRun'

bootRepackage {
   mainClass = '<%= packageName %>.<%= mainClass %>'
}

war {

}

springBoot {
    mainClass = '<%= packageName %>.<%= mainClass %>'
    executable = true
    buildInfo()
}

if (OperatingSystem.current().isWindows()) {
    task pathingJar(type: Jar) {
        dependsOn configurations.runtime
        appendix = 'pathing'

        doFirst {
            manifest {
                attributes 'Class-Path': configurations.runtime.files.collect {
                    it.toURL().toString().replaceFirst(/file:\/+/, '/').replaceAll(' ', '%20')
                }.join(' ')
            }
        }
    }

    bootRun {
        addResources = false
        dependsOn pathingJar
        doFirst {
            classpath = files("$buildDir/classes/main", "$buildDir/resources/main", pathingJar.archivePath)
        }
    }
} else {
    bootRun {
        addResources = false
    }
}

test {
    include '**/*UnitTest*'
    include '**/*IntTest*'

    // uncomment if the tests reports are not generated
    // see https://github.com/jbooter/generator-jbooter/pull/2771 and https://github.com/jbooter/generator-jbooter/pull/4484
    // ignoreFailures true
    reports.html.enabled = false
}
<%_ if (cucumberTests) { _%>

task cucumberTest(type: Test) {
    include '**/CucumberTest*'

    // uncomment if the tests reports are not generated
    // see https://github.com/jbooter/generator-jbooter/pull/2771 and https://github.com/jbooter/generator-jbooter/pull/4484
    // ignoreFailures true
    reports.html.enabled = false
}

test.finalizedBy(cucumberTest)
<%_ } _%>

task testReport(type: TestReport) {
    destinationDir = file("$buildDir/reports/tests")
    reportOn test
    <%_ if (cucumberTests) { _%>
    reportOn cucumberTest
    <%_ } _%>
}
<%_ if (cucumberTests) { _%>

cucumberTest.finalizedBy(testReport)
<%_ } _%>

<%_ if (!skipClient) { _%>
    <%_ if (clientFramework === 'angular1') { _%>
apply from: 'gradle/yeoman.gradle'
    <%_ } _%>
<%_ } _%>
apply from: 'gradle/sonar.gradle'
<%_ if (databaseType === 'sql') { _%>
apply from: 'gradle/liquibase.gradle'
<%_ } _%>
<%_ if (gatlingTests) { _%>
apply from: 'gradle/gatling.gradle'
<%_ } _%>
apply from: 'gradle/mapstruct.gradle'
apply from: 'gradle/docker.gradle'
//jbooter-needle-gradle-apply-from - JBooter will add additional gradle scripts to be applied here

if (project.hasProperty('prod')) {
    apply from: 'gradle/profile_prod.gradle'
} else {
    apply from: 'gradle/profile_dev.gradle'
}

if (project.hasProperty('graphite')) {
    apply from: 'gradle/graphite.gradle'
}

if (project.hasProperty('prometheus')) {
    apply from: 'gradle/prometheus.gradle'
}
<%_ if (serviceDiscoveryType || applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa') { _%>

if (project.hasProperty('zipkin')) {
    apply from: 'gradle/zipkin.gradle'
}
<%_ } _%>

group = '<%= packageName %>'
version = '0.0.1-SNAPSHOT'

description = ''

configurations {
    providedRuntime
    compile.exclude module: "spring-boot-starter-tomcat"
    <%_ if (applicationType === 'microservice' || applicationType === 'gateway' || applicationType === 'uaa') { _%>
    // netty's native is pulled by spring-cloud-starter-ribbon, but is useless unless you explicitly add the native binary dependency.
    // Having it in the classpath without the binary can cause warnings
    all*.exclude group: 'io.netty', module: 'netty-transport-native-epoll'
    <%_ } _%>
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
    maven { url 'http://repo.spring.io/milestone' }
    maven { url 'http://repo.spring.io/snapshot' }
    maven { url 'https://repository.jboss.org/nexus/content/repositories/releases' }
    maven { url 'https://oss.sonatype.org/content/repositories/releases' }
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    maven { url 'http://repo.maven.apache.org/maven2' }
    <%_ if (devDatabaseType === 'oracle' || prodDatabaseType === 'oracle') { _%>
    maven { url "https://maven.oracle.com" }
    <%_ } _%>
}

dependencies {
    compile "io.github.jbooter:jbooter:${jbooter_server_version}"
    compile "io.dropwizard.metrics:metrics-core:${dropwizard_metrics_version}"
    compile "io.dropwizard.metrics:metrics-jcache:${dropwizard_metrics_version}"
    compile "io.dropwizard.metrics:metrics-jvm:${dropwizard_metrics_version}"
    compile "io.dropwizard.metrics:metrics-servlet:${dropwizard_metrics_version}"
    compile "io.dropwizard.metrics:metrics-json:${dropwizard_metrics_version}"
    compile "io.dropwizard.metrics:metrics-servlets:${dropwizard_metrics_version}"
    compile ("net.logstash.logback:logstash-logback-encoder:${logstash_logback_encoder_version}") {
        exclude(group: 'ch.qos.logback')
    }
    compile "com.fasterxml.jackson.datatype:jackson-datatype-json-org"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-hppc"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
    <%_ if (databaseType === 'sql') { _%>
    compile "com.fasterxml.jackson.datatype:jackson-datatype-hibernate5"
    <%_ } _%>
    compile "com.fasterxml.jackson.core:jackson-annotations"
    compile "com.fasterxml.jackson.core:jackson-databind"
    compile "com.fasterxml.jackson.module:jackson-module-afterburner"
    compile ("com.ryantenney.metrics:metrics-spring:${metrics_spring_version}")
    <%_ if (hibernateCache === 'hazelcast') { _%>
    compile "com.hazelcast:hazelcast"
    compile "com.hazelcast:hazelcast-hibernate52:${hazelcast_hibernate52_version}"
    compile "com.hazelcast:hazelcast-spring"
    <%_ } _%>
    <%_ if ((clusteredHttpSession === 'hazelcast' || applicationType === 'gateway') && hibernateCache != 'hazelcast') { _%>
    compile "com.hazelcast:hazelcast"
    compile "com.hazelcast:hazelcast-spring"
    <%_ } _%>
    <%_ if (clusteredHttpSession === 'hazelcast') { _%>
    compile "com.hazelcast:hazelcast-wm:${hazelcast_wm_version}"
    <%_ } _%>
    <%_ if (hibernateCache === 'ehcache' || hibernateCache === 'hazelcast' || applicationType === 'gateway') { _%>
    compile "javax.cache:cache-api"
    <%_ } _%>
    <%_ if (databaseType === 'sql') { _%>
    compile "org.hibernate:hibernate-core:${hibernate_version}"
    compile ("com.zaxxer:HikariCP:${hikaricp_version}")
    <%_ } _%>
    <%_ if (databaseType === 'cassandra' || applicationType === 'gateway') { _%>
    compile "commons-codec:commons-codec"
    <%_ } _%>
    compile "org.apache.commons:commons-lang3:${commons_lang_version}"
    compile "commons-io:commons-io:${commons_io_version}"
    compile "javax.transaction:javax.transaction-api"
    <%_ if (databaseType === 'cassandra' || applicationType === 'gateway') { _%>
    compile "net.jpountz.lz4:lz4:${lz4_version}"
    <%_ } _%>
    <%_ if (hibernateCache === 'ehcache' && databaseType === 'sql') { _%>
    compile "org.ehcache:ehcache"
    compile "org.hibernate:hibernate-jcache:${hibernate_version}"
    <%_ } _%>
    <%_ if (databaseType === 'sql') { _%>
    compile "org.hibernate:hibernate-envers"
    compile "org.hibernate:hibernate-validator"
    compile ("org.liquibase:liquibase-core")
    compile "com.mattbertolini:liquibase-slf4j:${liquibase_slf4j_version}"
    <%_ } _%>
    compile "org.springframework.boot:spring-boot-actuator"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.springframework.boot:spring-boot-loader-tools"
    compile "org.springframework.boot:spring-boot-starter-mail"
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-starter-aop"
    <%_ if (databaseType === 'sql') { _%>
    compile "org.springframework.boot:spring-boot-starter-data-jpa"
    <%_ } _%>
    <%_ if (searchEngine === 'elasticsearch') { _%>
    compile "org.springframework.boot:spring-boot-starter-data-elasticsearch"
    // needed to get around elasticsearch stacktrace about jna not found
    // https://github.com/elastic/elasticsearch/issues/13245
    compile "net.java.dev.jna:jna"
    <%_ } _%>
    <%_ if (databaseType === 'mongodb') { _%>
    compile "org.springframework.boot:spring-boot-starter-data-mongodb"
    <%_ } _%>
    <%_ if (messageBroker === 'kafka') { _%>
    compile "org.springframework.cloud:spring-cloud-stream"
    compile "org.springframework.cloud:spring-cloud-stream-binder-kafka"
    compile "org.springframework.kafka:spring-kafka:${spring_kafka_version}"
    <%_ } _%>
    compile "org.springframework.boot:spring-boot-starter-security"
    compile ("org.springframework.boot:spring-boot-starter-web") {
        exclude module: 'spring-boot-starter-tomcat'
    }
    compile "org.springframework.boot:spring-boot-starter-undertow"
    <%_ if (websocket === 'spring-websocket') { _%>
    compile "org.springframework.boot:spring-boot-starter-websocket"
    <%_ } _%>
    compile "org.springframework.boot:spring-boot-starter-thymeleaf"
    <%_ if (databaseType === 'cassandra' || applicationType === 'gateway') { _%>
    compile "com.datastax.cassandra:cassandra-driver-extras:" + dependencyManagement.managedVersions["com.datastax.cassandra:cassandra-driver-core"]
    compile "com.datastax.cassandra:cassandra-driver-mapping"
    <%_ } _%>
    <%_ if (applicationType === 'gateway') { _%>
    compile "org.springframework.cloud:spring-cloud-starter-zuul"
    compile "com.github.vladimir-bukhtoyarov:bucket4j-core:${bucket4j_version}"
    compile "com.github.vladimir-bukhtoyarov:bucket4j-jcache:${bucket4j_version}"
    <%_ } _%>
    <%_ if (applicationType === 'microservice' || applicationType === 'gateway' || applicationType === 'uaa') { _%>
    compile "org.springframework.cloud:spring-cloud-starter"
    compile "org.springframework.cloud:spring-cloud-starter-ribbon"
    compile "org.springframework.cloud:spring-cloud-starter-hystrix"
    compile "org.springframework.cloud:spring-cloud-starter-spectator"
    compile "org.springframework.retry:spring-retry"
    <%_ } _%>
    <%_ if (serviceDiscoveryType === 'eureka') { _%>
    compile "org.springframework.cloud:spring-cloud-starter-eureka"
    compile "org.springframework.cloud:spring-cloud-starter-config"
    <%_ } _%>
    <%_ if (serviceDiscoveryType === 'consul') { _%>
    compile "org.springframework.cloud:spring-cloud-starter-consul-discovery"
    compile "org.springframework.cloud:spring-cloud-starter-consul-config"
    <%_ } _%>
    <%_ if (authenticationType === 'uaa') { _%>
    compile "org.springframework.cloud:spring-cloud-security"
    <%_ } _%>
    <%_ if (applicationType === 'microservice' || applicationType === 'gateway' || applicationType === 'uaa') { _%>
    compile "org.springframework.cloud:spring-cloud-starter-feign"
    <%_ } _%>
    compile "org.springframework.boot:spring-boot-starter-cloud-connectors"
    compile ("org.springframework:spring-context-support")
    compile "org.springframework.security:spring-security-config"
    compile "org.springframework.security:spring-security-data"
    compile "org.springframework.security:spring-security-web"
    <%_ if (websocket === 'spring-websocket') { _%>
    compile "org.springframework.security:spring-security-messaging"
    <%_ } _%>
    <%_ if (authenticationType === 'oauth2') { _%>
    compile "org.springframework.security.oauth:spring-security-oauth2:${spring_security_oauth2_version}"
    <%_ } _%>
    <%_ if (authenticationType === 'jwt') { _%>
    compile "io.jsonwebtoken:jjwt:${jjwt_version}"
    <%_ } _%>
    <%_ if (authenticationType === 'uaa') { _%>
    compile "org.springframework.security.oauth:spring-security-oauth2:${spring_security_oauth2_version}"
    compile "org.springframework.security:spring-security-jwt"
    <%_ } _%>
    <%_ if (databaseType === 'mongodb') { _%>
    compile "com.github.mongobee:mongobee:${mongobee_version}"
    <%_ } _%>
    compile ("io.springfox:springfox-swagger2:${springfox_version}") {
        exclude module: 'mapstruct'
    }
    compile "io.springfox:springfox-bean-validators:${springfox_version}"
    <%_ if (devDatabaseType === 'mysql' || prodDatabaseType === 'mysql') { _%>
    compile "mysql:mysql-connector-java"
    <%_ } _%>
    <%_ if (devDatabaseType === 'postgresql' || prodDatabaseType === 'postgresql') { _%>
    compile "org.postgresql:postgresql"
    <%_ } _%>
    <%_ if (devDatabaseType === 'mariadb' || prodDatabaseType === 'mariadb') { _%>
    compile "org.mariadb.jdbc:mariadb-java-client"
    <%_ } _%>
    <%_ if (devDatabaseType === 'h2Disk' || devDatabaseType === 'h2Memory') { _%>
    compile "com.h2database:h2"
    <%_ } _%>
    <%_ if (devDatabaseType === 'mssql' || prodDatabaseType === 'mssql') { _%>
    compile "com.microsoft.sqlserver:mssql-jdbc:${mssql_jdbc_version}"
    compile "com.github.sabomichal:liquibase-mssql:${liquibase_mssql_version}"
    <%_ } _%>
    compile "org.mapstruct:mapstruct-jdk8:${mapstruct_version}"
    <%_ if (enableSocialSignIn) { _%>
    compile "org.apache.httpcomponents:httpclient"
    compile "org.springframework.social:spring-social-security"
    compile "org.springframework.social:spring-social-google:${spring_social_google_version}"
    compile "org.springframework.social:spring-social-facebook"
    compile "org.springframework.social:spring-social-twitter"
    <%_ } _%>
    testCompile "org.awaitility:awaitility:${awaitility_version}"
    testCompile "com.jayway.jsonpath:json-path"
    <%_ if (databaseType === 'cassandra') { _%>
    testCompile ("org.cassandraunit:cassandra-unit-spring:${cassandra_unit_spring_version}") {
        exclude(group: 'org.slf4j')
    }
    <%_ } _%>
    <%_ if (cucumberTests) { _%>
    testCompile "info.cukes:cucumber-junit:${cucumber_version}"
    testCompile "info.cukes:cucumber-spring:${cucumber_version}"
    <%_ } _%>
    testCompile "org.springframework.boot:spring-boot-starter-test"
    testCompile "org.springframework.security:spring-security-test"
    testCompile "org.springframework.boot:spring-boot-test"
    testCompile "org.assertj:assertj-core:${assertj_version}"
    testCompile "junit:junit"
    testCompile "org.mockito:mockito-core"
    <%_ if (databaseType === 'sql') { _%>
    testCompile "com.mattbertolini:liquibase-slf4j:${liquibase_slf4j_version}"
    <%_ } _%>
    <%_ if (databaseType === 'mongodb') { _%>
    testCompile "de.flapdoodle.embed:de.flapdoodle.embed.mongo"
    <%_ } _%>
    testCompile "org.hamcrest:hamcrest-library"
    <%_ if (gatlingTests) { _%>
    testCompile "io.gatling.highcharts:gatling-charts-highcharts:${gatling_version}"
    <%_ } _%>
    <%_ if (devDatabaseType != 'h2Disk' && devDatabaseType != 'h2Memory') { _%>
    testCompile "com.h2database:h2"
    <%_ } _%>
    <%_ if (devDatabaseType === 'oracle' || prodDatabaseType === 'oracle') { _%>
    // more information at https://blogs.oracle.com/dev2dev/entry/how_to_get_oracle_jdbc
    compile "com.oracle.jdbc:ojdbc7:${oracle_version}"
    <%_ } _%>
    <%_ if (messageBroker === 'kafka') { _%>
    testCompile "org.springframework.cloud:spring-cloud-stream-test-support"
    <%_ } _%>
    optional "org.springframework.boot:spring-boot-configuration-processor:${spring_boot_version}"
    //jbooter-needle-gradle-dependency - JBooter will add additional dependencies here
}

clean {
    delete "target"
}

task cleanResources(type: Delete) {
    delete 'build/resources'
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.5'
}

task stage(dependsOn: 'bootRepackage') {
}
<%_ if (!skipClient) { _%>

if (project.hasProperty('nodeInstall')) {
    node {
        version = "${node_version}"
        npmVersion = "${npm_version}"
        yarnVersion = "${yarn_version}"
        download = true
    }
}
<%_ } _%>

compileJava.dependsOn processResources
processResources.dependsOn cleanResources,bootBuildInfo
bootBuildInfo.mustRunAfter cleanResources
