Skip to content

Instantly share code, notes, and snippets.

@nobeans
Forked from vvakame/build.gradle
Last active December 16, 2015 14:29
Show Gist options
  • Save nobeans/5448682 to your computer and use it in GitHub Desktop.
Save nobeans/5448682 to your computer and use it in GitHub Desktop.
import groovy.xml.MarkupBuilder
apply plugin: "java"
apply plugin: "war"
apply plugin: "eclipse"
// maven-publish, maven2Gradle, build-dashboard
// checkstyle, findbugs, jdepend, pmd
sourceCompatibility = 1.7
targetCompatibility = 1.7
task wrapper(type: Wrapper) { gradleVersion = '1.4' }
repositories {
mavenCentral()
maven { url "https://www.seasar.org/maven/maven2" }
}
configurations {
providedCompile // <scope>provided</scope>
aptCompile5 // 1.5
aptCompile6 // 1.6
}
sourceSets {
main {
java { srcDirs generatedSrc }
output.resourcesDir = "${generatedWar}/WEB-INF/classes"
output.classesDir = "${generatedWar}/WEB-INF/classes"
}
main.compileClasspath += configurations.providedCompile
test.compileClasspath += configurations.providedCompile
test.runtimeClasspath += configurations.providedCompile
}
dependencies {
compile("org.slim3:slim3:${slim3Version}") {
exclude group: "com.google.appengine", module: "appengine-api-1.0-sdk"
exclude group: "com.google.appengine", module: "appengine-api-labs"
}
compile "com.google.appengine:appengine-api-1.0-sdk:${appengineVersion}"
compile "com.google.appengine:appengine-api-labs:${appengineVersion}"
aptCompile5("org.slim3:slim3-gen:${slim3Version}") {
exclude group: "org.apache.ant", module: "ant"
}
providedCompile "javax.servlet:servlet-api:2.5"
testCompile "junit:junit:4.11"
testCompile "org.hamcrest:hamcrest-all:1.3"
testCompile "com.google.appengine:appengine-api-stubs:${appengineVersion}"
testCompile "com.google.appengine:appengine-testing:${appengineVersion}"
compile "net.vvakame:memvache:0.3.2"
compile "net.vvakame:jsonpullparser-core:${jppVersion}"
aptCompile6 "net.vvakame:jsonpullparser-apt:${jppVersion}"
compile "org.twitter4j:twitter4j-appengine:3.0.3"
}
task clean(overwrite: true) {
delete generatedSrc
delete testGeneratedDir
delete "${generatedWar}/WEB-INF/classes"
delete "build"
}
// ほいほい消えると開発時に困るので dependsOn は設定しない
task cleanAppEngine << { delete "${generatedWar}/WEB-INF/appengine-generated" }
task compileApt5Java(overwrite: true, dependsOn: clean) {
description = "Generate java code by AnnotationProcessor"
logging.level = LogLevel.QUIET
doLast {
file(generatedSrc).mkdir()
ant.path(id: "aptFactoryPath", location: configurations.aptCompile5.asPath)
ant.apt(
// srcdir: "",
// destdir: "",
classpath: configurations.compile.asPath,
// debug: "on",
compile: false,
// factory: "",
factorypathref: "aptFactoryPath",
preprocessdir: generatedSrc,
includeAntRuntime: false,
encoding: "UTF-8"
) {
sourceSets.main.java.srcDirs.each { src(path: it) }
// 何故か src/test/java でClassCastExceptionが出る
// sourceSets.test.java.srcDirs.each { src(path: it) }
}
// 自動生成コードが入ったディレクトリをソースディレクトリとして追加する
sourceSets.main.java.srcDirs += generatedSrc
}
compileJava.dependsOn compileApt5Java
}
task compileApt6Java(overwrite: true, dependsOn: clean) {
description = "Generate java code by AnnotationProcessor"
logging.level = LogLevel.QUIET
doLast {
// 生成コードを出力するディレクトリを予め作成
file(generatedSrc).mkdir()
// apt 1.6
ant.javac(
includeAntRuntime: false,
classpath: configurations.compile.asPath,
srcdir: "src/main/java",
encoding: "UTF-8"
) {
sourceSets.main.java.srcDirs.each { src(path: it) }
sourceSets.test.java.srcDirs.each { src(path: it) }
compilerarg(line: "-proc:only")
compilerarg(line: "-processorpath ${configurations.aptCompile6.asPath}")
compilerarg(line: "-s ${generatedSrc}")
}
// 自動生成コードが入ったディレクトリをソースディレクトリとして追加する
sourceSets.main.java.srcDirs += generatedSrc
}
// compileJavaタスクの前にcompileAptJavaタスクを実行させる
compileJava.dependsOn compileApt6Java
}
eclipse {
// sample from http://forums.gradle.org/gradle/topics/eclipse_plugin_adding_classpath_entries_remove_them
project {
name = 'gradle-sample'
natures 'com.google.appengine.eclipse.core.gaeNature', 'com.google.gdt.eclipse.core.webAppNature'
buildCommand 'com.google.gdt.eclipse.core.webAppProjectValidator'
buildCommand 'com.google.appengine.eclipse.core.enhancerbuilder'
buildCommand 'com.google.appengine.eclipse.core.projectValidator'
}
classpath {
defaultOutputDir = file("${generatedWar}/WEB-INF/classes")
containers 'org.eclipse.jdt.launching.JRE_CONTAINER', 'com.google.appengine.eclipse.core.GAE_CONTAINER'
downloadSources = true
downloadJavadoc = true
}
jdt {
file {
withProperties { properties ->
properties.setProperty("org.eclipse.jdt.core.compiler.processAnnotations", "enabled")
}
}
}
}
task copyAptProcessorToEclipseLib(type: Copy) {
eclipseJdt.dependsOn copyAptProcessorToEclipseLib
into "eclipse.lib"
from configurations.aptCompile5 + configurations.aptCompile6
}
def writeEclipseSettings(File f, String s) {
f.parentFile.mkdirs()
f.text = s
}
eclipseJdt {
doFirst {
file(".factorypath").withWriter { w ->
def xml = new MarkupBuilder(w)
xml.setDoubleQuotes(true)
xml."factorypath"() {
"factorypathentry"(
kind: "PLUGIN", id: "org.eclipse.jst.ws.annotations.core", enabled: true, runInBatchMode: false
)
"factorypathentry"(
kind: "WKSPJAR", id: "/${project.name}/eclipse.lib/slim3-gen-${slim3Version}.jar", enabled: true, runInBatchMode: false
)
"factorypathentry"(
kind: "WKSPJAR", id: "/${project.name}/eclipse.lib/jsonpullparser-apt-${jppVersion}.jar", enabled: true, runInBatchMode: false
)
}
}
writeEclipseSettings file(".settings/org.eclipse.jdt.apt.core.prefs"), """\
|eclipse.preferences.version=1
|org.eclipse.jdt.apt.aptEnabled=true
|org.eclipse.jdt.apt.genSrcDir=${generatedSrc}
|org.eclipse.jdt.apt.processorOptions/JsonPullParserClassPostfix=${jsonpullparserGenclassPostfix}
|org.eclipse.jdt.apt.processorOptions/JsonPullParserDebug=${jsonpullparserDebug}
|org.eclipse.jdt.apt.reconcileEnabled=false
|""".stripMargin()
writeEclipseSettings file(".settings/com.google.gdt.eclipse.core.prefs"), """\
|eclipse.preferences.version=1
|jarsExcludedFromWebInfLib=
|warSrcDir=${generatedWar}
|warSrcDirIsOutput=false
|""".stripMargin()
writeEclipseSettings file(".settings/com.google.appengine.eclipse.core.prefs"), """\
|eclipse.preferences.version=1
|ormEnhancementInclusions=
|""".stripMargin()
}
}
task cleanMiscEclipse {
cleanEclipse.dependsOn cleanMiscEclipse
doLast {
delete ".settings"
delete ".factorypath"
delete "eclipse.lib"
}
}
slim3Version = "1.0.16"
appengineVersion = "1.7.5"
jppVersion = "1.6.1"
generatedSrc = ".apt_generated"
testGeneratedDir = "www-test"
generatedWar = "src/main/webapp"
jsonpullparserGenclassPostfix = "Gen"
jsonpullparserDebug = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment