How to compile each jasper file to its own directory? - ant

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

Related

How can PMD HTML Report's Error Description Link Show Local Folder

I am using PMD source code analyzer (PMD) for my java web project through ant task. The computer is offline (not connected to the Internet). Part of ant task is as follows:
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
<classpath>
<fileset dir="E:/pmd-bin-6.41.0/lib">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
<pmd shortfilenames="true" cachelocation="pmd.cache" encoding="UTF-8">
<ruleset>web/resources/category/java/bestpractices.xml</ruleset>
<formatter type="html" tofile="report.html">
</formatter>
<fileset dir="src/java/">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
When I run pmd target, report.html file is generated ok. The html file basically lists <fileName, lineNumber, description> triplets.
e.g.
foo.java...43...The initializer for variable "tempIDNo" is never used (overwritten on lin 67)
The description in this html file has a link as file:///E:ws/project/${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unusedassignment which does not work. E:ws/project/ is the folder where my project resides.
As a matter of fact, I have all the necessary html files (such as pmd_rules_java_bestpractices.html) unzipped in E:/pmd-doc-6.41.0 folder.
Could you please help me how to set up description link in html file to show local folder?
Thank you.
Here is the solution I have come up with:
(Using suggestion from (How can I create a link to a local file on a locally-run web page?)) Before ant pmd target define property pmd.website.baseurl
<propery name="pmd.website.baseurl" value="file:///E:/pmd-doc-6.41.0"/>
(Using usage/suggestion from (https://ant.apache.org/manual/Types/filterchain.html#expandproperties), ANT replacing strings in specified files using file with properties ) Change inside target as follows
...
<pmd ...>...
</pmd>
<copy file="report.html" tofile="report2.html">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ExpandProperties"/>
</filterchain>
</copy> ...
Run the ant target.

Copy entire directory before creating an ear file

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>

Ant build script not seeing refid

I have a build.xml file that includes a common.xml file that defines some refid values. However, my task cannot see the refid value. I have not been able to find a solution on the web and am looking for some help.
I call the genbeans target in the build.xml file. It fails on the xmlbean taskdef with the message Reference my_classpath_jars not found.
build.xml
----------------------------
[includes common.xml]
**my_classpath_jars fails to be seen at this point - defined in common.xml**
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean">
<classpath refid="my_classpath_jars"/>
</taskdef>
<!-- Generate the XMLBeans java code from our source XSD file(s) -->
<target name="genbeans" description="Generate XML Bean files" depends="build_my_jar_cpath">
<mkdir dir="${lib}"/>
<xmlbean destfile="${lib}/${appname}Beans.jar" failonerror="true">
<classpath refid="my_classpath_jars"/>
<fileset dir="src/XSD Files" includes="*.xsd, *.wsdl"/>
</xmlbean>
</target>
common.xml
-----------------------------
<target name="build_my_jar_cpath">
<path id="my_classpath_jars">
<fileset dir="${jardir}" includes="**/*.jar" />
</path>
<pathconvert pathsep="${path.separator}" property="myjar.clpath" refid="my_classpath_jars"/>
</target>
When in doubt, use the ant -d switch when calling your target. You'll see a ton of output. Save it to a file and parse through it.
Do that, and the first thing you'll notice in the output is that it's defining your taskdefbefore you have defined your my_classpath_jars. That my_classpath_jars refid is only set when you call that greenbeans target. Your <taskdef> is executed before any of your targets are called.
Either take the definition of my_classpath_jars out of the target greenbeans, or put your <taskdef> in there.

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.

Ant Zip Extracted Parent Directory

I have several zip files that I need to unzip within an Ant target. All the zip files are in the same directory, and have the same internal directory and file structure.
So I am using the following snippet to unzip all the zip files in the directory, but each zip file does not contain a parent folder at the root, so each successive zip file is unzipped and overwrites the previous files.
<unzip dest="C:/Program Files/Samsung/Samsung TV Apps SDK/Apps">
<fileset dir=".">
<include name="**/*.zip"/>
</fileset>
</unzip>
Is there a better way to unzip a group of files, and create a directory to unzip them to that is based on the zip file name?
So, if the zip files are:
1.zip
2.zip
3.zip
then the content of each will be extracted to:
1/
2/
3/
Thanks
One solution might be to use the ant-contrib 'for' and 'propertyregex' tasks to do this:
<for param="my.zip">
<fileset dir="." includes="**/*.zip" />
<sequential>
<propertyregex property="my.zip.dir"
input="#{my.zip}"
regexp="(.*)\..*"
select="\1"
override="yes" />
<unzip src="#{my.zip}" dest="${my.zip.dir}" />
</sequential>
</for>
The 'propertyregex' strips the .zip extension from the zip file name to use as the target directory name.
Without ant-contrib:
https://stackoverflow.com/a/12169523/957081
<!-- Get the path of the war file. I know the file name pattern in this case -->
<path id="warFilePath">
<fileset dir="./tomcat/webapps/">
<include name="myApp-*.war"/>
</fileset>
</path>
<property name="warFile" refid="warFilePath" />
<!-- Get file name without extension -->
<basename property="warFilename" file="${warFile}" suffix=".war" />
<!-- Create directory with the same name as the war file name -->
<mkdir dir="./tomcat/webapps/${warFilename}" />
<!-- unzip war file -->
<unwar dest="./tomcat/webapps/${warFilename}">
<fileset dir="./tomcat/webapps/">
<include name="${warFilename}.war"/>
</fileset>
</unwar>

Resources