Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Wednesday, January 7, 2009

Ant and gui components ( Groovy Swing )

What to do if you want to extend ant scripts functionality with some gui features i.e. wizards for the beginners etc. ? I was facing the same problem - I needed some quick and nice solution so my choice was Swing and Java, well not directly - it's Groovy and SwingBuilder. It's even easier :)

Two things, we need to create our component in Groovy and integrate it with Ant's environment.

Component's key parts and structure ( Groovy ):

Some explanations:

We need to add the code preventing from the thread termination due to Ant policies. Ant executes target and ends its work by terminating its thread and all sub threads.

isAlive - flag which indicates whether component is alive or not



class Configurator extends SoftwareBuild {

boolean isAlive = true
SwingBuilder swing = null
JFrame frame = null

public void init(AntBuilder providerAntBuilder) {
if (swing == null || frame == null) {
swing = new SwingBuilder()
frame = swing.frame()
}
super.init(providerAntBuilder);
}


public void start() {

frame = swing.frame(title: 'Vine configurator', size: [800, 600],
defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE,
addShutdownHook { isAlive = false}) {

}

frame.show()

//To keep this tread alive - prevents from being closed by ant
while (isAlive) {
sleep(1000)
}

}

Invocation of our component from the ant script level ( Ant's build.xml ):




<target name="gui" description="Displays gui for Vine configuration">
<groovy>

ant.gui.Configurator configurator = new ant.gui.Configurator()
try {
// from the level of Ant's build script we're passing a reference to the ant environment
configurator.init(ant)
// displaying our swing component
configurator.start()
} catch (Exception e) {
e.printStackTrace()
}

</groovy>
</target>


Ant the effect of this work is ( based on the example from the Vine Toolkit ):



Wednesday, December 31, 2008

mxmlc ant Flex task in Groovy

Hi,

lately, there was a need to add a flex compilation feature to the Vine Toolkit build system. In the build scripts we're using Groovy as our extension to Ant scripts so I designed some additional logic to automatically choose source files and compile them in a special manner:

  • compile only source files with the Vine Toolkit extensions,
  • add locale bundles,
  • add dependent source paths,
  • add external libraries.

I'm presenting this code because not all options were mapped correctly in ant task def, especially the external library path was pretty troublesome :)

Here is the code snippet:

flexAppPath - path to the flex sources.
flexLocalePath - path to the flex locale bundles.
dependentSourcePath - path to dependent flex sources.
flexLibPath - path to the external swc libraries.



antBuilder.fileset(id: "flexSourcesList", dir: flexAppPath,
includes: "**/*.mxml") {
antBuilder.contains(text: "VineApplication")

if (new File(flexLibPath).exists()) {
antBuilder.fileset(id: "flexLibList", dir: flexLibPath,
includes: "**/*.swc")

}


antBuilder.project.references.flexSourcesList.each {
println "Compiling " + it.name
antBuilder.mxmlc(
file: it) {
antBuilder.'use-network'('true')
antBuilder.'compiler.strict'('true')
antBuilder.'compiler.context-root'('gridsphere')
antBuilder.'compiler.services'(applicationBuild.getBuildAptPath() + '/WEB-INF/services-config.xml')
println "* Using services-config.xml file from: " + applicationBuild.getBuildAptPath() + '/WEB-INF/services-config.xml'
println "** Adding locale source path: " + flexLocalePath + '/{locale}'
antBuilder.'compiler.source-path'(flexLocalePath + '/{locale}')

new File(flexLocalePath).listFiles().each {
if (!it.name.startsWith(".")) {
println "** Adding locale: " + it.name
antBuilder.'compiler.locale'(it.name)
}
}

antBuilder.'compiler.source-path'('path-element': flexAppPath)
println "*** Adding dependency from: " + flexAppPath

usesProjectBuildList.each {
String dependentSourcePath = it + 'flex/src'
antBuilder.'compiler.source-path'('path-element': dependentSourcePath)
println "*** Adding dependency from: " + dependentSourcePath

}
antBuilder.project.references.flexLibList.each {
String libraryPath = it
println "**** Adding external library: " + libraryPath
antBuilder.'compiler.library-path'('file': libraryPath, 'append': 'true')
}
}



I especially like this code for listing files:



new File(flexLocalePath).listFiles().each {
if (!it.name.startsWith(".")) {
println "** Adding locale: " + it.name
antBuilder.'compiler.locale'(it.name)
}
}


This is the pure power of Groovy especially in conjunction with ant features.

Enjoy :)

Friday, December 19, 2008

To begin with..

Hi,

just few words of introduction.  The main purpose of this blog is to share my ideas regarding Web UI with you. Many times sites like this were saving me a lot of time and now I'd like to give something in exchange :) I'll mainly focus on the Flex / BlazeDs issues as well as on the Groovy scripting language since I'm using it to enhance Ant build scripts in our framework. Talking about that, I'm the core developer in the Vine Toolkit project and most cases my posts will be the solutions to problems I have faced there. I'll also do a brief presentation of this framework - it's the glue between Flex/BlazeDs and the web application containers like Tomcat and it could be deployed in a portlet containers ( Gridsphere ) as a set of integrated portlet components.

That all for now.

Cheers,

Piotr