ozanonurtek
ozanonurtek

Reputation: 306

Adding Jar To Ant Build

I try to add json jar to my Travis-CI ant build, I also checked questions in stackoverflow this question is might be duplicate but I cant handle with it. When travis compiles my code it can not JSON, I think I do something wrong in build xml. Please help me, I m new at Ant.

  <project name="Hello" basedir="." default="main">
       <property name="dir.src"     value="src"/>
       <property name="dir.build"   value="build"/>
       <property name="dir.classes" value="${dir.build}/classes"/>
       <property name="dir.jar"     value="${dir.build}/jar"/>

       <property name="main-class"  value="TestConsole"/>
       <target name="clean">
           <delete dir="${dir.build}"/>
       </target>

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

           <target name="jar" depends="clean, compile">
              <mkdir dir="${build}/${jar}" />
              <jar destfile="src/json-simple-1.1.1.jar" basedir="${build}/${classes}">
               <manifest>
                   <attribute name="Main-Class" value="MainClass" />
               </manifest>
              </jar>
       </target>

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

       <target name="clean-build" depends="clean,jar"/>
       <target name="main" depends="clean,run"/>
  </project>

Upvotes: 1

Views: 1967

Answers (3)

binarytrails
binarytrails

Reputation: 645

I wanted to add this example which covers how to share dependencies found in classpath.test (such as gson) across different ant commands which will make the CI pass with this build.xml on ant test command too!

<project name="junit-example">
  <property name="main.build.dir" value="build/main"/>
  <property name="main.src.dir" value="src/package"/>
  <property name="test.build.dir" value="build/test"/>
  <property name="test.src.dir" value="tests/package"/>

  <path id="classpath.test">
    <pathelement location="lib/junit-4.12.0.jar"/>
    <pathelement location="lib/org.hamcrest.core_1.3.0.jar"/>
    <pathelement location="lib/gson-2.8.6.jar"/>
    <pathelement location="${main.build.dir}"/>
  </path>

  <target name="compile">
    <mkdir dir="${main.build.dir}"/>
    <javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false">
        <classpath refid="classpath.test"/>
    </javac>
  </target>

  <target name="test-compile" depends="compile">
    <mkdir dir="${test.build.dir}"/>
    <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
        <classpath refid="classpath.test"/>
    </javac>
  </target>

  <target name="test" depends="test-compile">
    <junit printsummary="on" haltonfailure="yes" fork="true">
        <classpath>
          <path refid="classpath.test"/>
          <pathelement location="${test.build.dir}"/>
        </classpath>
        <formatter type="brief" usefile="false" />
        <batchtest>
            <fileset dir="${test.build.dir}" includes="**/*Test*" />
        </batchtest>
    </junit>
  </target>
</project>

Upvotes: 1

ozanonurtek
ozanonurtek

Reputation: 306

Here is my solution

<project name="Hello" basedir="." default="main">
<property name="dir.src"     value="src"/>
<property name="dir.build"   value="build"/>
<property name="dir.classes" value="${dir.build}/classes"/>
<property name="dir.jar"     value="${dir.build}/jar"/>
<property name="dir.lib"     value="."/>
<property name="main-class"  value="TestConsole"/>
<target name="clean">
    <delete dir="${dir.build}"/>
</target>

 //Edited part

<target name="compile">
<mkdir dir="${dir.classes}"/>
<javac srcdir="${dir.src}" destdir="${dir.classes}">
    <classpath>
           <fileset dir="${dir.lib}">
              <include name="**/*.jar" />
           </fileset>
     </classpath>
  </javac>
</target>

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

//Edited part ends here

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

<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 9946

This is because java compiler is not able to find required classes for compilation. You need to make available all required jars to javac by means of class-path.

So in your build xml you need to specify class path like this:

  <path id="project.class.path">
    <pathelement location="bin/"/> <!-- to put a folder in class path -->
    <pathelement location="lib/helper.jar"/> <!-- to put a jar in class path -->
    <pathelement path="${java.class.path}/"/>
  </path>

And then use this class path while executing javac like this:

<javac srcdir="${dir.src}" destdir="${dir.classes}">
     <classpath refid="project.class.path"/>
</javac>

Similarly you have to do it while running your class using java

Please see writing a simple build file for more information.

Upvotes: 2

Related Questions