import org.apache.tools.ant.filters.ReplaceTokens apply plugin: "groovy" apply plugin: "maven" apply plugin: "war" apply plugin: "jetty" apply from: 'shell.gradle' group = "com.jdbernard" version = new ProjectVersion() // webAppDirName = "build/webapp/main" repositories { mavenLocal() mavenCentral() } dependencies { compile 'ch.qos.logback:logback-classic:1.1.2' compile 'ch.qos.logback:logback-core:1.1.2' compile 'com.impossibl.pgjdbc-ng:pgjdbc-ng:0.3' compile 'com.lambdaworks:scrypt:1.4.0' compile 'com.zaxxer:HikariCP-java6:2.3.2' compile 'javax:javaee-api:7.0' compile 'javax.ws.rs:javax.ws.rs-api:2.0.1' compile 'joda-time:joda-time:2.7' compile 'org.codehaus.groovy:groovy-all:2.3.6' compile 'org.slf4j:slf4j-api:1.7.10' runtime 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.2' runtime 'org.glassfish.jersey.containers:jersey-container-servlet:2.16' runtime 'org.glassfish.jersey.media:jersey-media-json-jackson:2.16' providedCompile 'javax.servlet:javax.servlet-api:3.1.0' testCompile 'com.jdbernard:jdb-util:3.4' testCompile 'junit:junit:4.12' testRuntime 'com.h2database:h2:1.4.186' } war { from "resources/webapp" from "build/webapp" filter(ReplaceTokens, tokens: [version: version]) rename '(.+)(\\..*(css|js))', '$1-' + version + '$2' version = project.version.releaseVersion webInf { from 'resources/main/WEB-INF' } exclude "**/.*.swp", "**/.sass-cache" } test { testLogging { events 'failed' } } task testWar(type: War) { from 'resources/webapp' filter(ReplaceTokens, tokens: [version: version]) rename '(.+)(\\..*(css|js))', '$1-' + version + '$2' version = project.version.releaseVersion webInf { from 'resources/test/WEB-INF' } classifier 'test' } task compileScss( group: 'build', description: 'Compile SCSS files into CSS.', type: Exec ) { executable "scss" args "--update", "src/main/webapp/css:build/webapp/css" } war.dependsOn compileScss testWar.dependsOn compileScss // ## Build Versioning task task incrementBuildNumber( group: 'versioning', description: "Increment the project's build number." ) << { ++version.build } task incrementMinorNumber( group: 'versioning', description: "Increment the project's minor version number." ) << { ++version.minor } task incrementMajorNumber( group: 'versioning', description: "Increment the project's major version number." ) << { ++version.major } task markReleaseBuild( group: 'versioning', description: "Mark this version of the project as a release version." ) << { version.release = true } war.dependsOn << incrementBuildNumber testWar.dependsOn << incrementBuildNumber // ## Custom tasks for local deployment task deployLocal(dependsOn: ['build']) << { def warName = "${project.name}-${version.releaseVersion}.war" def jettyHome = System.getenv("JETTY_HOME") def deployedWar = new File("$jettyHome/webapps/$warName") if (deployedWar.exists()) deployedWar.delete(); copy { from "build/libs" into "$jettyHome/webapps" include warName } } task killJettyLocal() << { def pidFile = new File(System.properties['user.home'] + "/temp/jetty.pid") println "Killing old Jetty instance." shell_("sh", "-c", 'kill $(jps -l | grep start.jar | cut -f 1 -d " ")') } task localJetty(dependsOn: ['killJettyLocal', 'deployLocal']) << { spawn(["java", "-jar", "start.jar"], new File(jettyHome)) } // ## Project Version class ProjectVersion { private File versionFile int major int minor int build boolean release public ProjectVersion() { this(new File('version.properties')) } public ProjectVersion(File versionFile) { this.versionFile = versionFile if (!versionFile.exists()) { versionFile.createNewFile() this.major = this.minor = this.build = 0 this.save() } else this.load() } @Override String toString() { "$major.$minor${release ? '' : '-build' + build}" } public String getReleaseVersion() { "$major.$minor" } public void setRelease(boolean release) { this.release = release; save() } public void setMajor(int major) { this.major = major; minor = build = 0; release = false; save() } public void setMinor(int minor) { this.minor = minor; build = 0; release = false; save() } public void setBuild(int build) { this.build = build; save() } private void save() { def props = new Properties() versionFile.withInputStream { props.load(it) } ["major", "minor", "build"].each { props[it] = this[it].toString() } props["version.release"] = release.toString() versionFile.withOutputStream { props.store(it, "") } } private void load() { def props = new Properties() versionFile.withInputStream { props.load(it) } ["major", "minor", "build"].each { this[it] = props[it] ? props[it] as int : 0 } release = Boolean.parseBoolean(props["version.release"]) } }