无法从Ant build.xml运行groovyc

时间:2014-04-23 11:52:27

标签: eclipse ant groovy build.xml java-home

我正在尝试从Ant groovyc文件中运行build.xml。这是我的项目目录结构:

myapp/
    src/main/java
        <Some Java sources>
    src/main/groovy
        DemoController
    lib/main
        groovy-all-2.2.2.jar
        servlet-2.4.jar
    gen
        bin
        dist

这是我的compile目标:

<?xml version="1.0"?>
<project name="myapp" default="compile" basedir=".">
    <path id="groovy-path">
        <fileset dir="lib/main" includes="groovy-all-2.2.2.jar" />
    </path>

    <taskdef name="groovyc"
        classname="org.codehaus.groovy.ant.Groovyc"
        classpathref="groovy-path" />

    <!-- Other targets omitted for brevity... -->

    <target name="compile" depends="init">
        <echo message="Compiling Java sources..." />
        <javac destdir="gen/bin" includeantruntime="false">
            <src path="src/main/java" />
            <src path="lib/main" />
        </javac>

        <echo message="Compiling Groovy sources..." />
        <groovyc destdir="gen/bin">
            <src path="src/main/java" />
            <src path="src/main/groovy" />
            <src path="lib/main" />
        </groovyc>
    </target>
</project>

在我的代码中,在src/main/groovy下,我有一个名为DemoController的Groovy类:

package com.ar.myapp.controllers

import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

class DemoController extends HttpServlet {
    @Override
    void doGet(HttpServletRequest request, HttpServletResponse response) {
        String resp = "Hello, Groovy!";
        response.getWriter().write(resp);
    }
}

从Eclipse(我安装了Groovy插件并为所有这些代码创建了一个Groovy项目)我没有看到任何编译器错误或危险信号。但是当我运行Ant compile目标时,我得到以下异常:

compile:
    [echo] Compiling Java sources...
    [javac] Compiling 1 source file to /home/myuser/sandbox/eclipse/workspace/myapp/gen/bin
    [echo] Compiling Groovy sources...
    [groovyc] Compiling 1 source file to /home/myuser/sandbox/eclipse/workspace/myapp/gen/bin
    [groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    [groovyc] /home/myuser/sandbox/eclipse/workspace/myapp/src/main/groovy/com/myapp/controllers/DemoController.groovy: 3: unable to resolve class javax.servlet.http.HttpServlet
    [groovyc]  @ line 3, column 1.
    [groovyc]    import javax.servlet.http.HttpServlet
    [groovyc]    ^
    [groovyc] 
    [groovyc] /home/myuser/sandbox/eclipse/workspace/myapp/src/main/groovy/com/myapp/controllers/DemoController.groovy: 4: unable to resolve class javax.servlet.http.HttpServletRequest
    [groovyc]  @ line 4, column 1.
    [groovyc]    import javax.servlet.http.HttpServletRequest;
    [groovyc]    ^
    [groovyc] 
    [groovyc] /home/myuser/sandbox/eclipse/workspace/myapp/src/main/groovy/com/myapp/controllers/DemoController.groovy: 5: unable to resolve class javax.servlet.http.HttpServletResponse
    [groovyc]  @ line 5, column 1.
    [groovyc]    import javax.servlet.http.HttpServletResponse;
    [groovyc]    ^
    [groovyc] 
    [groovyc] 3 errors

当我打开lib/main/servlet-2.4.jar时,我确实看到Http*类打包应该放在哪里(javax.servlet.http下)。如果我没有lib/main/servlet-2.4.jar添加到groovyc的路径,我会理解这个错误,但这没有意义。这是怎么回事?

1 个答案:

答案 0 :(得分:3)

您需要将classpath属性添加到groovyc任务。

  <groovyc destdir="gen/bin">
    <src path="src/main/groovy" />
    <classpath>
      <path location="gen/bin"/>
      <fileset dir="lib/main">
          <include name="*.jar" />
      </fileset>
    </classpath>
  </groovyc>

请注意,原始build.xml包含javac和groovyc任务中的Java源代码。您可以1)将javac目标(即gen / bin)添加到groovyc任务的类路径而不是引用源或2)在groovyc任务中一起编译java和groovy源并完全删除javac步骤。

选项2:删除&#34; javac&#34;任务和使用结合groovy任务与java + groovy源:

  <groovyc destdir="gen/bin">
    <src path="src/main/java" />
    <src path="src/main/groovy" />
    <classpath>
      <fileset dir="lib/main">
          <include name="*.jar" />
      </fileset>
    </classpath>
  </groovyc>