Last active
October 14, 2023 09:53
-
-
Save thadguidry/f29b6a007061139f2c1a5b5d5338812f to your computer and use it in GitHub Desktop.
Example Gradle Build for Java War project including usage of Jenkins system variables, git branch, git tag, Artifactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
apply plugin: 'war' | |
apply plugin: 'eclipse' | |
apply plugin: 'idea' | |
apply from: 'cobertura.gradle' | |
defaultTasks 'clean', 'build' | |
description = """Example Messaging Gateway""" | |
group = 'com.example' | |
def warName = 'emg' | |
def tarName = 'example-emg' | |
def buildNumber = System.getenv("BUILD_NUMBER") // Get the Jenkins BUILD_NUMBER | |
def workingBranch = """git rev-parse --abbrev-ref HEAD""".execute().text.trim() | |
def isEmergencyRelease = workingBranch.matches("^ER.*_release") && buildNumber | |
def isRelease = workingBranch.endsWith('_release') && buildNumber | |
def isIntegration = workingBranch.equals('master_integrate') && buildNumber | |
def isSnapshot = project.hasProperty('snapshot') && buildNumber | |
def gitTag = """git describe --abbrev=0""".execute().text.trim() | |
def (gitAbbrev, versionMajor, versionMinor, versionPatch, Rev) = gitTag.tokenize('\\.') | |
version = "${versionMajor}.${versionMinor}" | |
def integrationExt = 'INT' | |
def localExt = 'LOCAL' | |
def snapshotExt = 'SNAPSHOT' | |
def projJVen = System.getProperty('java.vendor') | |
def projJVer = System.getProperty('java.version') | |
def projJDK = "${projJVer} (${projJVen})" | |
sourceSets { | |
main { | |
java { | |
srcDirs = ['src/main/java'] | |
} | |
resources { | |
srcDirs = ['src/main/resources'] | |
} | |
} | |
test { | |
java { | |
srcDirs = ['src/test/java'] | |
} | |
resources { | |
srcDirs = ['src/test/resources']; exclude "**/*.java" | |
} | |
} | |
} | |
task setVersion { | |
if ( buildNumber ) { | |
if ( (!gitAbbrev.equals(abbrev)) || (versionMajor==null) || (versionMinor==null) || (versionPatch==null) || (Rev==null) ) { | |
throw new Exception("could not determine version information") | |
} | |
if ( isSnapshot ) { | |
println " Setting snapshot version ..." | |
version = "${version}.${buildNumber}.${snapshotExt}" | |
} | |
else if ( isEmergencyRelease ) { | |
println " Setting Emergency Release version ..." | |
version = "${version}.${versionPatch}" | |
} | |
else if ( isRelease ) { | |
println " Setting release version ..." | |
version = "${version}.0" | |
} | |
else if ( isIntegration ) { | |
println " Setting integration version ..." | |
version = "${version}.${buildNumber}.${integrationExt}" | |
} | |
else { | |
println " Setting local version ..." | |
def localBuild = "00.00.00" | |
version = "${localBuild}.${localExt}" | |
} | |
println " buildNumber: ${buildNumber}" | |
} else { | |
version = "00.00.00.${localExt}" | |
} | |
println " Build-Jdk: ${projJDK}" | |
println " Working branch: ${workingBranch}" | |
println " Git Tag: ${gitTag}" | |
println " Version: ${version}" | |
} | |
compileJava.dependsOn setVersion | |
test { | |
afterTest { TestDescriptor td, TestResult tr -> | |
println "${tr} ${td}" | |
} | |
} | |
war { | |
archiveName = "${warName}.war" | |
manifest { | |
attributes('RELEASE-VERSION':version) | |
attributes('Build-Date':new Date().format("yyyy-MM-dd' 'HH:mm:ssZ")) | |
attributes('Build-Jdk':projJDK) | |
} | |
} | |
task buildTar(type: Tar) { | |
baseName = "${tarName}-${version}" | |
extension = 'tar' | |
version="" | |
from ("build/libs/") { | |
include "${warName}*" | |
into tarName | |
} | |
from("scripts") { | |
into("${tarName}/scripts") | |
} | |
} | |
task buildRelease(type: Tar) { | |
baseName = "${version}/${container}_X_${Rev}" | |
extension = 'tar.gz' | |
compression = Compression.GZIP | |
version = "" | |
from("build/distributions/") { | |
include "$tarName*" | |
} | |
} | |
if ( isRelease || isEmergencyRelease ) { | |
build.finalizedBy buildTar | |
buildTar.finalizedBy buildRelease | |
} | |
else { | |
build.finalizedBy buildTar | |
} | |
task all(dependsOn: build) { | |
} | |
task showMoreHelp { | |
doLast { | |
println "\n ===== EXAMPLE EMG Help =====\n" | |
println " To use default tasks, execute 'gradle' or 'gradle clean build'\n" | |
println " To build only the war file: 'gradle clean assemble'\n" | |
println " To build without executing Junit tests: 'gradle -x test' or 'gradle clean build -x test'\n" | |
println " All build artifacts are located in the build/ directory:" | |
println " war file location: build/libs/${warName}.war" | |
println " tar file location: build/distibutions/${tarName}-${version}.tar" | |
} | |
} | |
help.finalizedBy showMoreHelp | |
task wrapper(type: Wrapper) { | |
gradleVersion = '3.5' | |
} | |
sourceCompatibility = 1.7 | |
targetCompatibility = 1.7 | |
repositories { | |
// Check local repositories first ... no Example.com dependencies yet ... | |
// Check Artifactory repositories ... | |
maven { url 'https://example.com/artifactory/proj-emg-release-local/lib/' } | |
maven { url 'https://example.com/artifactory/proj-emg-dev-local/lib/' } | |
maven { url 'https://example.com/artifactory/proj-emg-staging-local/lib/' } | |
maven { url 'https://example.com/artifactory/proj-emg-external-local/lib/' } | |
} | |
dependencies { | |
testRuntime "org.slf4j:slf4j-api:1.7.10" | |
providedCompile ( | |
'javax.servlet:javax.servlet-api:3.0.1', | |
'junit:junit:3.8.1' | |
) | |
compile ( | |
// 3rd party jars | |
'aopalliance:aopalliance:1.0', | |
'apache-log4j:snmpTrapAppender:1.2.8', | |
'c3p0:c3p0:0.9.1.1', | |
'classworlds:classworlds:1.1-alpha-2', | |
'com.fasterxml.jackson.core:jackson-annotations:2.7.0', | |
'com.fasterxml.jackson.core:jackson-core:2.7.3', | |
'com.fasterxml.jackson.core:jackson-databind:2.7.3', | |
'com.google.code.gson:gson:2.3.1', | |
'com.oracle.jdbc:ojdbc7:12.1.0.1', | |
'commons-dbcp:commons-dbcp:1.2.2', | |
'commons-logging:commons-logging:1.2', | |
'commons-pool:commons-pool:1.3', | |
'javax.jms:jms:1.1', | |
'javax.servlet:javax.servlet-api:3.0.1', | |
'jstl:jstl:1.2', | |
'log4j:log4j:1.2.17', | |
'net.sf.joesnmp:joesnmp:0.3.4', | |
'org.apache.commons:commons-collections4:4.1', | |
'org.apache.commons:commons-lang3:3.1', | |
'org.apache.maven.wagon:wagon-provider-api:1.0-beta-2', | |
'org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2', | |
'org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1', | |
'org.codehaus.plexus:plexus-utils:1.5.6', | |
'org.hornetq:hornetq-core:2.2.5.Final', | |
'org.mockito:mockito-all:1.10.19', | |
'org.quartz-scheduler:quartz:2.2.1', | |
'org.slf4j:slf4j-api:1.6.6', | |
'org.springframework:spring-aop:4.2.5.RELEASE', | |
'org.springframework:spring-beans:4.2.5.RELEASE', | |
'org.springframework:spring-context:4.2.5.RELEASE', | |
'org.springframework:spring-context-support:4.2.5.RELEASE', | |
'org.springframework:spring-core:4.2.5.RELEASE', | |
'org.springframework:spring-expression:4.2.5.RELEASE', | |
'org.springframework:spring-jdbc:4.2.5.RELEASE', | |
'org.springframework:spring-jms:4.2.5.RELEASE', | |
'org.springframework:spring-messaging:4.2.5.RELEASE', | |
'org.springframework:spring-orm:4.2.5.RELEASE', | |
'org.springframework:spring-tx:4.2.5.RELEASE', | |
'org.springframework:spring-web:4.2.5.RELEASE', | |
'org.springframework:spring-webmvc:4.2.5.RELEASE' | |
) | |
runtime ( | |
'cglib:cglib-nodep:2.2.2' | |
) | |
testCompile ( | |
'junit:junit:4.12', | |
'org.springframework:spring-test:4.2.5.RELEASE' | |
) | |
/* | |
// This is just an example of how to customize the Cobertura coverage reporting | |
// to exclude certain classes from being inspected and reported. | |
cobertura { | |
coverageExcludes = ['.*com.example.gateway.cache.*','.*com.example.gateway.common.*','.*com.example.gateway.model.*','.*com.example.gateway.dao.ConfigParamDao.*'] | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment