Showing posts with label Ant. Show all posts
Showing posts with label Ant. Show all posts

Wednesday, June 5, 2013

Difference between antcall and depends

Sometime we work without thinking much about the build script and other times we need to change the build scripts. At this time we start thinking about various functions in the language.

So today when I was modifying such build script, then I got stuck between the use of "depends" and "antcall". I did little bit of Googling and I got 2 nice articles.

It is not possible for me to elaborate all the uses of ‘depends’ and ‘antcall’ but I will try to provide little information on these targets.

Depends is an attribute of “target” target.

<target name=”runtarget” depends=”target1, target2, target3”  if="condition">

About specified line says before running “runtarget”, ant must execute target mentioned in the “depends” attributes. If condition is verified once all the targets mentioned in the “depends” attribute have already run.

So execution order will be as follows:
1.                   target1
2.                   target2
3.                   target3
                if(condition == true)
4.                   runtarget
                endif

Let’s take another example.
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>

In this case if target D is executed then it will run in the following order.
A -> B -> C -> D
In this case target will not be executed twice.

‘antcall’ is a target/task. It invokes another target which exists in the build xmls.

<antcall target=”target1”/>

In this case processing will switch to another target specified in the antcall. Let’s look at the an example to illustrate the functionality.
<target name=”target1” />
<target name=”target2” >
<antcall target=”target1”/>
</target>
<target name=”target3” >
<antcall target=”target1”/>
<antcall target=”target2”/>
</target>

Now on invoking target3, following will be sequence of run.
Target3 -> target1 -> target2-> target1

As you can see, target 1 is run twice as it will called by target 3 independently and target 2 also invoked target 1 internally. Whereas when such duplicity exists in target called by depends attribute, then ant resolves such duplicate target.

Uses of antcall and depends in one example
Requirement: I have to build a project and create a jar file. Building of this jar depends on already build 100+ jar files, which are at some remote location at the starting of the project. If these jar files do not exist locally, ftp these jar files from remote location.

Approach 1: We will copy the jar files irrespective of their existence.
Approach 2: We will check whether jar files exists, if not then we will copy them from server.

Solution:
Approach 1 is not only time consuming but also it involves the risk of copying the file from remote location, which may not exists at time.
Approach 2 is efficient and nice. So let’s write the code for this.

<target name=”CopyJarsFromServer”>
<!—code for copying the jar using ftp -->
</target>

<target name=”validatejars”>
<if condition>
<antcall target=”CopyJarsFromServer”/>
<!—these is not other way to do it without using ant call -->
<end if>
</target>

<!—We are using depends attribute as we want to make sure that jar exists before any actual processing starts -->
<target name=”build” depends=”validatejars”>
<!—relevant code to build the jar -->
</target>

Hurrey, Now I understand the difference between antcall and depends, and I hope you too. J

Misuse of antcall task may hamper efficiency of your build script as antcall task does allow you to control what properties and references are defined (which is why a new Ant environment is created--and why it's so slow) so it can be used to create variants of the same thing; e.g., maybe two jars, one with and one without debug symbols.

So use antcall task only if it is not possible to find a workaround using depends target.

If you didn’t like my explanation and wants to check actual links that I have used to understand the difference, then please visit the followings

Wednesday, May 22, 2013

Jboss Esb - Imported file can not be resolved


I was working on an application that was built in Jboss Esb. I changed my source code and uploaded the jar, which was built using ant files.

As soon as I uploaded the file, Jboss bombed while starting it. It threw the exception mentioned below
The import com.XYZ.adapter.abc cannot be resolved.

I am new to JBOSS Esb. It was an urgent requirement and I needed to fix it ASAP. I did not have any idea to solve the issue in JBOSS ESB. I was cusring JBOSS and developer who thought to use the application. So I had to depend on Mr Reliable Google.

I search on google, and most of the people said that I have not build the jar in the correct manner. Whereas I was sure that I have build it correctly. I researched more. When Google stopped responding to my queries with satisfactory answers, then I started searching the answers at my own, and finally I found the solution for the problem.

I found that my class file did not have corresponding import statement(Which I lost while merging the file with version control tool). But my question was then how I got jar file. So I troubleshooted the ant scripts and found following.

-> My ant script was not deleting old class files. So instead compiling java source file, it was zipping already compiled files.
-> My Eclipse project did not show error, because auto building was disabled.
-> My Eclipse project and Ant Script was building class file at the same location. I.e. Both were building at ${basedir}/bin.

Keep it in mind, whatever technology you are using, it is based on one specific language. Irrespective of technology, the language concepts will always hold true.

The import com.XYZ.adapter.abc cannot be resolved.

Error listed above shows that there are compilation problems with the jar, so rebuild the jar with additional care.

Thursday, September 22, 2011

Eclipse/ANT Error Specified VM install not found



My company provided me new laptop and happily I moved from my old rusted desktop to new shining, powerful and portable laptop. Everything was copied without problem and I started working. But is not it strange to see everything working perfectly fine? I was stunned but happy as well. 

I started using the installed application. Eclipse was working fine until I tried building (Using ant) my application. Ant showed me a scary popup displaying "Specified VM Not found..." 



Simple solution, there is JDK/JRE problem with the system. Correct the JRE and JDK of the project. I did, but it did not work. I googled and found following 2 solutions.

You might even face this problem when you upgrade ant plug-in.

Solution 1
Remove the file which stores build information about the project and restart the eclipse. You can find the respective file in "${PROJECT_WORKSPACE}/.metadata/.plugins/org.eclipse.debug.core/.launches/${PROJECT_NAME} build.xml.launch"

Solution 2 (Location and Screen Shot may differ for different Eclipse Version. Following screen shot and paths are valid for Eclipse Version 3.7)

Go to RUN->External Tools ->External Tools Configurations… 


It will open console below. Select your build.xml(in my case it is abc build.xml). Move to JRE tab. 



Select the appropriate JRE.



Apply the changes and run the build xml. No need to restart eclipse, and you will not face the problem again.

Bmmm, Solved. Saved time.