File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ant, Maven and Other Build Tools and the fly likes urgent help please (creating jar file using ant) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "urgent help please (creating jar file using ant)" Watch "urgent help please (creating jar file using ant)" New topic
Author

urgent help please (creating jar file using ant)

suchitra gowda
Greenhorn

Joined: Oct 15, 2003
Posts: 8
Hi,
I am able to create jar file with all class files but I want to include
class files and java files with corresponding directory structure in the jar file.
I have one folder structure like
c:/src/src1
Now src contains some java files and src1 contains some other files.
What changes i need to do in the following code to include java files
also in the jar file.
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" />
</target>
<target name="dist" depends="compile" description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}" />
</target>
<target name="clean" depends="dist" description="clean up" >
<!-- Delete the ${build} directory trees -->
<delete dir="${build}"/>
</target>
</project>

Any help how to add class files also to jar file.
Thanks,
Suchi.
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
<jar destfile="${dist}/lib/app.jar"
basedir="${build}/classes"
includes="mypackage/test/**"
excludes="**/Test.class"
/>
This is the sample from Ant doc. Maybe try using attribute
includes="**/*.java"
It should pick up all the java files from basedir down.


Tony Yan<br /> <br />IBM Certified Developer XML and Related Technology<br />Sun Certified Web Component Developer For J2EE Platform<br />Sun Certified Programmer For Java 2 Platform
suchitra gowda
Greenhorn

Joined: Oct 15, 2003
Posts: 8
Hi,
The problem is my base directory is c:\ and if i say
includes="**/*.java" it picks from all directories. But I want to pick up those class files from 2 or 3 folders not from all the folders.
C drive has many folders and each folder contains lot files.
I have one more question.
Why is my delete is not working. The directory is not deleting. Even if print some message using under tag
<target name="clean" depends="dist" description="clean up" > like
<echo message="Hello" /> this message is also not printing.
Which means control is not entering into this tag.
Thanks,
suchi.
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
It seems to me that you have some hidden problems. You can start the Ant with -verbose to see if things are doing what you intend to. Post the dumps if you want here.
suchitra gowda
Greenhorn

Joined: Oct 15, 2003
Posts: 8
Hi,
I am not clear what is mean by -verbose.
I comiled my ant script using ant -v -buildfile test.xml then it is displaying all the stuff except the message in the clean .
I added the following code before compile.
<target name="clean" description="Delete all old files" >
<echo message="Deleting all old class files" />
<delete includeEmptyDirs="true" >
<fileSet dir="" >
<patternSet refid="class.files" />
</fileSet>
</delete>
</target>
I can not see the echo message in the logs which means that this target tag is not executed.
Any help please how I have to solve this problem.
Thanks,
suchi.
suchitra gowda
Greenhorn

Joined: Oct 15, 2003
Posts: 8
Hi,
If i try to delete the directory after jar file creation like
<target name="dist" depends="compile" >
<jar jarfile="${dist}/lib/MyProject.jar" basedir="${build}" />
<delete dir="${build}"/>
</target>
now the directory deleted successfully.
If i use that delete tag in different target then the directory is not deleted.
for ex:
<target name="clean" description="clean up" >
<delete dir="${build}"/>
</target>
What is the diff between these and why is not working ...
Any help on this please.
Thanks,
suchi.
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Sorry. I didn't look carefully. The depends="dist" will not work since your project default target is "dist", so the Ant will just finish everything that dist is depending on recursively ONLY. If any task is NOT in the dependence tree of "dist", it is not to be executed.
-v and -verbose are the same, I think.
Hope it helps.
suchitra gowda
Greenhorn

Joined: Oct 15, 2003
Posts: 8
Hi Tony,
Thanks for your quick response.
I got it . What ever you said is correct ..
Thanks,
Suchi.
suchitra gowda
Greenhorn

Joined: Oct 15, 2003
Posts: 8
Hi Tony,
I have one question regarding class path.
Here is the code i am using:
<!-- Tool directories as defined by the environment variables -->
<property environment="env"/>
<property name="classpath" value="${env.path}"/>
<!-- Classpath to compile our EJBs -->
<path id="server.classpath">
<pathelement location="${classpath}" />
</path>
<target name="compile" >
<javac srcdir="." classpathref="server.classpath" >
<patternset refid="source.files" />
</javac>
</target>
I am unable to compile my java files because of class path problem.
I tested by setting classpath also in environment variables.
Any help on class path .
thanks,
suchi.
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
You can add something like this
<echo message="${classpath}"/>
to see if you are getting the right path from env.path. Looks okay to me as far as the buildfile goes. However, make sure if you are running on UNIX system, the path should be case-sensitive. Thus, env.path != env.PATH.
BTW, shouldn't you use env.CLASSPATH instead of env.PATH?
suchitra gowda
Greenhorn

Joined: Oct 15, 2003
Posts: 8
Hi Tony,
Thanks for your response.
I got solution bu using path and pathelement tags like
<path id="fuclasspath">
<pathelement location="\common.jar" />
<pathelement location="\j2ee.jar" />
</path>
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Great! I'd recommend the same that you set CP inside each javac since in that way, you will have a clean CP to use for that task. Introducing CLASSPATH from Ant Env can potentially cause some problems for CP, as we know it.
 
Consider Paul's rocket mass heater.
 
subject: urgent help please (creating jar file using ant)