Apache Ant Interview Questions

«»

Apache Ant Interview Questions – 2

1)Where can I find the javadoc for ant API?

Download apache ant src version. Use ant javadocs command to see generated javadoc for ant in build/docs directory.

2)How can I use ant to run a Java application?

Here is a real world example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<target name="run" depends="some.target,some.other.target">
 
    <java classname="${run.class}" fork="yes">
      <classpath>
        <path refid="classpath" />
      </classpath>
 
      <jvmarg line="${debug.jvmargs}" />
      <jvmarg line="${my.jvmargs}" />
      <jvmarg value="-Dname=${name}" />
      <jvmarg line="${run.jvmargs}" />
      <arg line="${run.args}" />
    </java>
  </target>

3)How to use ant to run commandline command? How to get perl script running
result?

Use exec ant task.

Don’t forget ant is pure Java. That is why ant is so useful, powerful and versatile. If you want ant receive unix command and result, you must think Unix. So does in MS-Windows. Ant just helps you to automate the process.

4)How do I debug my ant script?

Many ways

  • Do an echo on where you have doubt. You will find out what is the problem easily. Just like the old c printf() or Java System.println()
  • Use project.log(“msg”) in your javascript or custom ant task
  • Run Ant with -verbose, or even -debug, to get more information on what it is doing, and where. However, you might be tired with
    that pretty soon, since it give you too much information.

5)How to exclude multi directories in copy or delete task?

Here is an example.

1
2
3
4
5
6
7
8
<copy todir="${to.dir}" >
    <fileset dir="${from.dir}" >
      <exclude name="dirname1" />
      <exclude name="dirname2" />
      <exclude name="abc/whatever/dirname3" />
      <exclude name="**/dirname4" />
    </fileset>
  </copy>

6)How to use Runtime in ant?

You don’t need to use Runtime etc. Ant have exec task.

The class name is org.apache.tools.ant.taskdefs.ExecTask. You can create the task by using the code in your customized ant Task.

ExecTask compile = (ExecTask)project.createTask(“exec”);

7)How to rearrange my directory structure in my jar/war/ear/zip file? Do I
need to unarchive them first?

No, you don’t need to unarchive them first.

  • You don’t need to unzip the files from archive to put into your destination jar/ear/war files.
  • You can use zipfileset in your jar/war/ear task to extract files from old archive to different directory in your new archive.
  • You also can use zipfileset in your jar/war/ear task to send files from local directory to different directory in your new archive.

See the follow example:

1
2
3
4
  <jar destfile="${dest}/my.jar">
    <zipfileset src="old_archive.zip" includes="**/*.properties" prefix="dir_in_new_archive/prop"/>
    <zipfileset dir="curr_dir/abc" prefix="new_dir_in_archive/xyz"/>
  </jar>

8)Why did I get such warning in ant?

compile:

[javac] Warning: commons-logging.properties modified in the future.

[javac] Warning: dao\\DAO.java modified in the future.

[javac] Warning: dao\\DBDao2.java modified in the future.

[javac] Warning: dao\\HibernateBase.java modified in the future.

System time problem, possible reasons:

  • You changed the system time
  • I had the same problem before, I checked out files from cvs to windows, and transfer them to a unix machine, somehow, I got huge amount of such warnings because the system timing issue.
  • If you transfer files from Australia/China/india to the United States, you will get the problem too. True enough, I did and met the
    problem once.

9)How can I write my own ant task?

Easy!

Writing Your Own Task How-To from ant.

In your own $ANT_HOME/docs/manual directory, there also is tutorial-writing-tasks-src.zip

Use them! Use taskdef to define it in your script, define it before using it.

10)How to copy files without extention?

If files are in the directory:

1
<include name="a,b,c"/>

If files are in the directory or subdirectories:

1
<include name="**/a,**/b,**/c"/>

If you want all files without extension are in the directory or subdirectories:

1
<exclude name="**/*.*"/>
email

«»

Comments

comments

,