Copy entire directory before creating an ear file - ant

I am trying to build an ear file from an ant script. There are several targets that create .jar and .war (to be contained within the ear) files using different projects and these are building without issue so I will not include them.
Imagine this directory structure:-
Project1/
build/
lib/
META-INF/
build.xml
So when the ant script is called the build directory is deleted and remade, all fairly standard stuff. Then I create the jar's and war's from external projects and store them in build/ - everything is fine.
But I also want to include the directories lib/ and META-INF/ in the ear file. So I try to copy them to the build directory using this target.
<target name="file_cleanup">
<copy todir="${build}">
<fileset dir="lib/"/>
<fileset dir="META-INF/"/>
</copy>
</target>
This file_cleanup target is a dependant of the default build target which creates the ear - shown below:
<target name="ear" depends="initialise, file_cleanup, other targets...">
<ear destfile="My.ear" appxml="META-INF/application.xml">
<fileset dir="${build}" includes="*.jar,*.war"/>
</ear>
</target>
What I want to see when I extract the ear is:
target1.jar
target2.war
lib/
META-INF/
But what I actually get is:
target1.jar
target2.war
and all of the contents of both the lib and META-INF directories...

I was able to resolve this issue by creating additional properties and directories and copying the directory structures to the new directories:
<target name="initialise">
<delete dir="${build}"/>
<mkdir dir="${build}"/>
<mkdir dir="${build}/${lib}"/>
<mkdir dir="${build}/${meta-inf}"/>
</target>
<target name="file_cleanup">
<copy todir="${build}/${lib}">
<fileset dir="lib"/>
</copy>
<copy todir="${build}/${meta-inf}">
<fileset dir="META-INF"/>
</copy>
</target>

Related

How to compile each jasper file to its own directory?

