ant scp task

Ant SCP Task:

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

<project name="MyProject" default="copyToUnix" basedir=".">

	<target name="copyToUnix">
		<scp todir="user@vserver:/var/www/myFolder/" keyfile="/Users/dummy/.ssh/id_rsa">
			<fileset dir="WebContent">
				<include name="*.php" />
			</fileset>
		</scp>
	</target>
	
</project>

You need an additional jar file for ant scp task: http://www.jcraft.com/jsch/index.html

Using javadoc ant task

Javadoc is a very powerful and helpful tool of java. You can document your source, as well as insert installation or configuration information. The usage of javadoc in ant is very comfortable, setup once, use every build.
Here is an example of a simple javadoc ant task:

<target name="createJavadoc">
	<!-- this classpath is only necessary when using 3rd party libs or annotation processing -->
	<path id="annotations.classpath">
		<fileset dir=".">
			<include name="lib/**/*.jar" />
		</fileset>
	</path>
	
	<!-- creata javaDoc for package com.pbo.* inside src folder. Store javadoc in folder javaDoc -->
	<javadoc packagenames="com.pbo.*" 
		sourcepath="src" 
		defaultexcludes="yes" 
		destdir="javaDoc" 
		author="true" 
		version="true" 
		use="true" 
		classpathref="annotations.classpath" 
		splitindex="true" 
		nonavbar="true"
		windowtitle="F1 Betting Game : Documentation">
			<doctitle>
				<![CDATA[<h1>F1 Betting Game</h1>]]>
			</doctitle>
		<bottom>
			<![CDATA[Copyright © Philipp Boss.<br/> <b>Please contact Philipp Boss in case of questions</b>]]>
		</bottom>
	</javadoc>
</target>

This task creates the javadoc for the package com.pbo.* inside the src-folder and stores the created doc files inside the javaDoc folder.

The parameter nonavbar hides the navigation bar in the created doc files. For my opinion this generates more „clean“ javadoc pages. The windowtitle is the title of the html-page (what you can see in your browser tab), the doctitle is the one beeing displayed inside the web page. The value of bottom is beeing displayed at the end of the page.

Here you can see the created page:

Javadoc created Webpage
Javadoc created Webpage

Using SVN in Ant

In my current ant-script I need to get the SVN revision to insert that info into the deployment-package.

There are three commands available.

svn info is the first one:

D:\Projekte\workspace\myCode >svn info
Pfad: .
URL: https://svn.xxx.xx/abc/def/
Basis des Projektarchivs: https://svn.svn.xxx.xx/abc/def/
UUID des Projektarchivs: hidden
Revision: 12955
Knotentyp: Verzeichnis
Plan: normal
Letzter Autor: pbo
Letzte geänderte Rev: 12859
Letztes Änderungsdatum: 2014-05-21 10:10:14 +0200 (Mi, 21. Mai 2014)

The Revision is the current Revision of the Repository (that means, that if you checkin some changes now, you will get the Revsion 12955+1). The „Letzte geänderte Rev“ (last changed revision) ist the one which was assigned on the last commit/checkin. This is the revision of code that is checked in on the repository. This format is diffiult to handle in ant, that’s the reason why I present another solution:

svnversion

svnversion without any parameter will display the revsion of the repositry:

D:\Projekte\workspace\myCode>svnversion
12955M

„M“ means, that the local code has changed (modified). If you need the latest commited revision, you can append the parameter „-c“:

D:\Projekte\workspace\myCode>svnversion -c
11789:12859M

The firs number is the revision that was assigned during first checkin of this project / path. The second number is the revision of the latest commit. There is also a third solution:

svn log

With the svn command with the log parameter you are able to see the commit-messages. With some parameters you can display only the latest commit, including revsion number, date and author:

D:\Projekte\workspace\myCode>svn log -r COMMITTED -q
------------------------------------------------------------------------
12859| pbo | 2014-05-21 10:10:14 +0200 (Mi, 21. Mai 2014)
------------------------------------------------------------------------

How to use this in ant:

<target name="svnversion">
	<exec executable="svnversion" outputproperty="svnversion" />
	<echo message="SVN Version: ${svnversion}" />
</target>

Results in:

Buildfile: D:\Projekte\workspace\myCode\svn.xml
svnversion:
     [echo] SVN Version: 129555M
BUILD SUCCESSFUL
Total time: 834 milliseconds

 

<target name="svnLog">
	<exec executable="svn" outputproperty="lastCommittedRevision">
		<arg value="log" />
		<arg value="-r" />
		<arg value="COMMITTED" />
		<arg value="-q" />
	</exec>
	<echo message="lastCommittedRevision = " />
	<echo message="${lastCommittedRevision}" />
</target>

Results in:

Buildfile: D:\Projekte\workspace\myCode\svn.xml
svnLog:
     [echo] lastCommittedRevision = 
     [echo] ------------------------------------------------------------------------
     [echo] r12859 | pbo | 2014-05-21 10:10:14 +0200 (Mi, 21. Mai 2014)
     [echo] ------------------------------------------------------------------------
BUILD SUCCESSFUL
Total time: 1 second

 

Apache Ant – Copy files to ftp server

Sample for copying files to an FTP Server:

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" basedir="." default="deploy">
	<target name="deploy">

		<ftp server="myserver.de"
	   port="21"
	   remotedir="/html/testing/"
	   userid="myUser"
	   password="myPassword"
	   passive="yes"
	   depends="yes"
	   binary="no">
			<fileset dir="Testing">
				<include name="*.php"/>
				<include name="*.js"/>
			</fileset>
		</ftp>

	</target>
</project>

By default, ant is not able to process this, because the ftp classes are not included by the typical ant installation.

To enable ant to use ftp tasks, you have to download the apache commons ftp package. The dependencies of Apache Ant can be found here: http://ant.apache.org/manual/Tasks/ftp.html

Place the downlaoded commons-net.jar file into the lib folder of your ant installation. Now you can execute the ant tasks in your cmd shell.

If you have placed the jar-file outside the ant-lib directory you can run the ant-script with the lib-parameter:

ant -lib "/home/philipp/java/libs/commons-net-3.3/commons-net-3.3.jar"

Links:

Apache commons net: http://ant.apache.org/manual/install.html#commons-net

Ant FTP Task: http://ant.apache.org/manual/Tasks/ftp.html

versioned jar with ant

Versioned jars (like MyCode-1.2.jar) can be easily generated with ant.

First you have to set the necessary Properties:

<property name="major-version-number" value="0" />
<property name="project-name" value="${ant.project.name}" />
<property name="src" value="src"/>
<property name="build" value="bin" />

Then you need a compile-task:

<target name="compile" description="compile the source" >
    <javac srcdir="${src}" destdir="${build}"/>
</target>

Then you need a method for generating jars:

<target name="jar-version">
  <buildnumber />
  <property name="version-number" value="${major-version-number}.${build.number}" />
  <jar basedir="bin" destfile="build-version/${project-name}-${version-number}.jar">
    <manifest>
     <attribute name="Built-By" value="${builder}" />
     <attribute name="Built-On" value="${build-info.current-date}" />
     <attribute name="Built-At" value="${build-info.current-time}" />
     <attribute name="Implementation-Version" value="${version-number}" />
    </manifest>
  </jar>
</target>

At last you need a method that compiles the files, generates the build-number and the jar-File:

<target name="build-jar-version">
    <antcall target="compile" />
    <mkdir dir="build-version" />
    <antcall target="jar-version" />
</target>