Ant build using two projects - java

I have two separate projects inside of eclipse: "project" and "pinclude"
Project includes pinclude, so without somehow include the java files for that project inside of my build.xml, javac will always return errors.
How do I got about including .class files inside of ant/javac? I've tried searching for a solution, but so far I've only came up with ways of adding jar files. Would creating a jar of all the "pinclude" .class files fix my problem?.
Thank you for your help.
NOTE:
I'm sorry for the poor naming convention; These are just projects I made to figure out this problem out.
Also, please ignore srcdir and destdit, they are not important.
Build.xml
<project name="project" basedir="." default="dist" >
<target name="dist" >
<javac destdir="bin"
srcdir="${basedir}\myfileslocation\" >
</javac>
</target>

How do I got about including .class files inside of ant/javac? I've
tried searching for a solution, but so far I've only came up with ways
of adding jar files. Would creating a jar of all the "pinclude" .class
files fix my problem?.
Yes. It helps to separate functionality into smaller modules. If you export one project (pinclude in this case) into a jar and import(via classpath) in another , that is the most correct step.

I'm not 100% sure what you need. Are you saying that you have class files from pininclude, and you need to include them in your <javac> compile task? It would be something like this:
<javac destdir="bin"
srcdir="${basedir}/myfileslocation">
<classpath>
<pathelement path="${path.to.pininclude.javac.files}"/>
</classpath>
</javac>

Related

Copy external docs (from xyz-javadoc.jar) into JavaDocs

I want to create a .jar file with Ant, which contains all JavaDocs of my library and its dependencies. I did a lot of searching yesterday afternoon/evening and this morning, but none of the solutions work for me.
My first solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}" windowtitle="${ant.project.name}">
<classpath path="${lib}/nv-websocket-client-2.9-javadoc.jar"/>
<classpath path="${lib}/gson-2.8.6-javadoc.jar"/>
</javadoc>
My second solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}">
<classpath>
<fileset dir="${lib}">
<include name="gson-2.8.6-javadoc.jar"/>
<include name="nv-websocket-client-2.9-javadoc.jar"/>
</fileset>
</classpath>
</javadoc>
In both cases, however, only the JavaDoc for my own code is produced. The libraries are completely ignored. In the log of the Ant-Task there are errors, that the classes from the libraries were not found.
I don't know if this is the best aproach, but for me it's easier to use "jar" from command line.
Then all you have to do is indicate where are your files located:
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_files\*.*
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_other_files\*.*
If the libraries you want to add are already packed in a jar file, I would extract them first in the root directory, so the path of every file is correct. If you execute the previous commands, you'll have all your files in the "your_jar_file.jar" file.
if you type jar --help from command line you'll see more options. I hope it helps.

how to compile only changed source files using ANT

I am trying to write ant build for compiling source folder here is my script target for compiling.
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true">
<classpath refid="master-classpath"/>
</javac>
</target>
In my project I have near about 1000 .java files. When ever a single .java file is changed above target tends to compile all .java files. Which make development very slow. I just want to know is there any way or code to change the behavior of task to compile only modified or changed .java file rather than all .java file.
Please help me.
As I understand, compiling only the modified java files is the default behavior of ant javac task. Ant uses the timestamp of a .java file and its corresponding .class file to determine if the a Java file needs to be recompiled. I have used it in many projects and never have issues.
Here are a few things you can check
Is your source tree directory structure matching your Java package
structure? Please see Why does ant always recompile all my Java
files?. This is probably the number 1 reason since it is in the FAQ.
I see your compile target depends on init target? What does the init target do? Does it depends on any clean target or does it cleanup the .class files?
What is your ant version? Can you try a different ant version to see if you still have the same problem?
If none of the above fixes your issue, after the compilation, can you manually check the .class file timestamp and compare it with the timestamp of the corresponding .java file?
So if you are not deleting the classes directory in any dependent task say 'clean' the javac task inherently compiles only the changed java files. It is an out of the box feature that javac task provides. Hope it helps.

Using Ant to build multiple jars

I have multiple Java eclipse projects. Each of them has "jardesc" file for building jar. It's nice - double click -> finish and jar file is made. But when i have to export several jars it's a pain - i have to repeat procedure several times.
Please tell me, can i use Ant script to run several "jardesc" files at once (and get several jars according to each jardesc file)? How to do it?
You could use the jar target to make the jars for you:
<jar destfile='destination.jar' basedir='source\dir\' />
so your build.xml would look a little like this:
<project default="makejars">
<target name="makejars">
<jar destfile="app1.jar" basedir="app1\src\" />
<jar destfile="app2.jar" basedir="app2\src\" />
<jar destfile="app3.jar" basedir="app3\src\" />
</target>
</project>
then just run ant in the same directory as build.xml, and the jars should be made.
Take a look at subant task in ant. You can create ant-file which would call other files to.
<subant target="create_jar1">
<fileset dir="." includes="jar2.xml"/>
</subant>
<subant target="create_jar2">
<fileset dir="." includes="jar1.xml"/>
</subant>
You can use some loops to create ant parameters however there is no way to loop to create multiple jars (even with ant-commons extension), a copy & paste is the only viable solution unless you want to write an ant plugin (which doesn't really take that much 2 hours reading docs + write simple plugin)

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">

Java: How to compile a runnable jar from packages?

My Java application has got a package structure similar to this:
src/com/name/app
src/com/name/app/do
src/com/name/utils/db
How would I go about compiling Java files in these directories in to a runnable jar? I need to package required libraries into the generated JAR (jdbc).
I've always done these things in Eclipse but now I need to supply a couple of people with a way to compile the repository without the use of eclipse and I was thinking of making a makefile or a script that invokes the necessary javac pattern.
Take a look at Ant. It's a relatively simple build tool to understand, and provides everything that meets your requirements. Here's a quick skeleton build.xml to get you started:
<project name="my_app_name" default="jar">
<target name="compile">
<javac srcdir="src" destdir="bin">
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="jar">
<jar manifest="manifest_file" destfile="dist/my_app_name.jar">
<fileset dir="bin" />
<fileset dir="lib" />
</jar>
</target>
You need to create a manifest file that will tell the java process which class holds the "main" method. Here is a good place to start learning about manifests.
As an alternate that produces really cluttered Ant build files, you can right click on your Eclipse project and choose "Export...", then choose "General > Ant Buildfiles".
Anyway, that should get you started. You can ask more specific questions as you run into them.
First of all, consider using Ant for such a task.
But since you asked for a manual process, you need to first create a manifest file, like so:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Class-Path: lib/jdbc.jar lib/otherlib.jar
Main-Class: com.name.app.MainClass
Replace the contents of Class-Path with your libs, and Main-Class with the fully qualified name of your main class.
Then, you need to generate the actual .jar, using the following command:
jar cfm app.jar MANIFEST.MF src/com/name/app/*.class src/com/name/app/do/*.class
Where MANIFEST.MF is the previously mentioned manifest file, and the rest is the folders where your .java classes lie in.
Finally, to run your app, you simply execute: java -jar app.jar.
Consider using Ant to do this. http://ant.apache.org/
I recommend that you use Apache Ant to implement your build scripts.
If implemented correctly, Ant is easy to use and the build scripts can be run on any platform that you can install a JDK on. Indeed, with a little bit of work, you can even set up your project so that users don't even need to download / install Ant. (Hint: add the Ant JAR files and a wrapper script to your project distro)

Categories