tasks.gradle 10.4 KB
Newer Older
1 2 3 4
import org.apache.ivy.util.url.*
import org.apache.tools.ant.taskdefs.condition.Os
import java.nio.file.*
import java.util.*
Misagh Moayyed's avatar
Misagh Moayyed committed
5

6 7 8 9 10 11 12 13 14
afterEvaluate { project ->
    println "******************************************************************"
    println "Apereo CAS $casServerVersion"
    println "Enterprise Single SignOn for all earthlings and beyond"
    println "- https://github.com/apereo/cas"
    println "- https://apereo.github.io/cas"
    println "******************************************************************"
}

Misagh Moayyed's avatar
Misagh Moayyed committed
15 16 17 18 19 20 21 22 23 24 25
buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "org.apache.ivy:ivy:${project.ivyVersion}"
    }
}

26 27
apply plugin: "de.undercouch.download"
apply plugin: "com.boazj.log"
28

29
def tomcatDirectory = "${buildDir}/apache-tomcat-${tomcatVersion}"
30 31 32
project.ext."tomcatDirectory" = tomcatDirectory

def explodedDir="${buildDir}/cas"
33 34

task copyCasConfiguration(type: Copy, group: "build", description: "Copy the CAS configuration from this project to /etc/cas/config") {
Misagh Moayyed's avatar
Misagh Moayyed committed
35 36 37 38 39 40 41
    from "etc/cas/config"
    into "/etc/cas/config"
    doFirst {
        mkdir "/etc/cas/config"
    }
}

42
task copyWebAppIntoJib(type: Copy, group: "Docker", description: "Copy the web application into Docker image") {
Misagh Moayyed's avatar
Misagh Moayyed committed
43 44 45 46 47
    dependsOn build
    from "build/libs/${casWebApplicationBinaryName}"
    into "src/main/jib/docker/cas/war"
}

48
task copyConfigIntoJib(type: Copy, group: "Docker", description: "Copy the CAS configuration into Docker image") {
Misagh Moayyed's avatar
Misagh Moayyed committed
49 50 51 52 53 54 55 56 57 58 59 60
    dependsOn build
    from "etc/cas"
    into "src/main/jib/docker/cas"
}

task deleteWebAppFromJib(type: Delete, group: "Docker", description: "Explodes the CAS web application archive") {
    delete "src/main/jib/docker/cas"
}

task explodeWar(type: Copy, group: "build", description: "Explodes the CAS web application archive") {
    dependsOn build
    from zipTree("build/libs/${casWebApplicationBinaryName}")
61
    into explodedDir
Misagh Moayyed's avatar
Misagh Moayyed committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
}

task run(group: "build", description: "Run the CAS web application in embedded container mode") {
    dependsOn build
    doLast {
        def casRunArgs = Arrays.asList("-Xmx2048M -XX:+TieredCompilation -XX:TieredStopAtLevel=1".split(" "))
        javaexec {
            main = "-jar"
            jvmArgs = casRunArgs
            args = ["build/libs/${casWebApplicationBinaryName}"]
            logger.info "Started ${commandLine}"
        }
    }
}

task setExecutable(group: "build", description: "Configure the project to run in executable mode") {
    doFirst {
        project.setProperty("executable", "true")
        logger.info "Configuring the project as executable"
    }
}

task executable(type:Exec, group: "build", description: "Run the CAS web application in standalone executable mode") {
    dependsOn setExecutable, build
    doFirst {
        workingDir "."
        if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine "chmod", "+x", bootWar.archivePath
        }
91
        logger.info "Running ${bootWar.archivePath}"
Misagh Moayyed's avatar
Misagh Moayyed committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        commandLine bootWar.archivePath
    }
}

