Ant 在 Eclipse 中不能像我预期的那样工作



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

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

然后我只是按照通常的方法使用 Eclipse 构建 ANT 文件,所以我使用了导出功能并选择了 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

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

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

此外,类名的第一个字母应该大写...

或多或少这就是你的蚂蚁应该是什么样子,但我仍然不明白你为什么要在这里使用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"/>

相关内容

  • 没有找到相关文章

最新更新