Reputation: 1611
I am trying to run groovyc
from inside an Ant build.xml
file. Here's my project directory structure:
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
Here's my compile
target:
<?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>
In my code, under the src/main/groovy
, I have a Groovy class called DemoController
:
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);
}
}
From Eclipse (I installed the Groovy plugin and created a Groovy project for all this code) I don't see any compiler errors or red flags. But when I run the Ant compile
target, I get the following exception:
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
When I open up lib/main/servlet-2.4.jar
I do in fact see the Http*
classes packaged where they ought to be (under javax.servlet.http
). I would understand this error if I didn't have lib/main/servlet-2.4.jar
added to groovyc
's path, but this just doesn't make sense. What is going on here?
Upvotes: 0
Views: 4834
Reputation: 23738
You need to add the classpath
attribute to the groovyc task.
<groovyc destdir="gen/bin">
<src path="src/main/groovy" />
<classpath>
<path location="gen/bin"/>
<fileset dir="lib/main">
<include name="*.jar" />
</fileset>
</classpath>
</groovyc>
Note the original build.xml includes the Java source in both the javac and groovyc tasks. You can either 1) add the javac target (i.e. gen/bin) to classpath of groovyc task rather than reference the source or 2) compile the java and groovy source together in groovyc task and remove the javac step entirely.
Option 2: Remove "javac" task and use combined groovy task with java + groovy source:
<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>
Upvotes: 3