Skip to content

Instantly share code, notes, and snippets.

@emoseman
Created June 6, 2012 15:22
Show Gist options
  • Save emoseman/2882571 to your computer and use it in GitHub Desktop.
Save emoseman/2882571 to your computer and use it in GitHub Desktop.
Common gradle build
/*
* Common build file for gradle builds.
* To include use:
* apply from: "https://raw.github.com/gist/2882571/2a96a6218b24acbb1e47e87bedab180e2a3ac3ca/common.gradle"
*/
List apachecommons = ["commons-beanutils:commons-beanutils:1.8.3",
"org.apache.commons:commons-lang3:3.1",
"commons-logging:commons-logging:1.1.1"]
ext.commonRepositories = [
"http://mojo.informatik.uni-erlangen.de/nexus/content/repositories/public/",
"http://repo1.maven.org/maven2",
"http://maven.restlet.org",
"https://repository.apache.org/content/repositories/releases/",
"https://repository.jboss.org/nexus/content/groups/public/"
]
// Generates the list of package names for the package-list file for javadoc external references
task packageList() << {
def packages = []
sourceSets.main.java.srcDirs.each {
it.traverse { javaFile ->
if (javaFile.isFile()) {
def iterator = javaFile.iterator()
while (iterator.hasNext()) {
def line = iterator.next()
if (line.startsWith("package")) {
def matcher = line =~ /package (\S+);/
packages << matcher[0][1]
break
}
}
}
}
}
packages.unique(true)
def dirName = "${sourceSets.main.java.srcDirs.toArray()[0]}/config/"
println "dirName: $dirName"
new File(dirName).mkdirs()
def pkgFile = new File("${dirName}package-list")
pkgFile.write "# Generated by ***common.gradle\n"
packages.each {
println "writing $it to $pkgFile"
pkgFile.append "${it}\n"
}
}
task javaDoc(dependsOn: ["packageList"])
// applied to all projects
allprojects {
apply plugin: 'java'
// templates plugin
apply from: 'https://launchpad.net/gradle-templates/trunk/latest/+download/apply.groovy'
sourceCompatibility = 1.6
targetCompatibility = 1.6
// common javadoc links
javadoc {
options.links "http://docs.oracle.com/javase/6/docs/api/"
}
// custom tasks for creating source jar
task sourcesJar(type: Jar, dependsOn:classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn:javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
task javadocZip(type: Zip, dependsOn:javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
/*
// custom tasks for creating test jar
task testJar(type: Jar) {
classifier = 'tests'
if (sourceSets.test.output.classesDir.isDirectory()) {
from sourceSets.test.output.classesDir.eachFile { f -> print f}
}
}
*/
// resolution strategies
configurations.all {
resolutionStrategy {
// fail eagerly on version conflict (includes transitive dependencies)
// e.g. multiple different versions of the same dependency (group and name are equal)
// failOnVersionConflict()
// cache dynamic versions for 10 minutes
cacheDynamicVersionsFor 1, 'minutes'
// don't cache changing modules at all
cacheChangingModulesFor 0, 'minutes'
}
}
// common repository list
repositories {
url 'repo1.maven.org'
mavenCentral()
maven {
commonRepositories.each {
artifactUrls it
}
}
flatDir {
dirs '/data/libs'
}
} // repos
dependencies {
compile apachecommons
}
} // allprojects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment