1

I am trying to build an executable jar program using a ant script. When the project doesn't have any dependencies it works fine, but when I add another project as dependency, the ant script don't find the classes from this.

I did a simple just to test and understand how it works.

Follow:

Ant script example:

<target name="compile">
    <mkdir dir="${classes.dir}" />
    <javac srcdir="${src.dir}" destdir="${classes.dir}" />
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}" />

    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" includes="*.class" >
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
        </manifest>
    </jar>
</target>

<target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true" />
</target>

My project structure is like this:

AntHelloWorld depends AntTest. My build.xml is in AntHelloWorld and I wanna modify my ant build.xml to works with the project dependency. (I can't post images yet)

I tried to find this and I just saw how to do this with jar dependencies, How to build an executable jar with external jar?

This is my first question in stackoverflow, feel free to inform where can I improve.

Community
  • 1
  • 1
Klinsman Maia
  • 45
  • 1
  • 1
  • 5

1 Answers1

0

The javac directive can have subelements that include a classpath. You can put your dependent project's JAR files on the classpath. Consider this example which demonstrates putting JAR files on the classpath:

//This goes inside the javac element     
    <classpath>
         <fileset dir="${dependentProject}/lib">
                <include name="**/*.jar" /> 
         </fileset>
     </classpath>

As a second example, if you want to include class files from your dependent project, and your class files are located in the build directory:

 <classpath>
     <pathelement location="${dependentProject}/build" />
     <fileset dir="${dependentProject}/lib">
            <include name="**/*.jar" /> 
     </fileset>
 </classpath>
KyleM
  • 4,445
  • 9
  • 46
  • 78
  • Thanks, but my dependency isn't a jar file, it's a project (I added the project folder in build path). How to build my project without generate the jar file from the dependent project? I mean, it's possible to do this using just one ant script? – Klinsman Maia Feb 19 '15 at 17:20
  • @KlinsmanMaia Yes it's possible. Just use two javac commands. The first javac command will build your first project and put the class files in the "build" folder. The second javac command will build your other project, and you will use a pathelement location of "firstProject/build" for the classpath - just like my example above. – KyleM Feb 19 '15 at 17:26
  • Thank you very much, I'll try to do this now. – Klinsman Maia Feb 19 '15 at 17:30