我正试图从Ant build.xml
文件内部运行groovyc
。这是我的项目目录结构:
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
的路径中,我会理解这个错误,但这没有意义。这是怎么回事?
您需要将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:删除"javac"任务,并将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>