build.gradle 12 KB
Newer Older
Misagh Moayyed's avatar
Misagh Moayyed committed
1
import org.apache.tools.ant.taskdefs.condition.Os
Misagh Moayyed's avatar
Misagh Moayyed committed
2
import java.nio.file.*
Misagh Moayyed's avatar
Misagh Moayyed committed
3

4 5 6 7
buildscript {
    repositories {
        mavenLocal()
        jcenter()
8
        maven { url "https://repo.spring.io/libs-milestone" }
Misagh Moayyed's avatar
Misagh Moayyed committed
9
        maven { url "https://plugins.gradle.org/m2/" }
10 11
    }
    dependencies {
Misagh Moayyed's avatar
Misagh Moayyed committed
12
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.springBootVersion}"
Misagh Moayyed's avatar
Misagh Moayyed committed
13 14 15
        classpath "de.undercouch:gradle-download-task:${project.gradleDownloadTaskVersion}" // used to download CAS shell
        classpath "org.apache.ivy:ivy:${project.ivyVersion}" // used to download CAS shell from snapshot repo
        classpath "gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:${project.jibVersion}"
16 17 18 19 20 21 22
    }
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
Misagh Moayyed's avatar
Misagh Moayyed committed
23 24
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    maven { url "https://build.shibboleth.net/nexus/content/repositories/releases/" }
25 26
    maven { url "https://repo.spring.io/milestone/" }
    maven { url "https://oss.jfrog.org/artifactory/oss-snapshot-local" }
27 28 29
}

apply from: "https://dl.bintray.com/scalding/generic/waroverlay.gradle"
Misagh Moayyed's avatar
Misagh Moayyed committed
30

31 32 33 34
apply plugin: "war"
apply plugin: "org.springframework.boot"
apply plugin: "eclipse"
apply plugin: "idea"
Misagh Moayyed's avatar
Misagh Moayyed committed
35 36 37 38 39 40
apply plugin: "de.undercouch.download"
apply plugin: "com.google.cloud.tools.jib"

def casServerVersion = project.'cas.version'
def casWebApplicationBinaryName = "cas.war"

Misagh Moayyed's avatar
Misagh Moayyed committed
41 42
tasks.findByName("build").mustRunAfter("setExecutable")

Misagh Moayyed's avatar
Misagh Moayyed committed
43 44 45 46 47 48 49 50 51
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
52 53 54
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

eclipse {
    classpath {
       downloadSources = true
       downloadJavadoc = true
    }
}

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}

war {
71
    includeWarJars = true
72
    entryCompression = ZipEntryCompression.STORED
73 74
}

75 76 77 78 79
springBoot {
    mainClassName = "org.apereo.cas.web.CasWebApplication"
}

bootWar {
Misagh Moayyed's avatar
Misagh Moayyed committed
80 81 82 83 84 85 86 87 88 89 90 91
    doFirst {
        def executable = project.hasProperty("executable") && Boolean.valueOf(project.getProperty("executable"))
        if (executable) {
            logger.info "Including launch script for executable WAR artifact"
            launchScript()
        } else {
            logger.info "WAR artifact is not marked as an executable"
        }
        archiveName "${casWebApplicationBinaryName}"
        baseName "cas"
        excludeDevtools = true
    }
92 93 94 95 96 97 98
}

bootRun {
    sourceResources sourceSets.main
    classpath = sourceSets.main.runtimeClasspath
}

99 100 101 102 103
wrapper {
    distributionType = Wrapper.DistributionType.BIN
    gradleVersion = "${project.gradleVersion}"
}

104
dependencies {
Misagh Moayyed's avatar
Misagh Moayyed committed
105
    if (project.hasProperty("external")) {
Misagh Moayyed's avatar
Misagh Moayyed committed
106
        compile "org.apereo.cas:cas-server-webapp:${casServerVersion}"
Misagh Moayyed's avatar
Misagh Moayyed committed
107
    } else {
Misagh Moayyed's avatar
Misagh Moayyed committed
108
        compile "org.apereo.cas:cas-server-webapp${project.appServer}:${casServerVersion}"
Misagh Moayyed's avatar
Misagh Moayyed committed
109 110
    }
    // Other dependencies may be listed here...
111 112
}

