0

我能够在 ant 上构建 java 程序,并且已经正确配置。但是我想知道如何使用 Ant 在控制台上看到 System.out.println / System.out.print ?

注意:我没有使用过蚂蚁,我正在尝试探索。我希望我的问题不是愚蠢的?

简单的程序

    package antTesting;
    public class Helloworld
    {

        /**
         * @param args
         */
        public static void main( String[] args )
        {
            System.out.println( " hello there !!!! " );

        }

    }

build.xml 相同

<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
  <!-- Sets variables which can later be used. -->
  <!-- The value of a property is accessed via ${} -->
  <property name="src.dir" location="src" />
  <property name="build.dir" location="build" />
  <property name="dist.dir" location="dist" />
  <property name="docs.dir" location="docs" />

  <!-- Deletes the existing build, docs and dist directory-->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <!-- Creates the  build, docs and dist directory-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

  <!-- Compiles the java code (including the usage of library for JUnit -->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
    </javac>

  </target>

  <!-- Creates Javadoc -->
  <target name="docs" depends="compile">
    <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
      <!-- Define which files / directory should get included, we include all -->
       <fileset dir="${src.dir}">
                <include name="**" />
           </fileset>
    </javadoc>
  </target>

  <!--Creates the deployable jar file  -->
  <target name="jar" depends="compile">
    <jar destfile="${dist.dir}\de.vogella.build.test.ant.jar" basedir="${build.dir}">
      <manifest>
        <attribute name="Main-Class" value="test.Main" />
      </manifest>
    </jar>
  </target>

  <target name="main" depends="compile, jar, docs">
    <description>Main target</description>
  </target>

</project> 

当我run > ant

在 CMD 上

Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:C:\PROGRA~2\HP\Sprinter\
bin\JAVA_S~1\classes;C:\PROGRA~2\HP\Sprinter\bin\JAVA_S~1\classes\jasmine.jar
Buildfile: C:\Users\ms025226\workspace\TestingWorld\build.xml

clean:
   [delete] Deleting directory C:\Users\ms025226\workspace\TestingWorld\build
   [delete] Deleting directory C:\Users\ms025226\workspace\TestingWorld\docs
   [delete] Deleting directory C:\Users\ms025226\workspace\TestingWorld\dist

makedir:
    [mkdir] Created dir: C:\Users\ms025226\workspace\TestingWorld\build
    [mkdir] Created dir: C:\Users\ms025226\workspace\TestingWorld\docs
    [mkdir] Created dir: C:\Users\ms025226\workspace\TestingWorld\dist

compile:
    [javac] C:\Users\ms025226\workspace\TestingWorld\build.xml:26: warning: 'inc
ludeantruntime' was not set, defaulting to build.sysclasspath=last; set to false
 for repeatable builds
    [javac] Compiling 1 source file to C:\Users\ms025226\workspace\TestingWorld\
build

jar:
      [jar] Building jar: C:\Users\ms025226\workspace\TestingWorld\dist\de.vogel
la.build.test.ant.jar

docs:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] Loading source file C:\Users\ms025226\workspace\TestingWorld\src\Hel
loworld.java...
  [javadoc] Constructing Javadoc information...
  [javadoc] Standard Doclet version 1.6.0_31
  [javadoc] Building tree for all the packages and classes...
  [javadoc] Building index for all the packages and classes...
  [javadoc] Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
  [javadoc] Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:C:\PROGRA~2\
HP\Sprinter\bin\JAVA_S~1\classes;C:\PROGRA~2\HP\Sprinter\bin\JAVA_S~1\classes\ja
smine.jar
  [javadoc] Building index for all classes...

main:

BUILD SUCCESSFUL
Total time: 2 seconds

当我为类路径定义目标时,我收到一个错误

<!-- to printoutputs -->    
  <target name="Helloworld" description="Hello world">
        <java classname="antTesting.Helloworld">
            <arg value="arg1"/>
            <arg value="arg2"/>         
        </java>
 </target>

  <target name="main" depends="compile, jar, docs, Helloworld">
    <description>Main target</description>
  </target>

错误:

Helloworld:
     [java] Could not find antTesting.Helloworld. Make sure you have it in your classpath
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:138)
     [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
     [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
     [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     [java]     at java.lang.reflect.Method.invoke(Method.java:597)
     [java]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
     [java]     at org.apache.tools.ant.Task.perform(Task.java:348)
     [java]     at org.apache.tools.ant.Target.execute(Target.java:357)
     [java]     at org.apache.tools.ant.Target.performTasks(Target.java:385)
     [java]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
     [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
     [java]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
     [java]     at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
     [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
     [java]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423)
     [java]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137)
     [java] Java Result: -1
4

2 回答 2

2

如果你想从 Ant 执行你的类,你没有在任何地方指定它,在你的脚本中添加这样的东西:

<target name="Helloworld" description="Hello world">
    <java classname="your.package.Helloworld">
        <arg value="arg1"/>
        <arg value="arg2"/>
        ...
    </java>
</target>

元素<arg value="..."/>被传递给您的主要方法数组参数:main( String[] args )

现在将目标Helloworld作为依赖项添加到其他目标或从运行选项中选择它。

于 2012-08-24T16:40:24.833 回答
0

您需要 System.out 还是使用日志记录(Apache Commons、SLF4J...)更有趣?

于 2014-03-16T14:31:34.267 回答