Why does not ant delete task work? - java

I have this ant script:
<sequential>
<echo message="Deleting needed folder" />
<property name="bigPathToFolder" value="${basePath}/pathToFolder" />
<delete dir="bigPathToFolder" quiet="false"/>
<echo message="Delete success" />
</sequential>
In console I see Deleting needed folder and after Delete success, but the content of the folder and folder don't change.
What did I do wrong?

replace
<delete dir="bigPathToFolder" quiet="false"/>
to
<delete dir="${bigPathToFolder}" quiet="false"/>

You can use if task available from ant-contrib to do file existance check at path you are looking & run a delete like below:
<if>
<available file="${bigPathToFolder}"/>
<then>
<delete dir="${bigPathToFolder}" quiet="false"/>
<echo message="Delete success" />
</then>
<else>
<echo message="File Not found at path : ${bigPathToFolder}" />
</else>
</if>

Related

Print Java's Package Hierarchy with Ant

I want to list all my java packages with ant's "echo". I mean, for example, I have this package hierarchy:
Folder1
Package1
Package2
Folder2
Package1
Package2
The ant file must show something like this with echo's task:
Folder1.Package1
Folder1.Package2
Folder2.Package1
Folder2.Package2
Any ideas?
Here's an example target showing how this is possible. Note that you might need to change the dirs value of cutdirsmapper depending on the location of your files.
<target name="default">
<delete dir="Folder1" />
<delete dir="Folder2" />
<mkdir dir="Folder1" />
<touch file="Folder1/Package1" />
<touch file="Folder1/Package2" />
<mkdir dir="Folder2" />
<touch file="Folder2/Package1" />
<touch file="Folder2/Package2" />
<pathconvert property="packages" pathsep="${line.separator}" dirsep=".">
<fileset dir="." includes="Folder1/**/*,Folder2/**/*" id="packages" />
<cutdirsmapper dirs="4" />
</pathconvert>
<echo message="${packages}" />
</target>

Ant - javac task - classpath is not taking the jars

I have my ant build.xml file like this:
<!-- Properties -->
<property name="src" value="${basedir}" />
<property name="jars" value="${src}/jars" />
<property name="dest" value="${src}/dest" />
<property name="reports" value="${src}/reports" />
<path id="claspath">
<fileset dir="${jars}">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<echo> removing the directories "dest" and "reports" </echo>
<delete dir="${dest}" />
<delete dir="${reports}" />
</target>
<target name="makedir" depends="clean">
<echo> creating directories "dest" and "reports" </echo>
<mkdir dir="dest" />
<mkdir dir="reports" />
</target>
<target name="complie" depends="makedir">
<javac srcdir="${src}" destdir="${dest}" />
<classpath refid="classpath"/>
</target>
When I type ant command in cmd prompt the compiling happens but the jar files are not loading so I am getting compiling errors. The jar folder that i have mentioned in the above code has only one jar file which is "testng-6.8.5.jar". Please let me know what is wrong in the above code.
You're closing the <javac> tag on the same line, it should be:
<javac srcdir="${src}" destdir="${dest}">
<classpath refid="classpath"/>
</javac>

Using Ant condition tag

