Archive for the ‘Java’ Category
Oracle JDK 7 Available for Mac
Download from here: http://www.oracle.com/technetwork/java/javase/downloads/index.html.
IntelliJ IDEA OpenSourced :-)
IntelliJ IDEA 9.0 will be OpenSourced under Apache 1.0 License. There will be a fully-loaded commercial edition also available. Check out the feature differences between the Community Edition and Ultimate Edition.
Guava Libraries from Google
Discovered from a blog post Apache Commons-like Java library from Google: Guava Libraries. Check out their presentation which briefs the functionality.
New releases in Java space
There have been some prominent releases in the past few weeks:
- Apache POI 3.5-FINAL: For reading/writing Microsoft Office format files from Java.
- Jetty 7.0: A servlet container akin to Tomcat. Easily embeddable and ideal for use in testing.
- XOM 1.2.3: New version of the XML parser written by Elliotte Rusty Harold.
Reduce Java boiler-plate code using Lombok
We all hate the verbocity of Java. We all hate getters and setters and toString and hashCode and equals. We also hate the lack of closure support for proper resource handling. Now you can still love your favorite language without doing these manual chores!
There is a fine OpenSource project, oddly named as Project Lombok which fixes the boiler plate code writing (or generation) using annotations. Beautifully designed, this is some thing which could save you lot of time. Checkout their video (available in the home page).
XSL Transform using Java
import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; ... // The XSL source: StreamSource xsl = new StreamSource(...); // Create the transformer: TransformerFactory factory = TransformerFactory.newInstance(); Templates template = factory.newTemplates(xsl); // can throw TransformerConfigurationException (parent exception is: TransformerException) Transformer transformer = template.newTransformer(); // can throw TransformerConfigurationException // The data XML source StreamSource data = new StreamSource(...); // The transformed output: StreamResult out = new StreamResult(...); // Transform using the Transformer instance: transformer.transform(data, out); // can throw TransformerException
Have a look at the StreamSource API and StreamResult API to understand the various ways by which it can be instantiated.
Maven Profiles
Thanks to Arun Jeganath for making me understand Maven Profiles.
Often we are forced to specify different build parameters in different environments. In the ages of Make, these parameters used to be specified as environment variables. In the world of Maven, we have profiles.
Profiles: Introduction
A sample profile information:
<profiles>
<!-- DEV Profile (Default) -->
<profile>
<id>dev-profile</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
<activeByDefault>true</activeByDefault> <!-- Note this -->
</activation>
<!-- Liferay Home in DEV -->
<properties>
<liferay-home>${user.home}/apps/liferay-portal-tomcat-6.0-5.1.2</liferay-home>
</properties>
</profile>
<!-- TEST Profile -->
<profile>
<id>test-profile</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
<!-- Liferay Home in TEST -->
<properties>
<liferay-home>/opt/apps/liferay-portal-tomcat-6.0-5.1.2</liferay-home>
</properties>
</profile>
</profiles>
Quick learning from the above example:
- There can be number of profiles defined.
- A profile can be marked as <activeByDefault>. This will enforce the configuration defined in the activated profile to take effect during build.
- To enable a specific profile during build, the value defined in XPath /profiles/profile/activation/property needs to be passed the Maven as System Property (in our case: -Denv=test).
- The property defined in the profile (liferay-home) can be referenced elsewhere in the pom.xml as ${liferay-home}.
Placing profile information
Profile information is generally placed in:
- In the project POM itself.
- Inside {user.home}/.m2/settings.xml.
In the first case (in this particular example), the deployment information is placed in the project’s POM. This, some might feel improper because the module/project build has hardcoded environment dependencies which might not work in other systems. The second option externalizes the platform specific build information. But a new developer checking out the project will not be aware of this external dependency causing his build to fail. Both these problems might be addressed using documentation: some projects have README and BUILD files explaining environment setup for build.
As we saw, both methods have advantages and disadvantages. Choose the method appropriate to your situation.
Parameters that can be overridden in profiles
Note: This list is shamelessly copied from the official Maven profiles guide. Refer to this guide for detailed information.
- <repositories>
- <pluginRepositories>
- <dependencies>
- <plugins>
- <properties> (not actually available in the main POM, but used behind the scenes)
- <modules>
- <reporting>
- <dependencyManagement>
- <distributionManagement>
- a subset of the <build> element, which consists of:
- <defaultGoal>
- <resources>
- <testResources>
- <finalName>
Building Alfresco AMPs Using Maven
[2009-12-03] Updated for new Maven repository.
Thanks to Ganesh Gembali and Velrajan for helping me with this post.
One thing I hate about Alfresco is it’s SDK. I hate to learn new methodologies and configurations specific to new software. When it comes to developing Alfresco modules and extensions, it was difficult to do it without configuring Alfresco SDK. But that is history
Alfresco Maven Archetypes
I will discuss the steps I followed to configure a new Alfresco module project using Maven Alfresco AMP Archetype.
Step 1: Configuring Alfresco Repositories
Add the following repositories to your Maven configuration:
<repositories>
<repository>
<id>alf-public</id>
<url>http://maven.alfresco.com/nexus/content/repositories/releases</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>alf-public-plugin</id>
<url>http://maven.alfresco.com/nexus/content/repositories/releases</url>
</pluginRepository>
</pluginRepositories>
Step 2: Using archetype:generate to generate a new project structure
mvn archetype:generate \ -DarchetypeCatalog=http://maven.alfresco.com/nexus/content/repositories/releases/alfresco-archetype-catalog.xml
Actually the above code did not work in our proxy environment (I have configured Maven for Proxy usage correctly). So I saved the alfresco-archetype-catalog.xml to local filesystem and referenced it thus:
mvn archetype:generate \ -DarchetypeCatalog=file:///home/subhash/alfresco-archetype-catalog.xml
The application quizzes you about artifactId, groupId, package name and version. Give the details. It will create the project directory.
Step 3: Placement of files
The following table will help you understand where to place your source files. The third column (AMP path) describes the path where the source file will be packaged inside the AMP. The fourth column (Deployment Path) the path where the file will be installed inside Alfresco deployment.
| Source Path | Description | AMP path | Deployment Path |
|---|---|---|---|
| src/main/config | Place your module configuration files (module-context.xml) here. | config/alfresco/module/{groupId}.{artifactId}/ | WEB-INF/classes/alfresco/module/{groupId}.{artifactId}/ |
| src/main/java | Place your Java source code | lib/{artifactId}-{version}.jar | WEB-INF/lib/{artifactId}-{version}.jar |
| src/main/resources | All other files like JavaScript, FTL templates, WebScript XMLs, etc. | config/ | WEB-INF/classes/ |
| module.properties | module.properties is stored in the root directory. | module.properties | WEB-INF/classes/alfresco/module/{artifactId}/module.properties |
Step 4: Configuring Alfresco Dependencies
As an example, our module depended on alfresco-webscript-framework and alfresco-core APIs:
<dependency>
<groupId>org.alfresco</groupId>
<artifactId>alfresco-webscript-framework</artifactId>
<version>3.2r</version>
<classifier>community</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.alfresco</groupId>
<artifactId>alfresco-core</artifactId>
<version>3.2r</version>
<classifier>community</classifier>
<scope>provided</scope>
</dependency>
Two things to note:
- The <classifier> element is needed. The Jar files in the repository is named as {artifactId}-{version}-{classifier}.jar
- Always give the scope as provided. This ensures Alfresco Jars do not get bundled with your AMP.
Terracotta Announces Purchase of Ehcache
What happens when two respectable enterprise Java scalability providers (Ehcache and Terracotta) join together? I am eager to witness the interesting time ahead! Get the news here.
Netbeans, Eclipse new releases
Eclipse Foundation released Eclipse Galileo on 24th June. Netbeans released version 6.7 on 29th June. Nice to see healthy competition from both sides
. Download the latest versions of Netbeans and Eclipse.
Maven managed projects: Generating dependency graph
When using Maven from commandline, use this command:
If you are using Netbeans 6.7, you can generate a much more visual graph:
For bringing up this graph, just right click on the project and select Show Dependency Graph.
The screenshots above show the dependency graph for my project WizTools.org RESTClient.
Netbeans 6.7 RC 2
I have been test-driving Netbeans 6.7 RC 2 for two days now. Has been pretty stable. It is also the first version with native Maven support.
“Hello World” (GUI) example in JavaFX
package tutorial;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
def WIDTH = 160;
def HEIGHT = 80;
def MESSAGE = "Hello World!";
Stage {
title: MESSAGE
width: WIDTH
height: HEIGHT
scene: Scene {
fill: Color.BLACK
content:
Text {
font: Font.font("Serif", FontWeight.BOLD, 16)
fill: Color.WHITE
x: 10
y: 30
content: MESSAGE
}
}
}
Command line “Hello World!” in JavaFX
Download JavaFX SDK. Install it, and have its bin folder in your PATH.
Open your favorite text editor, and write:
println("Hello World!");
java.lang.System.exit(0);
Save the file as A.fx. To compile the code:
$ javafxc A.fx
This will generate the class file A.class. To run the application:
$ javafx A
Isn’t it simple?
JavaFX 1.2 for Linux, Mac and Solaris
JavaFX is now available for Linux, Solaris and Mac. Finally, now I can try this technology (I use Linux at work and home)!


