Groovy failed to resolve java class

I am having a problem running groovy from ant file.

In Eclipse using the launcher everything works fine, but I run the ant file, I got the following output:

Main.groovy: 71: Unable to resolve class InitializeDatabase
[groovyc] @ line 71, column 40. [groovyc] java.lang.Object javaClassInstance = new InitializeDatabase ()
[groovyc]

[groovyc] 1 error

InitializeDatabase is a java class in the same package.

public class InitializeDatabase {

    public void test() {
        System.out.println("Hello Groovy");
    }
}

      

I think the problem is in the ant file:

<project name="tp" basedir="." default="dbsetup">
    <target name="dbsetup">
        <taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
            <classpath>
                <fileset dir="../files/lib/default" includes="*.jar" />
            </classpath>
        </taskdef>

        <delete dir="bin" />
        <mkdir dir="bin" />
        <groovyc srcdir="src" destdir="bin" />

        <java classname="groovy.ui.GroovyMain" dir="../.." fork="true" failonerror="true">
            <classpath>
                <fileset dir="../files/lib/default" includes="*.jar"/>
                <pathelement location="bin"/>
            </classpath>
            <arg line="build/scripts/src/build/Main.groovy" />
        </java>
    </target>

</project>

      

Can anyone help me?

+3


source to share


2 answers


You need to include the task javac

in yours groovyc

. Change:

    <groovyc srcdir="src" destdir="bin" />

      

to

    <groovyc srcdir="src" destdir="build">
        <javac/>
    </groovyc>

      



And it should work fine. As says here :

Co-compilation means that Groovy compilation will parse Groovy source files, create stubs for all of them, call the Java compiler to compile stubs along with Java sources, and then continue compiling in the usual Groovy compiler way. This allows mixing Java and Groovy files without restriction.

...

The correct way to work is, of course, to use a nested tag and all attributes and additional nested tags as needed.

+2


source


Here is the final file that works great. Thanks tim_yates!



<target name="dbsetup">

        <taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
            <classpath>
                <fileset dir="../files/lib/default" includes="*.jar" />
            </classpath>
        </taskdef>

        <delete dir="bin" />
        <mkdir dir="bin" />
        <groovyc srcdir="src" destdir="bin">
            <javac source="1.6" target="1.6" debug="on" />
        </groovyc>

        <java classname="groovy.ui.GroovyMain" dir="../.." fork="true" failonerror="true">
            <classpath>
                <fileset dir="../files/lib/default" includes="*.jar"/>
                <pathelement location="bin"/>
            </classpath>
            <arg line="build/scripts/src/build/access/AccessDbSetup.groovy" />
        </java>
    </target>

      

0


source







All Articles