Ant在Eclipse中没有像我期望的那样工作

时间:2013-09-20 15:36:12

标签: java eclipse ant

我是ANT的新手并且使用它,我只是在Eclipse中创建了一个新的java项目,只需在屏幕上打印欢迎这个词。我使用Eclipse运行程序,并在屏幕上成功打印了“欢迎”。这是我的程序

public class welcome {
    public static void main(String[] args) 
    {
        System.out.println("Welcome!!!");
    }
}

然后我按照通常的方式使用Eclipse构建ANT文件,因此我使用了Export功能并选择了ANT构建文件。 这是我的构建文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. --><project basedir="." default="build" name="test">
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="../../../../usr/lib/eclipse"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.7"/>
    <property name="source" value="1.7"/>
    <path id="test.classpath">
        <pathelement location="bin"/>
    </path>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="test.classpath"/>
        </javac>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
    <target name="welcome">
        <java classname="welcome" failonerror="true" fork="yes">
            <classpath refid="test.classpath"/>
        </java>
    </target>
</project>

当我将程序作为Ant运行时,它只给我一条构建成功的消息!没有在屏幕上打印“欢迎”字样! 这是输出

> Buildfile: /home/name/workspace/test/build.xml
> build-subprojects: init: build-project:
>      [echo] test: /home/name/workspace/test/build.xml build: BUILD SUCCESSFUL Total time: 376 milliseconds

2 个答案:

答案 0 :(得分:1)

ant文件的默认目标是“build”,它将创建项目的jar文件(稍后可以执行)。如果要通过ant运行项目,请将目标更改为“welcome”(请参阅​​ant文件的末尾)。那应该按照你的预期执行程序。

答案 1 :(得分:0)

  

Apache Ant是一个Java库和命令行工具,其任务是   驱动构建文件中描述的进程作为目标和扩展   点数相互依赖。 Ant的主要已知用法是   构建Java应用程序

类名首字母也应该是CAPITALIZED ......

或多或少这就是你的蚂蚁应该是这样的样子,但是我仍然不明白你为什么要在这里尝试使用ANT。

<project name="Welcome" basedir="." default="main">

<property name="src.dir"     value="src"/>

<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>

<property name="main-class"  value="(packagename).(class name)"/>



<target name="clean">
    <delete dir="${build.dir}"/>
</target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

<target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>