On 11/04/10 10:14 PM, chrismolozian wrote:
Thank you for your continued support Adam. The problem with the ANTLR tool
not being detected by the ant-antlr3 task was caused by my naive assumption
that the declared dependencies were being passed to the task's classpath. I
have adjusted my build script and all is working a treat finally. The
complete build script is attached below so that it may be of use to
gradle+antlr3 users in the future (until the gradle antlr3 plugin becomes
official).
Note that the script may not conform to the gradle style, I am still
learning the best practices...etc (I'd appreciate any suggestions you have
that would simplify and/or condense the build script further).
A few remaining tweaks that I am unsure how to make:
1. In the compileJava task I have adjusted the source directory list to
include the generated-src directory. It would be great if I could simply add
it to the list without redeclaring 'src/main/java' as a source dir.
You can do this:
compileJava {
source 'build/generated/src'
}
The source() method adds to the existing set.
2. In the jar task I'm adding manifest attributes, is there a property I can
use to get the version of gradle used to build with. Is there a property for
the JDK version too. (I tried using gradle -r to check all properties and
could not see one for these details).
You can use the gradle.gradleVersion property to get the Gradle version.
Gradle doesn't provide a property for the JDK version, but you can use
the 'java.vm.version' system property.
3. Can you point me to gradle documentation about versioning practices and
how to control the major.minor.build mechanism in gradle.
There isn't really any specific documentation about versioning. One
option is to use a gradle.properties file:
http://gradle.org/0.9-preview-1/docs/userguide/tutorial_this_and_that.html#sec:gradle_properties_and_system_properties
So you could add a $projectDir/gradle.properties file with
majorVersion = 0
minorVersion = 1
and in your build file
buildVersion = timestamp
version = "${majorVersion}.${minorVersion}.${buildVersion}"
The version property can by any object, and its toString() method is
used to determine the actual version to use. So, you can code up your
versioning logic in an object. Here's an example:
version = new Version()
class Version {
def getMajor () { .. determine major version .. }
def getMinor() { .. determine minor number .. }
def getBuildNumber() {
if ( .. is CI build .. ) { .. get build number provided by CI
server .. }
else if ( .. is release build .. ) { '' }
else { .. calculate timestamp .. }
}
String toString() {
"${major}.${minor}.${buildNumber ?: ''}"
}
}
Then, you can also address the components of the version object
individually:
println version.major
println version.minor
println version.buildNumber
Another advantage of this approach is that you can move the definition
of the Version class to, say, the buildSrc project or a plugin.
In fact, one thing I'd like to do eventually is to create a plugin which
bundles up a handful of common versioning schemes like this, so you
could do something like (just as an example):
apply plugin: 'versionSchemes'
version {
fromPropertiesFile('version.properties') // get major, minor from
version.properties
builds.useTimestamp() // add a timestamp to non-release builds
}
version {
fromRepository() // get current major, minor from destination
repository, add 1 to the minor
builds.useScmRevision() // add the SCM revision to non-release builds
}
And so on. There's a bunch of algorithms we could support. This would
work well with a future 'release' plugin, as the release plugin would
know where the version numbers come from, and potentially how to update
them when performing the release.
Here is the build script:
apply plugin: 'java'
sourceCompatibility = 1.5
//major.version = '0'
//minor.version = '1'
//build.version = timestamp
project.version = "0.0.1"
repositories {
mavenCentral()
}
configurations {
grammarCompile
}
dependencies {
grammarCompile 'org.antlr:antlr:3.+'
compile group: 'org.antlr', name: 'antlr-runtime', version: '3.+'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
task generateGrammarSources (description: 'Invokes the antlr3-task from ant
to generate grammar sources')<< {
generatedDir = new File(project.buildDir.toString() + '/generated-src/')
generatedDir.mkdirs()
ant {
taskdef(name: 'antlr3', classname:
'org.apache.tools.ant.antlr.ANTLR3'){
classpath {
fileset(dir: 'build-deps', includes: 'antlr3-ant.jar')
}
}
antlr3( target: 'src/main/antlr3/Calculator.g',
outputdirectory: 'build/generated-src/'){
classpath {
pathelement(path: configurations.grammarCompile.asPath)
}
}
}
}
compileJava.dependsOn generateGrammarSources
compileJava.doFirst {
source = ['src/main/java', 'build/generated-src']
}
jar {
manifest {
attributes "Implementation-Title": project.name,
"Implementation-Version": project.version,
"Main-Class": "calculator.lang.Main",
"Created-By": "Gradle 0.9-preview-1",
"Build-Jdk": "1.6.0_17"
}
// adds compile dependencies to jar package
from configurations.compile.collect { it.isDirectory() ? it :
zipTree(it) }
}
Kind Regards,
Chris
PS: If it would be useful, I would be happy to add a cookbook entry
somewhere on using ANTLR3 with gradle. Not sure how I'd do this.
Adam Murdoch-2 wrote:
On 10/04/10 10:17 PM, chrismolozian wrote:
After further research and testing I discovered that the antlr-ant task
referenced by my build configuration only supported antlr version 2. It
seems that the antlr3-ant task is not on any repository that I can find,
to
solve this problem I have created a 'build-deps' folder in my project and
am
loading the local task jar to process the grammar files. My build script
looks like the following (note: I am using the latest version of gradle
from
trunk, version 0.9+):
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.antlr', name: 'antlr-runtime', version: '3.+'
compile group: 'commons-cli', name: 'commons-cli', version: '1.+'
testCompile group: 'org.antlr', name: 'gunit', version: '3.+'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
task generateGrammarSources (description: 'Invokes the antlr3-task from
ant
to generate grammar sources')<< {
generatedDir = new File(project.buildDir.toString() +
'/generated-src/')
generatedDir.mkdirs()
ant {
taskdef(name: 'antlr3', classname:
'org.apache.tools.ant.antlr.ANTLR3'){
classpath {
fileset(dir: 'build-deps', includes: 'antlr3-ant.jar')
}
}
antlr3(target: 'src/main/antlr3/Calculator.g',
outputdirectory: 'build/generated-src/')
}
}
compileJava.dependsOn generateGrammarSources
This still does not work. I need to add the ANTLR tool to the classpath
of
Ant so that it can compile the grammar files. The build error output is
here:
[ant:antlr3] Exception in thread "main" java.lang.NoClassDefFoundError:
org/antlr/Tool
Could you run gradle with the -s command-line option, so we can see the
stack trace for the failure?
--
Adam Murdoch
Gradle Developer
http://www.gradle.org
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
--
Adam Murdoch
Gradle Developer
http://www.gradle.org
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email