Skip to content

Instantly share code, notes, and snippets.

@emartynov
Last active September 17, 2020 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emartynov/c88194c702b77f42a7c0 to your computer and use it in GitHub Desktop.
Save emartynov/c88194c702b77f42a7c0 to your computer and use it in GitHub Desktop.
Gradle build for j2me project
buildscript {
repositories { mavenCentral() }
dependencies {
classpath 'com.admc:gradle-javaPropFile-plugin:latest.integration'
}
}
apply plugin: 'javaPropFile'
apply plugin: 'java'
apply from: 'emma.gradle'
loadDefaultProperties()
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
flatDir name: 'wtk', dirs: project['wtk.home'] + "/lib"
maven {
#our releases repository
}
maven {
#our cache repository
}
maven {
#our snapshots repository
}
mavenCentral()
}
buildDir = "target"
sourceSets {
main {
java {
srcDir 'src'
}
resources {
srcDir 'res'
srcDir 'builds/imagesets/medium'
}
}
test {
java {
srcDir 'unit-test/src'
}
}
}
configurations {
nna
wtkProvide
compile.extendsFrom(wtkProvide)
}
dependencies {
compile group: 'org.j4me', name: 'j4me', version: j4me_version
compile group: 'com.tinyline', name: 'tinylinegzip', version: nokia_api_version
compile group: 'com.ebuddy.sdk', name: 'j2me-push-api', version: j2me_push_version
compile group: 'com.ebuddy.sdk', name: 'j2me-push', version: j2me_push_version
compile group: 'com.ebuddy.sdk', name: 'j2me-push-installer', version: j2me_push_version
compile group: 'com.ebuddy.sdk', name: 'j2me-push-aqua', version: j2me_push_version
compile group: 'com.nokia', name: 'napi', version: nokia_napi_version
compile group: 'com.nokia', name: 'nokiaapi', version: nokia_nokiaapi_version
compile group: 'com.nokia', name: 'api', version: nokia_api_version
testCompile group: 'org.mockito', name:'mockito-all', version:mockito_version
testCompile group: 'junit', name:'junit', version:junit_version
testCompile group: 'org.easytesting', name:'fest-assert', version:fest_assert_version
testCompile group: 'org.easytesting', name:'fest-util', version:fest_util_version
testCompile group: 'org.powermock', name:'powermock-api-mockito', version:powermock_version
testCompile group: 'org.powermock', name:'powermock-api', version:powermock_version
testCompile group: 'org.powermock', name:'powermock-api-support', version:powermock_version
testCompile group: 'org.powermock', name:'powermock', version:powermock_version
testCompile group: 'org.powermock', name:'powermock-module-junit4', version:powermock_version
testCompile group: 'net.java', name:'quickcheck', version:quickcheck_version
compile fileTree(dir: 'lib', include: '*.jar')
wtkProvide ':cldcapi11:@jar'
wtkProvide ':midpapi20:@jar'
wtkProvide ':jsr234:@jar'
wtkProvide ':wma20:@jar'
wtkProvide ':mmapi:@jar'
wtkProvide ':jsr75:@jar'
wtkProvide ':jsr211:@jar'
}
task(init) {
loadProperties 'build/build.symbols'
loadProperties 'build/configs/generic_cldc11_medium.properties'
}
compileJava {
options.bootClasspath = configurations.wtkProvide.inject("") { a, b -> a + b + File.pathSeparator }
sourceCompatibility = '1.3'
targetCompatibility = '1.1'
}
task(preverify, type: Exec, dependsOn: compileJava) {
description = "Preverify"
ext.destinationPath = compileJava.destinationDir
inputs.dir compileJava.destinationDir
outputs.dir destinationPath
executable project['wtk.home'] + "/bin/preverify"
args '-classpath', configurations.compile.inject("") { a, b -> a + b + File.pathSeparator }
args '-d', destinationPath
args '-target', 'CLDC1.1'
args compileJava.destinationDir
}
compileTestJava {
sourceCompatibility = '1.6'
targetCompatibility = '1.6'
}
jar {
dependsOn += preverify
dependsOn += init
archivesBaseName = project['build.name']
jar.baseName = project['build.name']
from configurations.nna.collect { it.isDirectory() ? it : zipTree(it) }
}
task(jad, dependsOn: jar) << {
description = "Generation JAD"
project.ext.properties.each { k, v ->
if (k.startsWith('wtk.')) {
ant.properties[k] = project[k]
}
}
ant {
taskdef(name: "wtkjad", classname: "de.pleumann.antenna.WtkJad", classpath: "build/buildlibs/antenna-bin-1.2.1-beta.jar")
taskdef(name: "wtkrun", classname: "de.pleumann.antenna.WtkRun", classpath: "build/buildlibs/antenna-bin-1.2.1-beta.jar")
}
println "Generation JAD"
ant.wtkjad(name: "ebuddy", vendor: "vendor", jadfile: "target/libs/" + project['build.name'] + ".jad", version:
project['version.number'], update: true) {
midlet(name: "ebuddy", class: "com.ebuddy.messenger.AppMIDlet", icon: "/icon.png")
attribute(name: "MIDlet-Permissions-Opt", value: "javax.microedition.io.Connector.http, javax.microedition.io.Connector.https, " +
"javax.microedition.io.Connector.socket, javax.microedition.io.Connector.ssl, javax.microedition.io.Connector.file.read, " +
"javax.microedition.io.Connector.file.write, javax.microedition.media.control.VideoControl.getSnapshot, " +
"javax.wireless.messaging.sms.send, javax.microedition.io.Connector.sms")
attribute(name: "MicroEdition-Configuration", value: String.format("CLDC-%s", project['wtk.cldc.version']));
attribute(name: "MicroEdition-Profile", value: String.format("MIDP-%s", project['wtk.midp.version']));
attribute(name: "MIDlet-Jar-URL", value: project['build.name'] + ".jar")
attribute(name: "MIDlet-Jar-Size", value: new File("target/libs/" + project['build.name'] + ".jar").size())
project.ext.properties.each { k, v ->
if (k.startsWith('jad.')) {
attribute(name: k.substring(4, k.length()), value: v)
}
}
}
}
task(sign, dependsOn: jad) << {
description = "sign"
ant {
taskdef(name: "sign", classname: "com.ebuddy.hacks.NokiaSigningTask", classpath: "build/buildlibs/nokia-signing-tool-1.0.0-SNAPSHOT.jar")
}
ant.sign(jadFile: "target/libs/" + project['build.name'] + ".jad",
jarFile: "target/libs/" + project['build.name'] + ".jar",
signedJadFile: "target/libs/" + project['build.name'] + ".jad")
}
task(createEnvironments, dependsOn: jad) << {
def jadFileName = "target/libs/" + project['build.name'] + ".jad";
def environmentProperties = propFileLoader.load(file('build/environments.properties'), [:])
// Get all enviroments e.g trunk, stg-new
def environmentKeys = getAllEnvironmentsFromProperties(environmentProperties)
// copy and update jad for each environment
for (environment in environmentKeys) {
copyJadForEnvironment(environmentProperties, jadFileName, environment)
}
}
def copyJadForEnvironment(Map environmentProperties, String originalJadFileName, String environment) {
def jadFileNamePattern = "target/libs/%s_%s.jad";
def newJadFileName = String.format(jadFileNamePattern, project['build.name'], environment)
def server = environmentProperties[String.format("%s.appserver", environment)]
def resourceUrl = environmentProperties[String.format("%s.resourceurl", environment)]
ant.copy(file: originalJadFileName, tofile: newJadFileName)
ant.wtkjad(name: "eBuddy", vendor: "eBuddy", jadfile: newJadFileName, version: project['version.number'], update: true) {
ant.attribute(name: "AppServer", value: server)
ant.attribute(name: "ResourceURL", value: resourceUrl)
}
}
def getAllEnvironmentsFromProperties(Map environmentProperties) {
def environmentKeys = [] as Set
for (e in environmentProperties) {
def environmentName = e.key.tokenize('.').get(0)
println e.key + "$environmentName"
environmentKeys.add(environmentName)
}
return environmentKeys
}
build {
dependsOn += createEnvironments
}
task wtkRun(dependsOn: build) << {
ant.wtkrun(jadFile: "target/libs/" + project['build.name'] + ".jad", device: "DefaultColorPhone", wait: true)
}
/* Load the properties that are needed to solve symbolic references in the
* build configuration files and the build script. These properties must
* be loaded as on of the first thing to be able to use them in the classpath
* definitions etc.
*/
def loadDefaultProperties() {
ext.env = System.getenv()
loadProperties 'build/version.number'
loadProperties 'build/local.properties'
loadProperties 'build/default.properties'
loadProperties 'build/dependencies.properties'
}
def loadProperties(String fileName) {
propFileLoader.load(file(fileName))
}
def loadProperties(String fileName, String objectName) {
propFileLoader.load(file(fileName), objectName)
}
j4me_version=1.0.3
nokia_api_version=1.0
j2me_push_version=1.0.0-SNAPSHOT
nokia_napi_version=1.3.12
nokia_nokiaapi_version=1.7
mockito_version=1.9.0
junit_version=4.10
fest_assert_version=1.4
fest_util_version=1.1.6
powermock_version=1.4.12
quickcheck_version=0.6
configurations{
emma
}
dependencies{
emma "emma:emma:2.1.5320"
emma "emma:emma_ant:2.1.5320"
}
def emmaConvention = new EmmaPluginConvention(project)
project.convention.plugins.emma = emmaConvention
class EmmaPluginConvention{
def verbosityLevel = "info"
def reportPath;
def coverageFileName;
def tmpDir;
def instrDir;
def metaDataFilePath;
def emma(Closure close){
close.delegate = this;
close.run()
}
EmmaPluginConvention(Project project){
reportPath = "${project.reportsDir.absolutePath}/emma"
coverageFileName = "coverage"
tmpDir = "${project.buildDir}/tmp/emma"
instrDir = "${tmpDir}/instr"
metaDataFilePath = "${tmpDir}/metadata.emma"
}
}
test {
// add EMMA related JVM args to our tests
jvmArgs "-XX:-UseSplitVerifier", "-Demma.coverage.out.file=$buildDir/tmp/emma/metadata.emma",
"-Demma.coverage.out.merge=true", "-Demma.rt.control=false"
doFirst {
println "Instrumenting the classes at " + sourceSets.main.output.classesDir.absolutePath
// define the custom EMMA ant tasks
ant.taskdef( resource:"emma_ant.properties", classpath: configurations.emma.asPath)
ant.path(id:"run.classpath") {
pathelement(location:sourceSets.main.output.classesDir.absolutePath)
}
def emmaInstDir = new File(sourceSets.main.output.classesDir.parentFile.parentFile, "tmp/emma/instr")
emmaInstDir.mkdirs()
println "Creating $emmaInstDir to instrument from " + sourceSets.main.output.classesDir.absolutePath
// instruct our compiled classes and store them at $buildDir/tmp/emma/instr
ant.emma(enabled: 'true', verbosity:'info'){
instr(merge:"true", destdir: emmaInstDir.absolutePath, instrpathref:"run.classpath",
metadatafile: new File(emmaInstDir, '/metadata.emma').absolutePath) {
instrpath {
fileset(dir:sourceSets.main.output.classesDir.absolutePath, includes:"**/*.class")
}
}
}
setClasspath(files("$buildDir/tmp/emma/instr") + configurations.emma + getClasspath())
}
// The report should be generated directly after the tests are done.
// We create three types (txt, html, xml) of reports here. Running your build script now should
// result in output like that:
doLast {
def srcDir = sourceSets.main.java.srcDirs.toArray()[0]
println "Creating test coverage reports for classes " + srcDir
def emmaInstDir = new File(sourceSets.main.output.classesDir.parentFile.parentFile, "tmp/emma")
ant.emma(enabled:"true"){
new File("$buildDir/reports/emma").mkdirs()
report(sourcepath: srcDir){
fileset(dir: emmaInstDir.absolutePath){
include(name:"**/*.emma")
}
txt(outfile:"$buildDir/reports/emma/coverage.txt")
html(outfile:"$buildDir/reports/emma/coverage.html")
xml(outfile:"$buildDir/reports/emma/coverage.xml")
}
}
println "Test coverage reports available at $buildDir/reports/emma."
println "txt: $buildDir/reports/emma/coverage.txt"
println "Test $buildDir/reports/emma/coverage.html"
println "Test $buildDir/reports/emma/coverage.xml"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment