/*
* Build file for authorization server
*/
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
apply plugin: 'docker'
apply plugin: 'eclipse'

buildscript {
	project.ext {
         springBootVersion = '1.5.6.RELEASE'
         jarName = 'basic-auth-server'
         versionName = '0.0.1'
         gradleDockerVersion = '1.2'
         swagger2version = '2.7.0'
	}
    repositories {
        jcenter()
        mavenCentral()
    }
    
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.springBootVersion}"
        classpath "se.transmode.gradle:gradle-docker:${project.gradleDockerVersion}"
    }
}

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

// Used by the Docker gradle plugin, group refers to the account under which the docker image is created
group = 'anilallewar'
mainClassName = 'com.anilallewar.microservices.auth.AuthServerApplication'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

ext {
	springCloudVersion = 'Dalston.SR3'
}

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

dependencies {
    // Basic Spring boot with config client
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile("org.springframework.boot:spring-boot-starter-web")    
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-freemarker")
    
    // Spring OAuth2 security
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.security.oauth:spring-security-oauth2")
    compile("org.springframework.security:spring-security-jwt")
    
    // Eureka client
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    
    // JPA for persisting user data
	compile("org.springframework.boot:spring-boot-starter-data-jpa")
	compile("mysql:mysql-connector-java:8.0.13")
	
	// Swagger for API testing
	compile("io.springfox:springfox-swagger2:${swagger2version}")
	compile("io.springfox:springfox-swagger-ui:${swagger2version}")
}

jar {
    baseName = "${project.jarName}"
    version =  "${project.versionName}"
}

/*
* This task builds the docker image by copying the output of the "jar" gradle command
* and moving it to the "build/docker" directory which is used as the staging directory 
* by the docker gradle plugin. We then build the docker image by invoking the appropriate
* Dockerfile.  
*/
task buildDocker(type: Docker, dependsOn: build) {
  push = false
  applicationName = jar.baseName
  tagVersion = jar.version
  dockerfile = file('src/main/docker/Dockerfile')
  doFirst {
    // Copy the built fat jar to the Docker plugin staging directory
    copy {
      from jar
      into stageDir
    }
    // Copy the run.sh file to the Docker plugin staging directory
    copy {
      from "${project.buildDir}/resources/main/run.sh"
      into stageDir
    }
  }
}

run {
    jvmArgs = ['-Xdebug', '-Xrunjdwp:server=y,transport=dt_socket,address=4100,suspend=n','-Dspring.profiles.active=default']
}
