java 使用ant编译java代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15978903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:32:07  来源:igfitidea点击:

Using ant to compile java code

javaant

提问by de1337ed

I'm a little new to using ant, and currently, the way I make ant scripts is by auto-generating them through eclipse in order to produce runnable jar's. The problem with this is that it only reads the bin directory. As a result, If I were to change a java src file, I wouldn't see the changes replicated in the ant build. What do I need to add to my ant script? I've shown an example script below:

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

I tried reading some of the documentation but it was a little confusing.

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

回答by Diego Nemo

You need to compile your sources with javacant's task

您需要使用javacant 的任务来编译您的源代码

Suppose your project structure is:

假设你的项目结构是:

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>

回答by NullPointerException

Add your compile target as dependency

将您的编译目标添加为依赖项

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

Compile target

编译目标

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

回答by whiskeyspider

You need to add a <javac>task to your Ant script.

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