task debug(group: "build", description: "Debug the CAS web application in embedded mode on port 5005") {
    dependsOn build
    doLast {
        logger.info "Debugging process is started in a suspended state, listening on port 5005."
        def casArgs = Arrays.asList("-Xmx2048M".split(" "))
        javaexec {
            main = "-jar"
            jvmArgs = casArgs
            debug = true
            args = ["build/libs/${casWebApplicationBinaryName}"]
            logger.info "Started ${commandLine}"
        }
    }
}

task downloadShell (group: "shell", description: "Download CAS shell jar from snapshot or release maven repo") {
    doFirst {
        mkdir "${project.shellDir}"
    }
    doLast {
        def downloadFile
        if (isRunningCasServerSnapshot(casServerVersion)) {
            def snapshotDir = "https://oss.sonatype.org/content/repositories/snapshots/org/apereo/cas/cas-server-support-shell/${casServerVersion}/"
119
            def files = new ApacheURLLister().listFiles(new URL(snapshotDir))
Misagh Moayyed's avatar
Misagh Moayyed committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
            files = files.sort{it.path}
            files.each {
                if (it.path.endsWith(".jar")) {
                    downloadFile = it
                }
            }
        } else {
            downloadFile = "https://repo1.maven.org/maven2/org/apereo/cas/cas-server-support-shell/${casServerVersion}/cas-server-support-shell-${casServerVersion}.jar"
        }
        logger.info "Downloading file: ${downloadFile}"
        download {
            src downloadFile
            dest new File("${project.shellDir}", "cas-server-support-shell-${casServerVersion}.jar")
            overwrite false
        }
    }
}

task runShell (group: "shell",  description: "Run the CAS shell") {
    dependsOn downloadShell
    doLast {
        def casShellArgs = Arrays.asList("-Xmx1500M".split(" "))
        javaexec {
            main = "-jar"
            jvmArgs = casShellArgs
            standardInput = System.in
            args = ["${project.shellDir}/cas-server-support-shell-${casServerVersion}.jar", "--shell"]
        }
    }
}

task debugShell (group: "shell", description: "Run the CAS shell with debug options, wait for debugger on port 5005") {
    dependsOn downloadShell
    doLast {
        def casArgs = Arrays.asList("-Xmx1500M ".split(" "))
        javaexec {
            main = "-jar"
            jvmArgs = casArgs
            debug = true
            standardInput = System.in
            args = ["${project.shellDir}/cas-server-support-shell-${casServerVersion}.jar", "--shell"]
        }
    }
}

task showConfiguration(group: "build", description: "Show configurations for each dependency, etc") {
    doLast() {
        def cfg = project.hasProperty("configuration") ? project.property("configuration") : "compile"
        configurations.getByName(cfg).each { println it }
    }
}

task allDependenciesInsight(group: "build", type: DependencyInsightReportTask, description: "Produce insight information for all dependencies") {}

task allDependencies(group: "build", type: DependencyReportTask, description: "Display a graph of all project dependencies") {}

task casVersion (group: "build", description: "Display the current CAS version") {
    doFirst { println casServerVersion }
}

task createKeystore (type:Exec, group: "build", description: "Create CAS keystore") {
    doFirst {
        mkdir "/etc/cas"
        workingDir "."

        def keystorePath = "/etc/cas/thekeystore"

        def dn = "CN=cas.example.org,OU=Example,OU=Org,C=US"
        if (project.hasProperty("certificateDn")) {
            dn = project.getProperty("certificateDn")
        }
	    def subjectAltName = "dns:example.org,dns:localhost,ip:127.0.0.1"
        if (project.hasProperty("certificateSubAltName")) {
            subjectAltName = project.getProperty("certificateSubAltName")
        }
        logger.info "Generating keystore for CAS with DN ${dn}"
        commandLine "keytool", "-genkeypair", "-alias", "cas",
            "-keyalg", "RSA",
            "-keypass", "changeit", "-storepass", "changeit",
            "-keystore", keystorePath,
            "-dname", dn, "-ext", "SAN=${subjectAltName}"

        logger.info "Exporting keystore..."
        commandLine "keytool", "-exportcert", "-alias", "cas",
            "-storepass", "changeit", "-keystore", keystorePath,
            "-file", "/etc/cas/cas.cer"
    }
}

