Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

java build problem

I need a help with Ant  build

I have a folder called  "c:\myapps" . This folder contains "project1" folder and "project2" folder.

"project1\build.xml"  ----> builds "project1"  .

"project2\Apps\build.xml"   ----> builds "project2".


However, I dont wish to run them individually and so I need a FINAL build.xml to be present in folder "c:\myapps" which will help me to build all projects once.


Basically , I would like to invoke the final build.xml this way
c:\myapps\ant --> This will build all projects

How do I import the two build.xml into Final build.xml ?  I'm worried about  file paths .

SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of colr__
colr__

Not answering your question, but you might want to consider using maven instead for this. While Ant was good in it's day, it has now been pretty much superseded industry-wide by Maven. Maven handles this type of thing naturally with a parent project structure.
Your final build.xml should look like the one below (of course, it might be extended by adding new targets):

<project basedir="." default="build" name="final-build">

      <target name="build">            
            <iterate target="build"/>
      </target>

        <macrodef name="iterate">
            <attribute name="target" default=""/>
            <sequential>
                <subant target="@{target}">
                      <fileset dir=".">
                                <include name="*/build.xml"/>
                                <include name="*/Apps/build.xml"/>
                        </fileset>
                </subant>
            </sequential>
      </macrodef>
</project>
Avatar of cofactor

ASKER

@for_yan ,

I have gone thorugh your link and I have come up with this

<?xml version="1.0" ?>
 <project name="myapps" default="all">
<target name="all" depends="app1, app2" />

<target name="app1">
    <ant buildfile="project1/build.xml" target="war" />        
</target>

<target name="app2">
    <ant buildfile="project2/Apps/build.xml" target="ear" />        
</target>
 </project>

Open in new window


This does not work . It throws Error "ant doesn't support the "buildfile" attribute."

my Ant version: Apache Ant version 1.7.0

how do I fix this ?
@ionaton,

I modified your code  but when I run your code I get this " [subant] No sub-builds to iterate on".

My default build  targers are "war" for project1  and "ear" for project2

I ALSO  tried this way ..but this too did not work.

<?xml version="1.0" ?>
 <project name="myapps" default="all">
<target name="all" depends="app1, app2" />

<target name="app1">
    <ant buildfile="project1/build.xml" target="war" />        
</target>

<target name="app2">
    <ant buildfile="project2/Apps/build.xml" target="ear" />        
</target>
 </project>

Open in new window

I get error  "ant doesn't support the "buildfile" attribute."  for this.

Do you see any workaround ? whats the solution ?
Well, this works well only if all default targets have the same name. If you cannot do this, just replace the buildfile attribute with antfile in the other solution:

<target name="app1">
    <ant antfile="project1/build.xml" target="war" />        
</target>

<target name="app2">
    <ant antfile="project2/Apps/build.xml" target="ear" />        
</target>
ant -f <buildfile> can be used.

have a new build.xml with name say new.xml that invokes those two build.xml files as below

<import file="project/build.xml"/>
<antcall target="first_project_run" />
>>Well, this works well only if all default targets have the same name.

No . I can not . one is for WAR and another is for EAR. they are separate projects.

>>If you cannot do this, just replace the buildfile attribute with antfile in the other solution:

I did this. But still it does not work . It throws error . it says it can not find the lib's now for project1.


Here is the updated content:

myapps/build.xml
------------------------
<?xml version="1.0" ?>
 <project name="myapps" basedir="." default="all">
<target name="all" depends="app1, app2" />
<target name="app1">
    <ant antfile="project1/build.xml" target="war" />        
</target>

<target name="app2">
    <ant antfile="project2/Apps/build.xml" target="ear" />        
</target>
 </project>


myapps/project1/build.xml
-------------------
<project name="project1" basedir="." default="war">
    <property name="src.dir"     value="src"/>
    <property name="jar.dir"     value="pmd-4.2.5/lib"/>
   ........................
   ........................


myapps/project2/Apps/build.xml
--------------------------------------------
 <project name="project2" default="ear">
   <property environment="env"/>

    <path id="compile.classpath">
       <fileset dir="WebContent/WEB-INF/lib">
          <include name="*.jar"/>
       </fileset>
        
         <pathelement location="serverlib/servlet-api.jar"/>
        
   </path>
   .............
   ..............

why the build still fails ? In final build  time  project1's  build.xml now unable to find lib folder from pmd-4.2.5/lib !
what happened ?  
That's another problem. Please check the following:
1. Does the folder pmd-4.2.5/lib really exist? If not, create it.
2. Where is the pmd-4.2.5 located as against the basedir? It should be in the same folder, otherwise you have to change the value of the property "jar.dir" to point to the correct location
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>1. Does the folder pmd-4.2.5/lib really exist? If not, create it.

yes ..absolutely ..its there . its under  myapps/project1/pmd-4.2.5/lib

>>>2. Where is the pmd-4.2.5 located as against the basedir?

 its under myapps/project1/pmd-4.2.5/lib .

>>>It should be in the same folder,

what do you mean by same folder ?




In the same folder with the build.xml file which refers it. Please follow the suggestion I made afterwards. This should do the trick.
>>>Please follow the suggestion I made afterwards. This should do the trick.

Yes.This works ...happy.

By the way, what does inheritAll="false" is doing basically ? I dont understand the meaning of this attribute.

Could you please explain what its doing ?

Also please post the Ant doc link which explains particular attribute.
The inheritAll attribute indicates whether you want or not to pass all properties available in the caller build.xml to the other, called build.xml. These properties include, among other things, the basedir, so if the attribute has the value "true" which is the default,  Ant looks for pmd-4.2.5 in the folder the caller build.xml resides in, i.e. myapps.
For details, check this: http://ant.apache.org/manual/Tasks/ant.html
Excellent