Ant task: add a file to an archive with a parameter - ant

everyone!
I'm currently struggling with an Ant task for a DITA plugin. It would consist in adding a file (whatever the extension is) to an archive via the command line. The idea is to set a parameter like this :
dita -f mymap.ditamap -transtype htmlCustom -Dadd-file=fileOfMyChoice.txt
The part that interests me is the last one: -Dadd-file=fileOfMyChoice.txt
So far, I've tried to include two choices into my target.
The user sets a add-file parameter and its value is added into the fileset with the base set of generated files.
Or, nothing is passed via this parameter and the base set of generated files is included in the fileset.
The part that is still challenging to me if the one that identifies if a parameter add-file is passed and how its value is transmitted in a fileset.
If you have any pointers for me, that would be awesome.
<target name="map2exportmap">
<dirname property="dita.temp.dir.fullpath" file="${dita.temp.dir}${file.separator}dummy.file"/>
<xslt in="${dita.temp.dir.fullpath}${file.separator}.job.xml"
out="${dita.temp.dir.fullpath}${file.separator}allResources.out"
style="${dita.plugin.com.myplugin.dir}/xsl/generateAllResources.xsl"
force="true"/>
<mkdir dir="${dita.map.output.dir}/temp"/>
<fileset id="global" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out" />
<union id="globalAddendum">
<fileset id="ditaGeneration" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out"/>
<fileset id="additionalFile" dir="${user.input.dir}/" includesfile="${add-File}"/>
</union>
<condition property="withAddedFile" refid="globalAddendum" else="global">
<isset property="add-file"/>
</condition>
<copy todir="${dita.map.output.dir}/temp">
<fileset dir="${user.input.dir}/" includesfile="${withAddedFile}"/>
</copy>
<zip destfile="${dita.map.output.dir}/${dita.map.filename.root}.zip">
<fileset dir="${dita.map.output.dir}/temp"/>
</zip>
<delete dir="${dita.map.output.dir}/temp"/>
</target>