task listTemplateViews (group: "build", description: "List all CAS views") {
    dependsOn explodeWar

    doFirst {
        fileTree(explodedDir).matching {
            include "**/*.html"
        }
        .collect { it.name }
        .toSorted()
        .each { println it }
    }
}

task getResource(group: "build", description: "Fetch a CAS resource and move it into the overlay") {
    dependsOn explodeWar

    doFirst {
        def resourceName = project.getProperty("resourceName")

        def results = fileTree(explodedDir).matching {
            include "**/${resourceName}.*"
        }
        if (results.isEmpty()) {
            println "No resources could be found matching ${resourceName}"
            return
        }
        if (results.size() > 1) {
            println "Multiple resources found matching ${resourceName}: ${results}"
            return
        }

        def fromFile = explodedDir + "/WEB-INF/classes"
        def resourcesDir = "src/main/resources"
        mkdir resourcesDir

        def resourceFile = results[0].canonicalPath
        def toResourceFile = resourceFile.replace(fromFile, resourcesDir)

        def parent = file(toResourceFile).getParent()
        mkdir parent

        Files.copy(Paths.get(resourceFile), Paths.get(toResourceFile), StandardCopyOption.REPLACE_EXISTING)
        println "Copied file to ${toResourceFile}"
    }
}

def isRunningCasServerSnapshot(casServerVersion) {
    return "${casServerVersion}".contains("-SNAPSHOT")
257 258 259
}

task downloadTomcat(type: Download) {
260 261
    def tomcatMajorVersion = "${tomcatVersion}".substring(0, "${tomcatVersion}".indexOf("."))
    def downloadFile = "https://www.apache.org/dist/tomcat/tomcat-${tomcatMajorVersion}/v${tomcatVersion}/bin/apache-tomcat-${tomcatVersion}.zip"
262 263 264 265 266 267 268 269 270 271 272 273 274 275
    logger.info "Downloading file $downloadFile"
    src downloadFile
    dest new File("apache-tomcat.zip")
    overwrite false
}

task unzipTomcat(type: Copy) {
    from zipTree(downloadTomcat.dest)
    into buildDir
    includeEmptyDirs = false
}

task shutdownTomcat(type:Exec) {
    doFirst {
276 277
        new File("${tomcatDirectory}/bin/catalina.sh").setExecutable(true)
        new File("${tomcatDirectory}/bin/shutdown.sh").setExecutable(true)
278 279 280 281 282 283 284
        environment["CATALINA_HOME"] = "${tomcatDirectory}"
        commandLine "${tomcatDirectory}/bin/shutdown.sh", "2>/dev/null"
        ignoreExitValue = true
    }
}

task tomcatDeploy(type:Exec) {
285
    dependsOn build, downloadTomcat, unzipTomcat, shutdownTomcat
286 287 288 289 290

    doFirst {
        Files.copy(Paths.get(bootWar.archivePath.canonicalPath), 
            Paths.get("${tomcatDirectory}/webapps/${casWebApplicationBinaryName}"), 
            StandardCopyOption.REPLACE_EXISTING)
291 292
        new File("${tomcatDirectory}/bin/catalina.sh").setExecutable(true)
        new File("${tomcatDirectory}/bin/startup.sh").setExecutable(true)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        mkdir "${tomcatDirectory}/logs"
        environment["CATALINA_HOME"] = "${tomcatDirectory}"
        commandLine "${tomcatDirectory}/bin/startup.sh"
    }
}

tail {
    log = files("${tomcatDirectory}/logs/catalina.out")
    showColors = true
}

build.mustRunAfter(setExecutable)

unzipTomcat.mustRunAfter(downloadTomcat)
tomcatDeploy.mustRunAfter(shutdownTomcat).finalizedBy(tail)