views:

186

answers:

2

Hi, I'm working on an automation project for my employer. We have a pool for each revision of our source code. When you download a revision, you need to create a directory structure with a bunch of third party includes to eventually build the project. I've automated this entire process up to the point of having my script (.bat) compile each particular runnable java application. There are many applications to this single project, and the directory listing looks something like this:

Proj Name
   -variousincludesfolder1
   -variousincludesfolder2
   -variousincludesfolder3
   -variousincludesfolder4
   -runnableapplicationsandmoreincludes
       -con.java

Right now, I'd like to do an automated compiling of con.java, but I don't know where to begin. People have suggested I try Ant, but any automated Ant file generation I get using Eclipse seems only enough to build con.java while an active project file exists. Is there anyway to automate this without using eclipse, to the point of having the batch file generate a .jar itself?

Thanks!

+4  A: 

This is definitely a job for Ant. Don't rely on Eclipse-generated Ant files; read through the manual and write one yourself. (You'll likely find out that Ant does things you didn't think of doing in your build script, too.)

To be more specific, here is the documentation for the jar task.

Michael Myers
But can Ant be used to automatically find and compile all associated classes that may be needed, but those includes often change and are usually complicated and time-consuming to find which are needed? Thanks.
Monster
I'm not quite sure what you mean, so I'll counter with another question: How are you doing this right now?
Michael Myers
Well, ok, basically before I goto compile anything or use eclipse, I have a directory structure with java files all about. Some of these can be compiled into Java Applications. But because of the size of the project, one Java Application may be interdependent on hundreds or even 1000+ other source files.So as of right now, I import my directory structure into a new Eclipse project. Then I go an compile my app (con.java), and it takes care of compiling all of the other class files. But I'd like to do this all from the command line.
Monster
Sounds like you would be better off modularizing your source. But I'll see what I can dig up in Ant's manual (I'm not an Ant guru, unfortunately).
Michael Myers
Thanks, I appreciate it! I'll do the same.
Monster
+1  A: 

You can define wildcard and pattern matches to include/exclude all sorts of files and folders in your build. Take a look at the ANT manual to see how things like filesets work with include and exclude filters.

Also, read the tutorial.

Here is a simple build file that looks to compile all java files and reference all jars. Place it in the top level directory:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" 
    href="http://www.ibm.com/developerworks/xml/library/x-antxsl/examples/example2/ant2html.xsl"?&gt;
<project name="Proj Name" default="build" basedir=".">
    <property name="src.dir" value="${basedir}" description="base folder where the source files will be found.  Typically under /src, but could be anywhere.  Defaulting to root directory of the project" />
    <property name="build.dir" value="build" description="Where to put build files, separate from src and resource files." />

    <path id="master-classpath">
        <fileset dir="${basedir}" description="looks for any jar file under the root directory">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <target name="build" description="Compile all JAVA files in the project">
        <javac srcdir="${src.dir}" 
            destdir="${build.dir}/classes" 
            debug="true" 
            deprecation="true" 
            verbose="false" 
            optimize="false"  
            failonerror="true">
            <!--master-classpath is defined above to include any jar files in the project subdirectories(can  be customized to include/exclude)-->
            <classpath refid="master-classpath"/>
            <!--If you want to define a pattern of files/folders to exclude from compilation...-->
            <exclude name="**/realm/**"/>
        </javac>  
    </target>

</project>
Mads Hansen