Posts Tagged ‘ant’

Ant task to execute a main class with command line args parameters

Yeah, once again another HelloWorld stuff on the web. I was just curious to try to execute a Main class with an Ant script and found out that I couldn´t find the straight working snippets online easily, because I couldn´t get to know how to set the classpath. So I have wasted some minutes to make it work.

Let´s consider a little more than the classic HelloWorld example and pass a command line parameter as well:

package de.demo;

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello Main! \n"  + "Parameter: "+ args[0]);


Then run following build.xml script:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project basedir="." default="run" name="Ant Hello World">
	<property name="src" value="." />

	<path id="classpath">
		<fileset dir="${src}">

	<target name="compile">
		<javac srcdir="." />

	<target name="run" depends="compile">
		<!-- Print directly in the console -->
		<echo message="Hello World!" />
		<!-- Run main class with parameters-->
		<java classname="de.demo.HelloWorld">
			<arg value="10"/>
			<classpath refid="classpath">


In the console you will see something like:

Buildfile: C:\Users\liparulol\workspace\AntDemos\buildHelloWorld.xml
[javac] C:\Users\liparulol\workspace\AntDemos\buildHelloWorld.xml:14: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file
[echo] Hello World!
1 Hello Main!
1 Parameter10
Total time: 745 milliseconds

That´s it!

Liferay Service Builder with external database

To make a Liferay Service Builder with external database there are some rules to follow. This post will help you to get straight to the point.
Keep in mind that:

  • You can’t deploy more than one portlet mapping column with the same entity name, otherwise you get a hot-Deployment error.
  • the names of the portlet name, namespace and database must be distinct (ex. portlet=”imageview“, namespace=”mammography“, database=”dicomviewer“).
  • when you add external jar, you must add them in the Tomcat/lib/ext
    so liferay can find them in the global classpath.

The steps to make the service builder with the external database mapping are:

  1. make the service.xml file in the /docroot/WEB-INF/
  2. launch the build-service ant command
  3. add the ext-spring.xml file in the /docroot/WEB-INF/src/META-INF/ folder
  4. deploy the portlet.

The example below might be helpful.

—service.xml ————

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "">

<service-builder package-path="it.dicom">
<author>Laura Liparulo</author>

<entity name="Volume" local-service="true" remote-service="true" table="volume"
data-source="mammographyDataSource" session-factory="mammographySessionFactory" tx-manager="mammographyTransactionManager">
<!-- PK Fields -->
<column name="volumeId" type="long" primary="true" />
<!-- Other Fields -->
<column name="volumeName" type="String" />
<!-- Relationships -->
<column name="caseVolume" type="Collection" entity="CaseArchive"
mapping-key="volumeId" />
<order by="asc">
<order-column name="volumeName" />

<finder name="Volume_Name" return-type="Collection">
<finder-column name="volumeName" />

<entity name="CaseArchive" local-service="true" remote-service="true" table="case_archive"
data-source="mammographyDataSource" session-factory="mammographySessionFactory" tx-manager="mammographyTransactionManager">
<!-- PK Fields -->
<column name="caseId" type="long" primary="true" />
<!-- Other Fields -->
<column name="caseName" type="String" />
<column name="volumeId" type="long" />
<column name="notes" type="String" />
<!-- Relationships -->
<column name="image_Case" type="Collection" entity="Image"
mapping-key="caseId" />
<order by="asc">
<order-column name="caseName" />
<finder name="Case_Name" return-type="Collection">
<finder-column name="caseName" />



—ext-spring.xml ————

<?xml version="1.0"?>
<bean id="mammographyDataSourceTarget"
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dicomviewer" />
<property name="username" value="root" />

<property name="password" value="tigertailz" />
<bean id="mammographyDataSource"
<property name="targetDataSource">
<ref bean="mammographyDataSourceTarget" />
<bean id="mammographyHibernateSessionFactory"
<property name="dataSource">
<ref bean="mammographyDataSource" />
<bean id="mammographySessionFactory">
<property name="sessionFactoryImplementor">
<ref bean="mammographyHibernateSessionFactory" />

<bean id="mammographyTransactionManager"
<property name="dataSource">
<ref bean="mammographyDataSource" />
<property name="sessionFactory">
<ref bean="mammographyHibernateSessionFactory" />

Now you can make it fast as a shark! 🙂