3

I'm a little new to using ant, and currently, the way I make ant scripts is by auto-generating them through eclipse in order to produce runnable jar's. The problem with this is that it only reads the bin directory. As a result, If I were to change a java src file, I wouldn't see the changes replicated in the ant build. What do I need to add to my ant script? I've shown an example script below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <project default="create_run_jar" name="Create Runnable Jar for Project poodah">
    <!--this file was created by Eclipse Runnable JAR Export Wizard-->
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="../lib/TestMaster.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Main-Class" value="test.startup.TestMaster"/>
                <attribute name="Class-Path" value="."/>
            </manifest>
            <fileset dir="../test/bin"/>
        </jar>
    </target>
    </project>

I tried reading some of the documentation but it was a little confusing.

1
  • You need to know that Eclipse automatically compiles the changed source code (/src) and puts it in /bin; it will even copy other (non-Java) files from /src to /bin. This way the /bin folder is always up to date. That is as long as you are making the changes in Eclipse. It seems that you have tried to do some changes outside Eclipse. In that case you need to have a standalone build.xml like others have suggested below. Commented Apr 12, 2013 at 20:15

3 Answers 3

8

You need to compile your sources with javac ant's task

Suppose your project structure is:

java
  your
    package
      structure
         SomeClass.java
lib
  log4j.jar
  guava-14.jar
test
  bin
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project poodah">
  <!--this file was created by Eclipse Runnable JAR Export Wizard-->
  <!--ANT 1.7 is required                                        -->
  <target name="create_run_jar" depends="compile">
      <jar destfile="../lib/TestMaster.jar" filesetmanifest="mergewithoutmain">
          <manifest>
              <attribute name="Main-Class" value="test.startup.TestMaster"/>
              <attribute name="Class-Path" value="."/>
          </manifest>
          <fileset dir="../test/bin"/>
      </jar>
  </target>

  <target name="compile">
    <javac srcdir="java" destdir="../test/bin" includes="**/*.java" target="1.6">

        <classpath refid="classpath.base" />
    </javac>

  </target>
  <!-- Libraries on which your code depends -->
  <path id="classpath.base">                                                                                                                           
     <fileset dir="lib">                                                                                                                          
         <include name="**/*.jar" />                                                                                                          
     </fileset>                                                                                                                                   
  </path>  
</project>
Sign up to request clarification or add additional context in comments.

Comments

0

Add your compile target as dependency

<target name="create_run_jar" depends="compile">
    <jar destfile="../lib/TestMaster.jar" filesetmanifest="mergewithoutmain">
        <manifest>
            <attribute name="Main-Class" value="test.startup.TestMaster"/>
            <attribute name="Class-Path" value="."/>
        </manifest>
        <fileset dir="../test/bin"/>
    </jar>
</target>
</project>

Compile target

<target name="compile" depends=""   description="compile the java source files">  
 <javac srcdir="." destdir="../test/bin">  
    <classpath>  
        <fileset dir="${lib}">  
            <include name="**/*.jar" />  
        </fileset>  
       </classpath>  
</javac>  

Comments

0

You need to add a <javac> task to your Ant script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.