Ant - Using external file to use in patternset - maven

In Ant, we use patternset to include or exclude some set of file using a pattern such as
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
Is Ant capable of taking this patternset from an external file say txt or xml?
Seeing around the Ant the wiki does not mention of such usuage, but i am thinking otherwise.

Consider using includesfile/excludesfile or includes/excludes attributes of patternset.
In case of includes/excludes, you can use values of properties stored in your normal property file.

Related

Return a custom data type from Ant to Maven

I have an Ant script called from Maven to perform some tasks. One such task is to read a text file linewise and use its content to perform text replacements on a huge set of files. The text file will be in the following format on each line:
name#URL#Title
Ant task for this is as follows:
<target name="update-urls" depends="load-ant-contrib">
<loadfile property="file" srcfile="${mapping.file}"/>
<foreach param="file.entry" list="${file}" delimiter="${line.separator}" target="update-template"></foreach>
</target>
<!--Get the name,url and title of each line-->
<target name="update-template">
<propertyregex property="name"
input="${file.entry}"
regexp="(.*)#(.*)#(.*)$"
select="\1"/>
<propertyregex property="url"
input="${file.entry}"
regexp="(.*)#(.*)#(.*)$"
select="\2"/>
<propertyregex property="title"
input="${file.entry}"
regexp="(.*)#(.*)#(.*)$"
select="\3"/>
<echo>${template.name}</echo>
<!--Use title and url to match and replace the URL with the URL in text file-->
<replaceregexp byline="true"
match="(<title>${title} *</title>.*)${url} *"
replace="\1../modified-path/${name}.zip"
flags="g"
encoding="utf-8"
>
<!--Following are the files on which replacement has to happen-->
<fileset dir="${huge.set.of.files}/items">
<include name="**/info.xml"/>
</fileset>
</replaceregexp>
</target>
Since this needs to work on huge set of files, the above solution is not very efficient. I have coded a mutithreaded solution for it in Java which takes up multiple regex expressions(name, value pairs) at once.I've integrated it into my maven project as a mojo.
I'm having trouble with two things:
Framing a data-type to contain a list of name, url, title triplets in Ant.
If I'm successful with step 1., how to return this data structure to POM from where the Ant task got called and decode this data structure to be able to access each element's name, url and title separately..
Can someone guide me on how to go about this..

Is there a way to disable validation when using Saxon in the XSLT Ant task?

I am running some XSL transforms via Ant's XSLT task. I am using Saxon 9HE as the processing engine. I have a situation where the input XML files all use the same DTD but declare it to be in different places. Some declare it to be in the current directory, some in a folder and others reference a URL. Here is the Ant script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="PubXML2EHeader" default="transform">
<property name="data.dir.input" value="./InputXML"/>
<property name="data.dir.output" value="./converted-xml"/>
<property name="xslt.processor.location" value="D:\\saxon9he.jar"/>
<property name="xslt.processor.factory" value="net.sf.saxon.TransformerFactoryImpl"/>
<path id="saxon9.classpath" location="${xslt.processor.location}"/>
<target name="clean">
<delete dir="${data.dir.output}" includes="*.xml" failonerror="no"/>
</target>
<target name="transform" depends="clean">
<xslt destdir="${data.dir.output}"
extension=".xml"
failOnTransformationError="false"
processor="trax"
style="Transform.xsl"
useImplicitFileset="false"
classpathref="saxon9.classpath"
>
<outputproperty name="method" value="xml"/>
<outputproperty name="indent" value="yes"/>
<fileset dir="${data.dir.input}" includes="**/*.xml" excludes="Transform.xml"/>
<factory name="${xslt.processor.factory}"/>
</xslt>
</target>
</project>
When I run this Ant script I get errors like this:
[xslt] : Fatal Error! I/O error reported by XML parser processing
file:/D:/annurev.biophys.093008.131228.xml:
http://www.atypon.com/DTD/nlm-dtd/archivearticle.dtd Cause:
java.io.FileNotFoundException:
http://www.atypon.com/DTD/nlm-dtd/archivearticle.dtd
I think these are caused by the fact that Saxon cannot get to the DTD (which is a actually a firewall issue in this case). I don't think I care about validating the input, which is what I think is happening here, and I would like to skip it. Is there an attribute I can add to the XSLT Ant task to stop Saxon from trying to read in the DTD?
You are confusing "reading the DTD" with validating. An XSLT processor will always ask the parser to read the external DTD of a document whether it is validating or not. This is because a DTD is used for more than validation; it is also used for expansion of entity references.
The usual way to deal with this problem is to redirect the DTD reference to a copy that is somewhere it can be accessed, generally by use of catalogs. This involves setting an EntityResolver on the underlying XML parser.
There's lots of information on the web about how to set up a catalog resolver with Saxon, usually from the command line: see for example here: http://www.sagehill.net/docbookxsl/UseCatalog.html
The advice is generally to set the -x, -y, and -r options, but in fact only -x is relevant if you only need to redirect DTD references in source documents (-y affects stylesheets, -r affects the document() function). In Ant, the equivalent to setting the -x option is to use the attribute child of the factory element to set the configuration property<attribute name="http://saxon.sf.net/feature/sourceParserClass" value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>.
That still leaves the part I find tricky, which is actually creating your catalog file.

