import java.text.SimpleDateFormat

node {
    def mvnHome
    stage('Preparation') { // for display purposes
        git([url: 'https://code.sdsdev.co.kr/SDL/Nonsan.git', credentialsId: 'sdlsupport', branch: '5.2.1'])
        mvnHome = tool 'M3'
        def dateFormat = new SimpleDateFormat("yyyy-MM-dd")
        def date = new Date()
        today = dateFormat.format(date)
        pomVersion = bat(script: 'mvn help:evaluate -Dexpression=project.version -q -DforceStdout', returnStdout: true).trim()
        pomVersion = pomVersion.readLines().drop(1).join(" ")

        echo 'project.version=' + pomVersion
        echo 'today=' + today
    }
    environment {
        DATE = today
    }
    stage('Directory Clean') {
        bat("start cmd.exe /c rmdir /s/q %USERPROFILE%\\.m2\\repository\\sdl")
        bat("start cmd.exe /c rmdir /s/q D:\\deploy\\sdl-5.0.0\\sdl-base")
        bat("start cmd.exe /c rmdir /s/q D:\\deploy\\sdl-5.0.0\\sdl-sql")
        bat('start cmd.exe /c xcopy sdl-base D:\\deploy\\sdl-5.0.0\\sdl-base\\ /s /e /d /y')
        bat('start cmd.exe /c xcopy sdl-sql D:\\deploy\\sdl-5.0.0\\sdl-sql\\ /s /e /d /y')
    }

    stage('lib deploy') {

        bat('mvn clean deploy -DaltDeploymentRepository=sdl-release::default::http://admin:admin123@70.118.68.230:8081/repository/sdl-release')

    }

    stage('Build') {
        dir('D:\\deploy\\sdl-5.0.0\\sdl-base'){
            bat('mvn compile dependency:sources -DincludeGroupIds=sdl')
            bat('start cmd.exe /c rmdir /s/q %USERPROFILE%\\.m2\\repository\\sdl\\sdl-base')
            bat('start cmd.exe /c xcopy %USERPROFILE%\\.m2\\repository\\sdl D:\\deploy\\sdl-5.0.0\\sdl-base\\source\\sdl\\ /s /e /d /y')
            bat('mvn clean compile dependency:sources -DincludeGroupIds=sdl')
            bat("start cmd.exe /c rmdir /s/q D:\\deploy\\sdl-5.0.0\\sdl-base\\target")
        }
    }
    stage('Packaging') {
        dir('D:\\deploy\\sdl-5.0.0'){
            bat('start cmd.exe /c bc c D:\\deploy\\sdl-5.0.0\\sdl-base-'+pomVersion+'-'+today+'.zip sdl-base')
            bat('start cmd.exe /c bc c D:\\deploy\\sdl-5.0.0\\sdl-ddl-'+pomVersion+'-'+today+'.zip sdl-sql')
        }
    }
    stage('Maven Package') {
        dir('D:\\deploy\\sdl-5.0.0\\sdl-base'){
            bat('mvn clean package')
        }
    }
    stage('Success') {
        echo 'project.version=' + pomVersion
        echo 'today=' + today
    }
}