用ant编译java代码

时间:2013-04-12 18:59:23

标签: java ant

我对使用ant有点新,目前,我制作ant脚本的方法是通过eclipse自动生成它们以生成可运行的jar。这个问题是它只读取bin目录。因此,如果我要更改java src文件,我将看不到在ant构建中复制的更改。我需要添加到我的蚂蚁脚本中?我在下面展示了一个示例脚本:

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

我尝试阅读一些文档,但这有点令人困惑。

3 个答案:

答案 0 :(得分:8)

您需要使用 javac ant的任务

编译源代码

假设您的项目结构是:

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>

答案 1 :(得分:0)

将编译目标添加为依赖项

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

编译目标

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

答案 2 :(得分:0)

您需要在Ant脚本中添加<javac>任务。