everyone!
I found the solution!
Here is the code if it can help anyone.
<target name="map2exportmap">
<dirname property="dita.temp.dir.fullpath" file="${dita.temp.dir}${file.separator}dummy.file"/>
<xslt in="${dita.temp.dir.fullpath}${file.separator}.job.xml"
out="${dita.temp.dir.fullpath}${file.separator}allResources.out"
style="${dita.plugin.com.xxxxx.xxxxxxx.dir}/xsl/generateAllResources.xsl"
force="true"/>
<mkdir dir="${dita.map.output.dir}/temp"/>
<!-- Create two filesets depending on the argument `add-file` sent in the command line -->
<fileset id="base" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out" />
<fileset id="addedFile" file="${add-file}"/>
<!-- this is the core of the condition. It tests if there was an argument for `add-file, then ii sets the property `with.or.without.added.file`-->
<condition property="with.or.without.added.file" value="addedFile" else="base">
<isset property="add-file"/>
</condition>
<echo>${with.or.without.added.file}</echo>
<echo>${add-file}</echo>
<!-- Process the copy of content in the tempoerary folder-->
<copy todir="${dita.map.output.dir}/temp">
<fileset refid="${with.or.without.added.file}"/>
<fileset id="base" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out" />
</copy>
<!-- Execute the zip packaging -->
<zip destfile="${dita.map.output.dir}/${dita.map.filename.root}.zip">
<fileset dir="${dita.map.output.dir}/temp"/>
</zip>
<delete dir="${dita.map.output.dir}/temp"/>

Related

Read the each file name and check for the string using ant

I have particular requirement. I have multiple reports file in particlar directly. Currently I build up ant script reading all files and checking for the particular string using the below code.
<target name="GenerateReports" >
<property name="search.string" value="Internal Error" />
<fileset id="existing" dir="${report.dir}">
<patternset id="files">
<include name="*.txt" />
</patternset>
</fileset>
<fileset id="matches" dir="${report.dir}">
<patternset refid="files" />
<contains text="${search.string}" />
</fileset>
<fail message="Found '${search.string}' in one or more test cases results in '${report.dir}' One or more test cases are failed">
<condition>
<resourcecount when="greater" count="0" refid="matches" />
</condition>
</fail>
</target>
But i want to read each file and give the name of the file where the error exists in my report file.
How to read the each file name and read the content also.
<foreach target="-List-File-Names" param="foreach.file" inheritall="true">
<path>
<fileset dir="${report.dir}" includes="*.txt">
<contains text="${search.string}" />
</fileset>
</path>
</foreach>
<target name="-List-File-Names">
<dirname property="file.dir" file="${foreach.file}"/>
<!-- dirname now holds the file -->
</target>

Ant: Add files to zip only if path is defined

I have to create an ant target that will add component .jar to a zip. The problem is that I do not have the version of component always defined. For example:
<zip destfile="${dist.dir}\result-${result-version}.zip">
<fileset dir="${dist.dir}" includes="maincomponent-${maincomponenet-version}*.jar"/>
<fileset dir="${second-componenet-folder}" includes="${second-component-version}.jar" />
</zip>
How can I add some condition that will create the zip with the main component if the second-component-version property is not defined?
Using Condition and isset (conditions):
<target name="zip">
<fileset id="main" dir="${dist.dir}" includes="maincomponent-${maincomponenet-version}.jar" />
<fileset id="component" dir="${second-componenet-folder}" includes="${second-component-version}.jar" />
<condition property="jar.file" value="component" else="main">
<isset property="second-component-version"/>
</condition>
<zip destfile="${dist.dir}/result-${result-version}.zip">
<fileset refid="${jar.file}" />
</zip>
</target>

ant build.xml target to check for debug code

When debugging it's quite common for me to use things such as Zend_Debug and die() in the PHP to locate an issue. Occasionally I forget to take these out before committing my code. So I was wondering...
How do I write an ant build.xml target which checks all the files in my application for specific strings and fails if they have been found?
Basically, I'm after a reverse grep command which fails when it finds a string.
Any ideas?
Also, given my build.xml file looks like this (I've removed most of my targets to make it short), how do I make it work?
I don't know how ant works, so I'm after a 'drop-in' solution or good instructions.
<?xml version="1.0" encoding="UTF-8"?>
<project name="API" default="build" basedir=".">
<property name="source" value="application"/>
<target name="build" depends="prepare,lint,phpcpd,phpdox,phpunit,phpcb"/>
<target name="clean" description="Cleanup build artifacts">
<delete dir="${basedir}/build/api"/>
</target>
<target name="lint">
<apply executable="php" failonerror="true">
<arg value="-l" />
<fileset dir="${basedir}/${source}">
<include name="**/*.php" />
</fileset>
<fileset dir="${basedir}/tests">
<include name="**/*.php" />
</fileset>
</apply>
</target>
</project>
Within the lint target (after the apply element) add
<fileset id="die-files" dir="${basedir}/${source}">
<include name="**/*.php" />
<contains text="die()"/>
</fileset>
<fail message="The following files contain "die()": ${ant.refid:die-files}">
<condition>
<resourcecount when="greater" count="0" refid="die-files"/>
</condition>
</fail>
If you can use ant-contrib than:
<for param="file">
<path>
<fileset dir="/path/to/application/"/>
</path>
<sequential>
<if>
<contains string="#{file}" substring="bad elements"/>
<then>
<fail>warning! substring is present in directory</fail>
</then>
</if>
</sequential>
</for>

Ant script: Prevent duplication of JAR in javac-classpath war-lib

I have a ANT script and I have a lot of duplicated path to same set JAR files.
But there is so many double wording in the classpath and also in the war element.
<path id="my.classpath">
<pathelement location="folderA/subFolderA/1.0/A.jar"/>
<pathelement location="folderC/subFolderB/1.0/B.jar"/>
<pathelement location="folderF/subFolderZ/2.0/Z.jar"/>
<pathelement location="compile/subFolderX/1.0/onlyForJavac.jar"/>
</path>
....
<javac ...>
<classpath refid="my.classpath" />
</javac>
....
<war ...>
<lib file="folderA/subFolderA/1.0/A.jar"/>
<lib file="folderC/subFolderB/1.0/B.jar"/>
<lib file="folderF/subFolderZ/2.0/Z.jar"/>
<lib file="moreFolderF/subFolderZ/2.0/additionFile.jar"/>
<lib file="moreFolderF/subFolderZ/2.0/additionRuntimeFile.jar"/>
</war>
I want to summary them into ONE list which is easier to keep update.
But I am blocked as I have no idea how to share a path-like-structure with a fileset-like-structure.
Since Ant 1.8.0 there is a new resource collection - mappedresources that
can be used in place of the war task lib element.
So, the task might look like this (pretty much straight from the docs):
<war ... >
<mappedresources>
<restrict>
<path refid="my.classpath"/>
<type type="file"/>
</restrict>
<chainedmapper>
<flattenmapper/>
<globmapper from="*" to="WEB-INF/lib/*"/>
</chainedmapper>
</mappedresources>
</war>
This feature was added to resolve a long-standing feature request to make
the task flatten jars when deploying to WEB-INF/lib.
previous answer:
Although you can't easily convert a path to a fileset with vanilla Ant, you can go the other way.
So one option would be to define your jars in a fileset, and derive the path from it.
Something like this perhaps:
<fileset id="my.fileset" dir="${basedir}">
<include name="folderA/subFolderA/1.0/A.jar"/>
<include name="folderC/subFolderB/1.0/B.jar"/>
<include name="folderF/subFolderZ/2.0/Z.jar"/>
<include name="moreFolderF/subFolderZ/2.0/additionFile.jar"/>
<include name="moreFolderF/subFolderZ/2.0/additionRuntimeFile.jar"/>
</fileset>
<path id="my.classpath">
<fileset refid="my.fileset" />
</path>
<!-- javac stays the same -->
<war ...>
<lib refid="my.fileset" />
</war>
Another possibility is to use the ant-contrib pathtofileset task.
Another solution, possibly 'not the best' will be to place required jar file in WEB-INF/lib, and then set the classpath from there.
<path id="compile.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
When its time to build the war, you need not worry about the <lib> at all, as jars are already placed in WEB-INF/lib folder.
<war destfile="${dist.dir}/${project.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}"/>
<classes dir="${build.dir}/classes"/>
</war>

How do I "expand" an ant path (accessed with refId=..) to all files in the path except some?

I am trying to get ant4eclipse to work and I have used ant a bit, but not much above a simple scripting language. We have multiple source folders in our Eclipse projects so the example in the ant4eclipse documentation needs adapting:
Currently I have the following:
<target name="build">
<!-- resolve the eclipse output location -->
<getOutputpath property="classes.dir" workspace="${workspace}" projectName="${project.name}" />
<!-- init output location -->
<delete dir="${classes.dir}" />
<mkdir dir="${classes.dir}" />
<!-- resolve the eclipse source location -->
<getSourcepath pathId="source.path" project="." allowMultipleFolders='true'/>
<!-- read the eclipse classpath -->
<getEclipseClasspath pathId="build.classpath"
workspace="${workspace}" projectName="${project.name}" />
<!-- compile -->
<javac destdir="${classes.dir}" classpathref="build.classpath" verbose="false" encoding="iso-8859-1">
<src refid="source.path" />
</javac>
<!-- copy resources from src to bin -->
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset refid="source.path">
<include name="**/*"/>
<!--
patternset refid="not.java.files"/>
-->
</fileset>
</copy>
</target>
The task runs successfully, but I cannot get the to work - it is supposed to copy all non-java files over too to emulate the behaviour of eclipse.
So, I have a pathId named source.path which contains multiple directories, which I somehow needs to massage into something the copy-task like. I have tried nesting which is not valid, and some other wild guesses.
How can I do this - thanks in advance.
You might consider using pathconvert to build a pattern that fileset includes can work with.
<pathconvert pathsep="/**/*," refid="source.path" property="my_fileset_pattern">
<filtermapper>
<replacestring from="${basedir}/" to="" />
</filtermapper>
</pathconvert>
That will populate ${my_fileset_pattern} with a string like:
1/**/*,2/**/*,3
if source.path consisted of the three directories 1, 2, and 3 under the basedir. We're using the pathsep to insert wildcards that will expand to the full set of files later.
The property can now be used to generate a fileset of all the files. Note that an extra trailing /**/* is needed to expand out the last directory in the set. Exclusion can be applied at this point.
<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*">
<exclude name="**/*.java" />
</fileset>
The copy of all the non-java files then becomes:
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset refid="my_fileset" />
</copy>
That will copy the source files over retaining the source directory structure under todir. If needed, the flatten attribute of the copy task can be set to instead make all the source files copy directly to todir.
Note that the pathconvert example here is for a unix fileseystem, rather than windows. If something portable is needed, then the file.separator property should be used to build up the pattern:
<property name="wildcard" value="${file.separator}**${file.separator}*" />
<pathconvert pathsep="${wildcard}," refid="source.path" property="my_fileset">
...
You could use the foreach task from the ant-contrib library:
<target name="build">
...
<!-- copy resources from src to bin -->
<foreach target="copy.resources" param="resource.dir">
<path refid="source.path"/>
</foreach>
</target>
<target name="copy.resources">
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset dir="${resource.dir}" exclude="**/*.java">
</copy>
</target>
If your source.path contains file paths as well then you could the if task (also from ant-contrib) to prevent attempting to copy files for a file path, e.g.
<target name="copy.resources">
<if>
<available file="${classes.dir}" type="dir"/>
<then>
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset dir="${resource.dir}" exclude="**/*.java">
</copy>
</then>
</if>
</target>

Resources