Difference between setting classpath in build.xml using fileset and pathelement - java

I have a build file that declares the classpath as shown
<path id="compile.classpath">
<fileset dir="${basedir}/lib" includes="**"/>
<fileset dir="${jboss.home}/lib" includes="**"/>
<pathelement path ="${build.classes.dir}"/>
</path>
I tried looking at the documentation but I am not able to understand the use of
pathelement.
I know that the ID is used to refer to this class path while performing a task and fileset includes the jarfiles.
edit 1:
My doubt is Why can't we use fileset to include the class files in place of pathelement?

Latest edit:
My doubt is Why can't we use fileset to include the class files in place of pathelement?
If you use a fileset then you'd be adding a set of class files to the path as follows:
CLASSPATH=classes/MyClass1.class:classes/MyClass2.class:classes/MyClass3.class:....
When what Java expects to see is simply:
CLASSPATH=classes
Only jar (and WAR,EAR,etc) files are explicitly listed on the classpath (Java will open them up and load their class files), hence the need for a fileset in ANT.
Update
Here's the Oracle documentation:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:
For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).

There was already similar question about 'pathelements' here. From the provided documentation:
"If it's path structure like in your example: "A path-like structure can include a reference to another path-like structure (a path being itself a resource collection) via nested elements"
<path id="base.path">
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</path>
If it's classpath structure:
"The path attribute is intended to be used with predefined paths"
<classpath>
<pathelement path="${classpath}"/>
<pathelement location="lib/helper.jar"/>
</classpath>

Related

Unknown level in directory, fetch the name of directory with .java file in it

