views:

1140

answers:

2

Hello everyone,

I want to develop a web application (no frameworks) mixing java with groovy. I am using the IDE Netbeans with the plugin.

If I start a new Java SE project and add a groovy class, it works with no problems.. but when I create a new java EE project and add a groovy class it can't compile and shows me the following error:

/home/webcodei/NetBeansProjects/testeGroovyWeb/src/java/pacote/Hello.java:23: cannot find symbol 

symbol  : class Hroovy
location: class pacote.Hello
            Hroovy h = new Hroovy();
/home/webcodei/NetBeansProjects/testeGroovyWeb/src/java/pacote/Hello.java:23: cannot find symbol
symbol  : class Hroovy
location: class pacote.Hello
            Hroovy h = new Hroovy();
2 errors
/home/webcodei/NetBeansProjects/testeGroovyWeb/nbproject/build-impl.xml:383: The following error         occurred while executing this line:
/home/webcodei/NetBeansProjects/testeGroovyWeb/nbproject/build-impl.xml:211: Compile failed; see     the compiler error output for details.
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)

Does anybody have a clue of how do I enable Java EE + Groovy in netbeans?

ps: I know the existence of Grails

ps2: The Groovy jar is in my classpath.

Thank you for all!

+4  A: 

It appears that the NetBeans 6.5 Java Webapp project manager does not have the "Enable Groovy" support that is present in the Java App and Java Class library projects.

I can think of two ways you might get around this:

First, you could put your Groovy code and tests in a separate project as a Java Class Library. Then make the Java webapp dependent on the Groovy project. NetBeans will build the dependent project automatically so you'll hardly notice they are in separate projects.

Second, the "Enable Groovy" isn't magic. All it does is write a groovy-build.xml in /nbprojects and modify build-impl.xml to import it. The groovy-build.xml overrides the default "javac" macro to invoke "groovyc" instead. If you're at all handy with Ant, you could copy a groovy-build.xml from a Java Application project and copy it to your Java Web project and then import it from your build.xml (before build-impl.xml is imported). The groovy-build.xml would likely need a few tweaks as some of the properties between a webapp and class library are a little different.

Dave Smith
+2  A: 

@Dave Smith,

This was exactly what I did. I created one javase project and one webapp and started to compare them. After a few minutes I realised that the only diference was the groovy-build.xml.

So I copied the groovy-build.xml into the dir, and inserted the following lines into my build.xml:

<import file="nbproject/groovy-build.xml"/>

Right before the regular

<import file="nbproject/build-impl.xml"/>

And then called the groovy file to overwrite the -init-macrodef-javac.

<target depends="-groovy-init-macrodef-javac" name="-pre-compile">

</target>

I also needed to change the namespace from the groovy-build.xml to mine ex:

<macrodef name="javac" uri="http://www.netbeans.org/ns/web-project/2"&gt;

And inserted the j2ee classpath (${j2ee.platform.classpath}) to the attribute a few lines later:

<attribute default="${javac.classpath}:${j2ee.platform.classpath}" name="classpath"/>

After that the project worked successfully! =D

Thank you for all!

José Leal