Generate Checksum for directories using Ant build command

I tried to generate the checksum for directory using ant.
I have tried the below command, but it generates recursively inside each folder for each file.
<target name="default" depends="">
<echo message="Generating Checksum for each environment" />
<checksum todir="${target.output.dir}" format="MD5SUM" >
<fileset dir="${target.output.dir}" />
</checksum>
</target>
I just want to generate one checksum for particular directory using Ant command.
How do I do that?
You want to use the totalproperty attribute. As per the documentation this property will hold a checksum of all the checksums and file paths.
e.g.
<target name="hash">
<checksum todir="x" format="MD5SUM" totalproperty="sum.of.all">
<fileset dir="x"/>
</checksum>
<echo>${sum.of.all}</echo>
</target>
Some other general notes.
This is not idempotent. Each time you run it you will get a new value because it includes the previous hash file in the new hash (and then writes a new hash file). I suggest that you change the todir attribute to point elsewhere
It's a good idea to name your targets meaningfully. See this great article by Martin Fowler for some naming ideas
You don't need the depends attribute if there's no dependency.

ANT and Netbeans GUI project as executable JAR could not find Main class

I receive could not ind Main class error message when trying to run jar file.
I am using Netbeans 6.9.1 with GUI framework and part of my ANT file is:
<target name="makeJar" depends="compile">
<delete file="${build.home}/izvrsniProgram.jar"/>
<jar update="true" destfile="${build.home}/izvrsniProgram.jar" basedir="${build.classes}">
<zipfileset dir="${lib}" includes="**/*.jar"/>
<manifest>
<attribute name="Main-Class" value="oat.DesktopApplication2 "/>
<attribute name="stos" value="jeste"/>
</manifest>
</jar>
</target>
<target name="runJAR">
<java jar="${build.home}/izvrsniProgram.jar"/>
</target>
Manifest file is in JAR, ANT_HOME is already set, "lib" contains GUI jar framework.
Please help me solve this issue... because I think of it day and night and could not find what did I do wrong.
The thing that catches my eye is that you are calling the 'zipfileset' task inside of the 'jar' task. If you are trying to compress the jar file, you can achieve that by using the "compress" and "level" attributes on the 'jar' task.
From the manual page for the 'jar' task on the Ant site...
compress - Not only store data but
also compress them, defaults to true.
Unless you set the keepcompression
attribute to false, this will apply to
the entire archive, not only the files
you've added while updating.
level - Non-default level at which file
compression should be performed. Valid
values range from 0 (no
compression/fastest) to 9 (maximum
compression/slowest). Since Ant 1.7
or if you are just trying to add your dependent jars as one zip archive (why?), you could try to zip the jars into your build directory first, then include the zipped file with a 'fileset'.

How can I change a config file variable using ant build?

I'm using Jboss 4/5 and have some .war .properties files with default configuration settings
I want to update these settings using information from the windows xp environment variables.
${env} in ant
Import enviroment variables before including your property file:
build.xml file:
<target name="build">
<!-- load enviroment properties -->
<property environment="env" />
<property file="test.properties" />
<echo>test: ${test}</echo>
</target>
test.properties file:
test = ${env.TEMP}
I needed to up the build number in several files. Since I needed to keep the formatting of the file as well as the comments, I used replaceregexp. Be careful when writing your regular expressions that you limit the expression to only find the instances that you care about.
<replaceregexp
file="${dir.project}/build.properties"
match="(build\.number[ \t]*=).*"
replace="\1${build.number}"
flags="g"
/>

Resources