I want to do something like this
<target name="init" description="Create build directories.
>
<mkdir dir="bin" />
<mkdir dir="dist" />
<!-- Check for the presence of dependent JARs -->
<condition property="dependent.files.available">
<and>
<available file="../../xx.jar" />
<available file="../../yy.jar" />
</and>
</condition>
</target>
<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources
and create classes">
<if>
<isset property="dependent.files.available"/>
<then>
<javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
</then>
<else>
<echo message="Either xx.jar or yy.jar not found"/>
</else>
</if>
</target>
When I tried to compile the code it gave me the following error
Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place
It it the correct way of doing it?
You need to have the ant-contrib jar visible at runtime, or configure it correctly as described in the link.
The thing boils down to have Ant load the task definition, so if you put ant-contrib in ant/lib, you just need
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
Just a suggestion. You can do the same thing with ant 1.9.1 onwards using if/unless without ant-contrib (http://ant.apache.org/manual/targets.html)
Your targets would be
<target name="init" description="Create build directories.
>
<mkdir dir="bin" />
<mkdir dir="dist" />
<!-- Check for the presence of dependent JARs -->
<condition property="dependent.files.available">
<and>
<available file="../../xx.jar" />
<available file="../../yy.jar" />
</and>
</condition>
</target>
<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources
and create classes" if="dependent.files.available">
<javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
</target>

How can I check a signed jar file using Ant?

I sign jar files with the Ant signjar task and now I want to test before deploy.
I can check with
jarsigner -verify sbundle.jar
but I do not know if it is possible to do the same with Ant?
An alternative would be to base your build on a maven script.
Maven does propose the jarsigner:verify plugin
If that is not a valid possibility, you still can use the Exec Ant task to directly call the jarsigner command.
If the return code is correctly set, you can add the attribute failonerror (Stop the build process if the command exits with a return code other than 0.)
The following Ant code can be used for verifying JAR-file signatures. The script will fail as soon as it encounters a JAR-file where the signature is not valid or where it is missing.
Note that ant-contrib is required for the for task.
<!-- Macro to verify whether or not a JAR file is signed -->
<macrodef name="verify-signatures">
<attribute name="filesetref" />
<sequential>
<for param="file">
<path>
<fileset refid="#{filesetref}" />
</path>
<sequential>
<echo message="Verifying signature on file: #{file}" />
<exec executable="jarsigner" failonerror="true">
<arg value="-verify" />
<arg value="#{file}" />
</exec>
<fail message="#{file} must be signed">
<condition>
<not>
<issigned file="#{file}" />
</not>
</condition>
</fail>
</sequential>
</for>
</sequential>
</macrodef>
<!-- Define the list of files to check -->
<fileset dir="p2repo" id="jarfiles">
<include name="**/*.jar" />
</fileset>
<!-- Verify signatures -->
<verify-signatures filesetref="jarfiles" />
Ant conditions offer "issigned".
"Test whether a jarfile is signed. If the name of the signature is passed, the file is checked for presence of that particular signature; otherwise the file is checked for the existence of any signature. It does not perform rigorous signature validation; it only looks for the presence of a signature.
This condition was added in Apache Ant 1.7."
From Ant conditions
You can use the VerifyJar Task in Ant to do this. Here is the link to Ant help
https://ant.apache.org/manual/Tasks/verifyjar.html
Sample code for Verifying multiple JAR files at once.
<verifyjar keystore="mykeystore" keypass="abc"
storepass="abc" alias="myalias">
<path>
<fileset dir="${build.dir}/signedjar" includes="**/*.jar" />
</path>
</verifyjar>
Based on #torkildr's answer.
It's possible to make macro pass nested path or fileset to ant-contrib for task.
<target name="verify-artifacts" description="Just an example of usage">
<verify-artifacts>
<fileset dir="${project.ear.dir}" includes="*.*ar"/>
</verify-artifacts>
</target>
<macrodef name="verify-artifacts">
<element name="artifact-path" implicit="true"/>
<sequential>
<for param="file">
<artifact-path/>
<sequential>
<verify-artifact file="#{file}"/>
</sequential>
</for>
</sequential>
</macrodef>
<macrodef name="verify-artifact">
<attribute name="file"/>
<attribute name="alias" default="${artifact.sign.keystore.alias}"/>
<attribute name="keystore" default="${artifact.sign.keystore.path}"/>
<attribute name="password" default="${artifact.sign.keystore.password}"/>
<sequential>
<if>
<istrue value="${artifact.sign.enabled}"/>
<then>
<echo message="Trying to verify #{file} with alias #{alias} from #{keystore}"/>
<required-macro-param value="#{alias}" prop="artifact.sign.keystore.alias"/>
<required-macro-param value="#{keystore}" prop="artifact.sign.keystore.path"/>
<required-macro-param value="#{password}" prop="artifact.sign.keystore.password"/>
<fail message="Keystore path '#{keystore}' not found">
<condition>
<not><available file="#{keystore}" type="file"/></not>
</condition>
</fail>
<fail message="Artifact '#{file}' not found">
<condition>
<not><available file="#{file}" type="file"/></not>
</condition>
</fail>
<!-- jarsigner -verify -keystore #{keystore} -storepass #{password} #{file} #{alias} -->
<exec executable="jarsigner" failonerror="true">
<arg value="-verify"/>
<arg value="-keystore"/>
<arg value="#{keystore}"/>
<arg value="-storepass"/>
<arg value="#{password}"/>
<arg value="#{file}"/>
<arg value="#{alias}"/>
</exec>
</then>
</if>
</sequential>
</macrodef>
<macrodef name="required-macro-param">
<attribute name="prop"/>
<attribute name="value"/>
<sequential>
<!--<echo message="#{value}"/>-->
<fail message="You must set property '#{prop}'">
<condition>
<and>
<or>
<equals arg1="#{value}" arg2=""/>
<matches string="#{value}" pattern="^\$\{.*?\}$"/>
</or>
<!--<not><isset property="#{prop}"/></not>-->
</and>
</condition>
</fail>
</sequential>
</macrodef>
<macrodef name="sign-artifact">
<attribute name="file"/>
<attribute name="alias" default="${artifact.sign.keystore.alias}"/>
<attribute name="keystore" default="${artifact.sign.keystore.path}"/>
<attribute name="password" default="${artifact.sign.keystore.password}"/>
<sequential>
<if>
<istrue value="${artifact.sign.enabled}"/>
<then>
<echo message="Trying to sign #{file} with alias #{alias} from #{keystore}"/>
<required-macro-param value="#{alias}" prop="artifact.sign.keystore.alias"/>
<required-macro-param value="#{keystore}" prop="artifact.sign.keystore.path"/>
<required-macro-param value="#{password}" prop="artifact.sign.keystore.password"/>
<fail message="Keystore path '#{keystore}' not found">
<condition>
<not><available file="#{keystore}" type="file"/></not>
</condition>
</fail>
<fail message="Artifact '#{file}' not found">
<condition>
<not><available file="#{file}" type="file"/></not>
</condition>
</fail>
<signjar jar="#{file}" alias="#{alias}" keystore="#{keystore}" storepass="#{password}"/>
<fail message="Signature check failed">
<condition>
<not><issigned file="#{file}" name="#{alias}"/></not>
</condition>
</fail>
</then>
</if>
</sequential>
</macrodef>
<macrodef name="sign-artifacts">
<element name="artifact-path" implicit="true"/>
<sequential>
<for param="file">
<artifact-path/>
<sequential>
<sign-artifact file="#{file}"/>
</sequential>
</for>
</sequential>
</macrodef>
<property name="artifact.sign.enabled" value="true"/>
<property name="artifact.sign.keystore.alias" value="alias"/>
<property name="artifact.sign.keystore.path" value="keystore.jks"/>
<property name="artifact.sign.keystore.password" value="pwd"/>

Echo target description in Ant

<target name="compile" description="Compile the File">
<echo>Compile the File </echo>
<mkdir dir="${compilation-dir}" />
<javac srcdir="." classpath="another2" destdir="${compilation-dir}" />
</target>
I want to echo the description of the target. Is there a better way of doing this other than duplicating it?
I guess this is not a perfect solution, but at least you avoid duplicate descriptions.
<property name="testing.desc" value="this is the desc" />
<target name="testing" description="${testing.desc}">
<echo message="${testing.desc}" />
</target>

Categories