Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ant to compile java code

Tags:

java

ant

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.

like image 509
de1337ed Avatar asked Apr 11 '26 07:04

de1337ed


1 Answers

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>
like image 183
Diego Shevek Avatar answered Apr 12 '26 20:04

Diego Shevek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!