3

我对使用 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>

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

4

3 回答 3

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>
于 2013-04-12T19:16:02.453 回答
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>  
于 2013-04-12T19:03:34.307 回答
0

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

于 2013-04-12T19:03:38.227 回答