将Jar添加到Ant构建

时间:2016-05-19 13:18:08

标签: java ant build travis-ci

我尝试将json jar添加到我的Travis-CI ant构建中,我还检查了stackoverflow中的问题这个问题可能是重复但我无法处理它。当travis编译我的代码时,它不能是JSON,我认为我在构建xml中做错了。请帮助我,我是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>

3 个答案:

答案 0 :(得分:1)

这是因为java编译器无法找到编译所需的类。您需要通过类路径向javac提供所有必需的罐子。

因此,在构建xml中,您需要指定类路径:

  <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>

然后在执行javac时使用此类路径,如下所示:

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

同样,您必须在使用java

运行课程时执行此操作

有关详细信息,请参阅writing a simple build file

答案 1 :(得分:1)

我想添加此示例,该示例涵盖如何共享classpath.test(例如gson)中的依赖项(跨不同的ant命令),这将使CI与此build.xml一起传递也ant test命令!

<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>

答案 2 :(得分:0)

这是我的解决方案

<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>