Misagh Moayyed's avatar
Misagh Moayyed committed
113 114
jib {
    from {
hdeadman's avatar
hdeadman committed
115
        image = project.baseDockerImage
Misagh Moayyed's avatar
Misagh Moayyed committed
116 117 118 119 120 121 122 123 124
    }
    to {
        image = "${project.group}/${project.name}"
        /**
         ecr-login: Amazon Elastic Container Registry (ECR)
         gcr: Google Container Registry (GCR)
         osxkeychain: Docker Hub
         */
        credHelper = "osxkeychain"
Misagh Moayyed's avatar
Misagh Moayyed committed
125
        /**
Misagh Moayyed's avatar
Misagh Moayyed committed
126 127 128 129 130 131 132 133
        auth {
            username = "*******"
            password = "*******"
        }
        tags = [casServerVersion]
        */
    }
    container {
Misagh Moayyed's avatar
Misagh Moayyed committed
134
        useCurrentTimestamp = true
Misagh Moayyed's avatar
Misagh Moayyed committed
135 136 137 138 139 140 141 142 143 144 145 146
        entrypoint = ['docker/entrypoint.sh']
        ports = ['80', '443', '8080', '8443']
        labels = [version:casServerVersion, name:project.name, group:project.group]
    }
    extraDirectory {
        path = file('src/main/jib')
        permissions = [
            '/docker/entrypoint.sh': '755'
        ]
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
147 148 149 150 151 152 153 154
task copyCasConfiguration(type: Copy, group: "build") {
    from "etc/cas/config"
    into "/etc/cas/config"
    doFirst {
        mkdir "/etc/cas/config"
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
155
task copyWebAppIntoJib(type: Copy, group: "Docker", description: "Copy the CAS configuration from this project to /etc/cas/config") {
Misagh Moayyed's avatar
Misagh Moayyed committed
156
    dependsOn build
Misagh Moayyed's avatar
Misagh Moayyed committed
157
    from "build/libs/${casWebApplicationBinaryName}"
158 159 160
    into "src/main/jib/docker/cas/war"
}

Misagh Moayyed's avatar
Misagh Moayyed committed
161
task copyConfigIntoJib(type: Copy, group: "Docker") {
162 163
    dependsOn build
    from "etc/cas"
Misagh Moayyed's avatar
Misagh Moayyed committed
164 165 166
    into "src/main/jib/docker/cas"
}

Misagh Moayyed's avatar
Misagh Moayyed committed
167
task deleteWebAppFromJib(type: Delete, group: "Docker", description: "Explodes the CAS web application archive") {
Misagh Moayyed's avatar
Misagh Moayyed committed
168 169 170 171
    delete "src/main/jib/docker/cas"
}

tasks.findByName("jibDockerBuild")
172
        .dependsOn(copyWebAppIntoJib, copyConfigIntoJib)
Misagh Moayyed's avatar
Misagh Moayyed committed
173 174 175 176 177
        .finalizedBy(deleteWebAppFromJib)

task explodeWar(type: Copy, group: "build", description: "Explodes the CAS web application archive") {
    dependsOn build
    from zipTree("build/libs/${casWebApplicationBinaryName}")
Misagh Moayyed's avatar
Misagh Moayyed committed
178
    into "${buildDir}/cas"
179 180 181 182 183
}

task run(group: "build", description: "Run the CAS web application in embedded container mode") {
    dependsOn build
    doLast {
184
        def casRunArgs = Arrays.asList("-Xmx2048M -XX:+TieredCompilation -XX:TieredStopAtLevel=1".split(" "))
185
        javaexec {
186
            main = "-jar"
187
            jvmArgs = casRunArgs
Misagh Moayyed's avatar
Misagh Moayyed committed
188
            args = ["build/libs/${casWebApplicationBinaryName}"]
189 190 191 192 193
            logger.info "Started ${commandLine}"
        }
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
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
        }
        logger.info "Running" + bootWar.archivePath
        commandLine bootWar.archivePath
    }
}

213
task debug(group: "build", description: "Debug the CAS web application in embedded mode on port 5005") {
214 215
    dependsOn build
    doLast {
Misagh Moayyed's avatar
Misagh Moayyed committed
216
        logger.info "Debugging process is started in a suspended state, listening on port 5005."
217
        def casArgs = Arrays.asList("-Xmx2048M".split(" "))
218
        javaexec {
219
            main = "-jar"
220 221
            jvmArgs = casArgs
            debug = true
Misagh Moayyed's avatar
Misagh Moayyed committed
222
            args = ["build/libs/${casWebApplicationBinaryName}"]
223 224 225 226 227
            logger.info "Started ${commandLine}"
        }
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
228
task downloadShell (group: "shell", description: "Download CAS shell jar from snapshot or release maven repo") {
229
    doFirst {
Misagh Moayyed's avatar
Misagh Moayyed committed
230
        mkdir "${project.shellDir}"
231
    }
232
    doLast {
Misagh Moayyed's avatar
Misagh Moayyed committed
233
        def downloadFile
234
        if (isRunningCasServerSnapshot(casServerVersion)) {
Misagh Moayyed's avatar
Misagh Moayyed committed
235
            def snapshotDir = "https://oss.sonatype.org/content/repositories/snapshots/org/apereo/cas/cas-server-support-shell/${casServerVersion}/"
236 237 238
            def files = new org.apache.ivy.util.url.ApacheURLLister().listFiles(new URL(snapshotDir))
            files = files.sort{it.path}
            files.each {
Misagh Moayyed's avatar
Misagh Moayyed committed
239
                if (it.path.endsWith(".jar")) {
240 241 242 243
                    downloadFile = it
                }
            }
        } else {
Misagh Moayyed's avatar
Misagh Moayyed committed
244
            downloadFile = "https://repo1.maven.org/maven2/org/apereo/cas/cas-server-support-shell/${casServerVersion}/cas-server-support-shell-${casServerVersion}.jar"
245
        }
Misagh Moayyed's avatar
Misagh Moayyed committed
246
        logger.info "Downloading file: ${downloadFile}"
247 248
        download {
            src downloadFile
Misagh Moayyed's avatar
Misagh Moayyed committed
249
            dest new File("${project.shellDir}", "cas-server-support-shell-${casServerVersion}.jar")
250 251 252 253 254
            overwrite false
        }
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
255
task runShell (group: "shell",  description: "Run the CAS shell") {
256 257 258 259 260 261 262
    dependsOn downloadShell
    doLast {
        def casShellArgs = Arrays.asList("-Xmx1500M".split(" "))
        javaexec {
            main = "-jar"
            jvmArgs = casShellArgs
            standardInput = System.in
Misagh Moayyed's avatar
Misagh Moayyed committed
263
            args = ["${project.shellDir}/cas-server-support-shell-${casServerVersion}.jar", "--shell"]
264 265 266 267
        }
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
268
task debugShell (group: "shell", description: "Run the CAS shell with debug options, wait for debugger on port 5005") {
269 270 271 272 273 274 275 276
    dependsOn downloadShell
    doLast {
        def casArgs = Arrays.asList("-Xmx1500M ".split(" "))
        javaexec {
            main = "-jar"
            jvmArgs = casArgs
            debug = true
            standardInput = System.in
Misagh Moayyed's avatar
Misagh Moayyed committed
277
            args = ["${project.shellDir}/cas-server-support-shell-${casServerVersion}.jar", "--shell"]
278 279 280 281
        }
    }
}

282 283 284 285 286 287 288
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 }
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
289 290 291 292 293 294
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 }
295 296
}

Misagh Moayyed's avatar
Misagh Moayyed committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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"
    }
}

Misagh Moayyed's avatar
Misagh Moayyed committed
326
task listTemplateViews (group: "build", description: "List all CAS views") {
Misagh Moayyed's avatar
Misagh Moayyed committed
327 328 329 330 331 332 333 334 335 336 337 338 339
    dependsOn explodeWar

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

Misagh Moayyed's avatar
Misagh Moayyed committed
340 341 342 343 344 345 346 347
task getResource(group: "build", description: "Fetch a CAS resource and move it into the overlay") {
    dependsOn explodeWar

    doFirst {
        def explodedDir="${buildDir}/cas"
        def resourceName = project.getProperty("resourceName")

        def results = fileTree(explodedDir).matching {
Misagh Moayyed's avatar
Misagh Moayyed committed
348
            include "**/${resourceName}.*"
Misagh Moayyed's avatar
Misagh Moayyed committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        }
        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)
Misagh Moayyed's avatar
Misagh Moayyed committed
370
        println "Copied file to ${toResourceFile}"
Misagh Moayyed's avatar
Misagh Moayyed committed
371 372 373 374
    }
}


375
def isRunningCasServerSnapshot(casServerVersion) {
Misagh Moayyed's avatar
Misagh Moayyed committed
376 377
    return "${casServerVersion}".contains("-SNAPSHOT")
}