Apache Pivot is a very interesting new RIA platform. You can think of it as a competitor to Flex, Silverlight, and JavaFX. Unlike all three of those languages, it uses Java and XML – two generally familiar languages, to create applications. It’s structured similar to Flex – it has an XML file that describes an application’s layout and allows enough functionality to create simple and not so simple applications with just XML with embedded scripts. Unlike Flex, the “code behind” language is straight up Java. It’s deployment model is just like an applet’s, so it’s ubiquitous. When you dig deeper, there are lots of little niceties – things that are simple and make sense. It’s nice to work with.
This post describes how to set up a maven project to build and deploy an Apache Pivot application. Once Pivot is in the maven central repo (it’s not quite out of incubator as of this writing), then the result of this post can be made into an archetype and contributed back. The result of this post is is this GitHub repository commit. (Git is very nifty – the fact that the entire set of files is available from one Git commit id is, well, wicked, as we say in Massachusetts.)
We’ll steal the code from the Pivot StockTracker tutorial, since it will be extended with a graph and used as an example of how and why to use the EventBus in UIs. The examples will be posted on the EventBus.org blog soon. I’ll add a blog entry here when the eventbus.org blog post is available.
Start by using the maven quickstart to create a blank Java project from an archetype:
mvn archetype:create -DgroupId=org.eventbus.tutorial -DartifactId=stocktracker cd stocktracker mvn install
To populate the example, we can pull the StockTracker sources to the our package in the src/main/java directory and delete the files we don’t need:
mkdir src/main/java/org/eventbus/tutorials/pivot/stocktracker cd src/main/java/org/eventbus/tutorials/pivot/stocktracker svn co http://svn.apache.org/repos/asf/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/ . rm -rf .svn rm App.java rm ../../../../../../../test/java/org/eventbus/tutorial/AppTest.java
According to Maven conventions, only the java code belongs in this directory, so let’s move the other files to the resource directory so that they can easily be picked up from the jar at runtime. A better practice may be to make image, json, and wtkx directory, or maybe make a main/wtkx directory for the latter, but let’s keep it simple. The community may come up with better standards for Pivot development with Maven.
cd ../../../../../.. mkdir resources mv java/org/eventbus/tutorial/*.json resources/org/eventbus/tutorials/pivot/stocktracker mv java/org/eventbus/tutorial/*.png resources/org/eventbus/tutorials/pivot/stocktracker mv java/org/eventbus/tutorial/*.wtkx resources/org/eventbus/tutorials/pivot/stocktracker
Change all the packages in the Java files to the new package.
find . -name *.java -print | xargs sed -i -e 's/org.apache.pivot.tutorials.stocktracker/org.eventbus.tutorials.pivot.stocktracker/g' find . -name *.wtkx -print | xargs sed -i -e 's/org.apache.pivot.tutorials.stocktracker/org.eventbus.tutorials.pivot.stocktracker/g'
(This is why I keep such detailed blogs. It took me while to figure out that the -e is required on Mac, but not Linux, and I’m sure I’ll forget again, but I’ll remember that I had to do this before.)
The maven default build uses Java 1.3 source, even though Java 1.5 is about to be obsoleted as of this writing, so we’ll have to tell maven to get with the new decade and use Java 6 (I think Pivot will work for Java 5 too) by adding this before <dependencies>:
<build> <sourceDirectory>src</sourceDirectory> <testSourceDirectory>test/src</testSourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build>
Pivot is not in the central maven repo yet, so we have to install it locally, which means building it first. The Pivot team is rock solid, pulling and building the project is easy and reliable. See the BUILD file in the svn trunk for details, but on a Mac using Java 6, it’s like this:
cd ~ mkdir pivot svn co http://svn.apache.org/repos/asf/incubator/pivot/trunk/ . export CLASSPATH=~/dev/maven-ant-tasks-2.1.0.jar:~/dev/junit/junit-4.8.1.jar:/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/plugin.jar:/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/javaws.jar ant maven-install
This builds all the pivot jars using ant and installs them to your local maven repo.
Now go back to our project and add the pivot dependencies to pom.xml:
<dependencies> <dependency> <groupId>org.apache.pivot</groupId> <artifactId>pivot-core</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.pivot</groupId> <artifactId>pivot-wtk</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.pivot</groupId> <artifactId>pivot-web</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.pivot</groupId> <artifactId>pivot-wtk-terra</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.pivot</groupId> <artifactId>pivot-charts</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
You might not need charts, but I will for the EventBus stuff coming soon.
should lead to a successful build.
It would be real nice to generate an executable jar file that has the entire classpath in it so that launching the app is a simple java -jar command. To do this add the following as a plugin after the compiler plugin configuration shown above:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <goals> <goal>attached</goal> </goals> <phase>package</phase> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>org.eventbus.tutorials.pivot.stocktracker.StockTracker</mainClass> </manifest> </archive> </configuration> </execution> </executions> </plugin>
Now run build again and launch the app:
mvn clean install
java -jar target/stocktracker-1.0-SNAPSHOT-jar-with-dependencies.jar