使用ant编译java代码



我对使用ant有点陌生,目前,我制作ant脚本的方式是通过eclipse自动生成它们,以生成可运行的jar。这样做的问题是,它只读取bin目录。因此,如果我要更改java src文件,我不会看到在ant构建中复制的更改。我需要向我的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>

我试着阅读了一些文档,但它有点令人困惑。

您需要使用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>

将编译目标添加为依赖项

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

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

相关内容

  • 没有找到相关文章

最新更新