I'm trying to create a ant script to compile my jasper files, but I have many "srcdir" and "destdir":
<target name="all">
<jrc
srcdir="many..."
destdir="many..."
tempdir="any"
xmlvalidation="true">
<classpath refid="classpath"/>
<include name="**/*.jrxml"/>
</jrc>
</target>
...and I would like it to compile each file to it's own dir. For every ".jrxml" file.
Is there a way?
You can use ant-contrib foreach task to loop over each jrxml file and call the jrc task for each of those. If you don't have it, you'll need to install ant-contrib by copying its JAR file to the lib directory of your Ant installation (if you're using Eclipse, you can add it by going to "Window > Preferences > Ant > Runtime" and adding the JAR into "Ant Home Entries").
The following defines a target "all" that will select all the jrxml files under the current directory. For each of those file, the "jrc" target will be called and the corresponding file will be referenced by the property jrxml.file.
Inside this task, the directory where the jrxml file is located is retrieved with the dirname task and the name of the jrxml file is retrieved with the basename task. The built .jasper file will be created under a folder having the same name as the jrxml file. (It needs to be created first with the mkdir task).
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<target name="all">
<foreach target="jrc" param="jrxml.file">
<path>
<fileset dir=".">
<include name="**/*.jrxml"/>
</fileset>
</path>
</foreach>
</target>
<target name="jrc">
<dirname property="jrxml.dir" file="${jrxml.file}"/>
<basename property="jrxml.filename" file="${jrxml.file}" suffix="jrxml"/>
<mkdir dir="${jrxml.dir}/${jrxml.filename}"/>
<jrc srcdir="${jrxml.dir}"
destdir="${jrxml.dir}/${jrxml.filename}"
tempdir="${jrxml.dir}/${jrxml.filename}"
xmlvalidation="true">
<classpath refid="classpath"/>
<include name="${jrxml.filename}.jrxml"/>
</jrc>
</target>
As an example, if you have a structure:
+folder
+--jrxml
+----Example1.jrxml
+----Example2.jrxml
the result will be
+folder
+--jrxml
+----Example1.jrxml
+----Example1
+------Example1.jasper
+----Example2.jrxml
+----Example2
+------Example2.jasper

Include commons-net.jar at the custom location instead of ant install location

I am using ftp task from ant. For ftp to work, I need commons-net.jar as dependency at
ANT_HOME/lib folder.
As best practice, I am following the folder structure to keep all external folders under customized External Jars folder. Is there a way to keep commons-net.jar at customized folder instead of
ANT_HOME/lib folder?
Another option is to place plugin jars in the "$HOME/.ant/lib" directory.
You can automate the install of your dependent jars as follows:
<project name="demo" default="build">
<available classname="org.apache.commons.net.ftp.FTP" property="ftp.installed"/>
<target name="init" unless="ftp.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/commons-net.jar" src="http://search.maven.org/remotecontent?filepath=commons-net/commons-net/3.3/commons-net-3.3.jar"/>
<get dest="${user.home}/.ant/lib/oro.jar" src="http://search.maven.org/remotecontent?filepath=oro/oro/2.0.8/oro-2.0.8.jar"/>
<fail message="FTP task installed. Run ANT again"/>
</target>
<target name="build" depends="init">
<ftp server="ftp.apache.org" userid="anonymous" password="me#myorg.com">
<fileset dir="htdocs/manual"/>
</ftp>
</target>
</project>

How do you create a classpath in ANT that includes all jars in a directory and it's subdirectories

I'm using ANT to build a Java project that has dependency jars during compile time. The dependency jars are nested inside a directory. What this means is, the directory contains sub-directories (which might contain sub-directories) and jars as well.
I want to create an ANT element to include all the jars inside the directory. Below is my code but doesn't work. I'm wondering if this can't be done in ANT or I'm doing something wrong:
<path id="javaee.classpath">
<fileset dir="${javaee.modules.dir}">
<patternset>
<include name="**/*.jar" />
</patternset>
</fileset>
</path>

Ant build script to create EAR with multiple WARs

I want to create Ant build script to build EAR from multiple WARs. I have 3 different web projects(3 different WARs). I need to know where I should keep the build.xml which builds EAR.
Should I create a new EAR project in the parent folder of those 3 projects?
Have you considered looking at Gradle. Gradle has support for multi-module projects
and if you follow their conventions, the build scripts are fairly straight-forward. For this project, you would have three projects that generate a war (from each) and then a parent project that creates the ear and pulls in the war artifacts to create the ear file.
Worth a look, but I know you asked about Ant.
I have parent project with build.xml and subfolders containing individual modules
build.xml
ear-module/
ejb-module/
web-module/
The build.xml calls ant for each module and then assembles them together to result ear.
This is sample from single-ant having multiple modules. I prefer to have individual ant files in each module that is called from master folder. Dependencies can be tricky in ant. This is probably the reason why people favour maven/graddle over ant.
<target name="war" depends="ejb">
<mkdir dir="${build.dir}/war" />
<path id="web-classpath">
<path refid="classpath" />
<file file="${build.dir}/${ant.project.name}.jar" />
</path>
<javac srcdir="web-module/java" classpathref="web-classpath" destdir="${build.dir}/war" debug="true"
source="${compiler.source.level}" target="${compiler.target.level}" encoding="${src.encoding}"/>
<war destfile="${build.dir}/${ant.project.name}.war" webxml="web-module/war/WEB-INF/web.xml">
<webinf dir="web-module/war/WEB-INF">
<exclude name="web.xml"/>
<exclude name="classes/**"/>
<exclude name="lib/**"/>
</webinf>
<classes dir="${build.dir}/war" />
<lib dir="web-module/war/WEB-INF/lib"/>
<fileset dir="web-module/war">
<exclude name="WEB-INF/*"/>
<exclude name="META-INF/*"/>
</fileset>
</war>
</target>

Ant archiving subfolders

I have next trouble. I have one folder("FirstFolder") and 3 subfolders("1", "2", "3"). I want to archive this folders in next - 1.acp, 2.acp, 3.acp and placed in other folder. It's important - I do not know what are called sub-folders and their number!!! I don't found solutions for this and write simple ant task-
<target name="start">
<foreach target="zipAcp" param="Files">
<path>
<dirset dir="src/main/bootstrap"/>
</path>
</foreach>
</target>
<target name="zipAcp">
<zip destfile="target/classes/alfresco/extension/agilent/${Files}.acp" basedir="src/main/bootstrap"/>
</target>
But i don't know how get directory name(1), but not full path as now(D:\test\1).
You can use the basename task for getting directory name.

Resources