How to access the directory name where I only know it contain .java files and level of directory keep on changing. so is their any way in ANT Script to access those directories with particular .java files in it.
I am using below Script:
<target name="check">
<for param="check1">
<path>
<dirset dir="${project_path}/adapters" includes="*/src/*.java"/>
</path>
<sequential>
<echo>#{check1}</echo>
</sequential>
</for>
</target>
Result:
It is giving nothing as output because it is searching .java file inside src folder but .java files are present under multilevel directories of src. I am not getting how to do it.
As the ant fileset doc states (https://ant.apache.org/manual/Types/fileset.html) you can use ** to search recursively into directories.
<dirset dir="${project_path}/adapters" includes="*/src/**/*.java"/>

Create Jar for a specific file in a project using ant

I compiled all the java files using ant and stored the class files in a folder.
I want to make jar for a specific class file form the output folder(compiled).
How can i done this.
NOTE:The class file depends another classes in the project
Use the ant Jar Task with a nested fileset that specifies only the file(s) you wish to include in the jar file.
Something like:
<jar jarfile="pinky.jar">
<fileset dir="build/classes" >
<include name="com/example/Brain.class" />
</fileset>
</jar>

Run a Java Program from an Ant Build File

So I wrote an ant build.xml file where I take the class files from two Java programs, one that extends the other, package them up into two separate jar files, and then, launches them.
<java classname="Main">
<classpath>
<pathelement location="${mainDir}"/>
<pathelement path="${Main-Class}"/>
</classpath>
</java>
Whenever I invoke ant, it says that "Main" can not be found. I can post the rest of the build.xml file if needed but it's really just this part that I'm confused about. I'm pretty sure that I have the classname right but my biggest problem is figuring out what goes in for location and path. Right now I just have dummy variables.
EDIT: Here's the whole file.
<?xml version="1.0"?>
<project default="dist" name="webscarab">
<description>Class</description>
<property name="ClassFiles" location="..\Simple\trunk\dev\Class\bin\" />
<property name="mainClassFiles" location="..\Simple\trunk\dev\main\build\" />
<property name="buildDir" location=".\build" />
<property name="distDir" location=".\dist" />
<property name="mainDir" location="..\Simple\trunk\dev\webscarab\src\" />
<target name="init">
<tstamp/>
<mkdir dir="${buildDir}"/>
<mkdir dir="${distDir}"/>
</target>
<target name="dist" depends="init">
<jar destfile="${distDir}/package${DSTAMP}.jar" basedir="${ ClassFiles}"/>
<jar destfile="${distDir}/package-web-${DSTAMP}.jar" basedir="${mainClassFiles}"/>
<java classname="Main">
<classpath>
<pathelement location="${mainDir}"/>
<pathelement path="${Main-Class}"/>
</classpath>
</java>
</target>
</project>
EDIT 2: I should mention everything is already compiled and I have all the .class files. It's not something I need to do in the Ant file.
The error message about “class cannot be found” is likely due to an incorrect classpath.
The classpath should grant access to all the class and resource files your application requires. It is composed of a set of directories and jar files. location will denote a single directory or path, whereas path will denote multiple, separated by ; or : depending on your platform.
In your case, it seems to me that you want your classpath to either consist of ${pegaFuzzClassFiles} and ${webScarabClassFiles} as directories, or of ${distDir}/package-pega-${DSTAMP}.jar and ${distDir}/package-web-${DSTAMP}.jar as jar files. You could do this using a single <pathelement path="…"/> element, but I'd suggest multiple <pathelement location="…"/> as I consider this to be clearer.
You should also make sure that any third-party libraries used by your application are available on that path as well. You could nest one or more <fileset> into that <classpath>, each of them describing all the jar files in a given directory.
You probably need to change the elements to point to the Jars that are being created, right now the classpath would appear to point to the source, which is not going to work as the classpath needs the compiled .class files or the jars that contain them.
I would also, just as a matter of style, move the task into a separate 'run' or 'run-app' target that depends on the dist target.
You need to Javac task to compile your code and after that you can run your compiled class. For more information about Javac task visit http://ant.apache.org/manual/Tasks/javac.html
The second advice:
The use of the unix slash '/' is strongly recommended, whether you're on
windows or not. Change your mainDir,distDir and buildDir property locations.
I don't have a ton of ant experience, but you might need to explicitly tell java which jar file to run.
<java jar="path-to-jar-file" classname="org.owasp.webscarab.Main">

Using ant to create a distributable zip file with JAR and libraries and doc

I have a library that is typically distributed by making a zip file of the JAR and its dependencies and its javadoc by hand. I would like to automate this task in ant.
One quirk for the intended use case for this distribution is that when it is unpacked, the JAR my team has created and any library JARs should all be in the same path. We cannot have myproject.zip/the.jar and myproject.zip/lib/a_library.jar both should be in the root path of the zip.
I have had much success using the following task:
<target name="myproject.distributable" depends="artifact.mycompany_myproject, myproject.javadoc"
description="Build the distributable JAR for myproject">
<zip destfile="${basedir}/dist/myproject.zip">
<fileset file="${temp.jar.path.mycompany_myproject.jar}"/>
<zipfileset dir="mycompany_myproject/lib" prefix="lib">
<patternset id="myproject.dist.libs">
<include name ="**/*.jar"/>
</patternset>
</zipfileset>
<zipfileset dir="docs/myproject" prefix="docs"/>
</zip>
</target>
The only thing it doesn't do is 'flatten' or move the library JARs to the root path of the zip.
I have tried using <zipfileset prefix="/"> for the libs but that did not work as expected.
The prefix attribute of the zipfileset is used to describe where the files should appear in the created zip file. Since you want the jar files to appear at the root of the zip file you don't need to specify this, and can leave it out (I'm not sure what the effect of setting it to "/" will be, I think it'll be safer to omit it).
You problem seems to be that your libs are stored in subdirectories under your lib dir, but you want them to be directly in the root of the zip file. The 'zip' task, unlike the copy task, doesn't accept a mapper directly to change how files should appear in the zip, but if you're using ant 1.7 or later it will accept a resource collection. You can use a mappedresources element with a fileset and a flattenmapper to get the effect you're after:
<target name="myproject.distributable" depends="artifact.mycompany_myproject, myproject.javadoc" description="Build the distributable JAR for myproject">
<zip destfile="${basedir}/dist/myproject.zip">
<fileset file="${temp.jar.path.mycompany_myproject.jar}"/>
<mappedresources>
<fileset dir="mycompany_myproject/lib" includes="**/*.jar" />
<flattenmapper />
</mappedresources>
<zipfileset dir="docs/myproject" prefix="docs"/>
</zip>
</target>
This means you don't have to use copy first to put the jars into a staging area.
According to this post the zipgroupfileset should do the trick; have not tried it myself though...

How can I include data text files in a jar using Ant?

In my src folder there is another folder called data which contains files data1.txt and data2.txt. The application loads a graph from these files in the initialization, so I want to include these files in my final jar. I use Ant to produce the jar file.
Example from http://ant.apache.org/manual/Tasks/jar.html :
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"/>
<fileset dir="${src}/resources"/>
</jar>
So basically you would want to include the data-files in the same way as "resources" are included above.
From the documentation of the <jar> task:
It is possible to refine the set of files that are being jarred. This can be done with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes.
Copy the files to your classes directory, where they will be included into the jar.
enter code here
<target name="copyHibernateXml">
<copy todir="classes">
<fileset dir="${basedir}/${sourceDir}" includes="*.xml,*.csv"/>
</copy>
</target>

Categories