使用maven- anrun -plugin进行Groovy编译时的异常



我有一堆Groovy文件,我需要将它们与Java类一起包含到jar文件中。开始时,我创建了单独的Maven xml文件,仅用于Groovy编译。结果是Zip Exception。

Maven XML脚本如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycomp.MyGroovy</groupId>
<artifactId>MyGroovy</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>GroovyBuild</name>
<dependencies>
    <dependency>
        <groupId>groovy</groupId>
        <artifactId>groovy-all-1.0-jsr</artifactId>
        <version>05</version>
    </dependency>
</dependencies>
<build>
   <sourceDirectory>WEB-INF/src/main/groovy</sourceDirectory>
    <outputDirectory>WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <configuration>
                        <tasks>
                            <taskdef name="groovyc"
                                classname="org.codehaus.groovy.ant.Groovyc">
                                <classpath refid="maven.compile.classpath"/>
                            </taskdef>
                            <mkdir dir="${basedir}/target/WEB-INF/classes"/>
                            <groovyc destdir="${basedir}/target/WEB-INF/classes"
                                srcdir="${basedir}/WEB-INF/src/main/groovy" listfiles="true">
                                <classpath refid="maven.compile.classpath"/>
                            </groovyc>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Maven调试日志中的一些摘录:

[groovyc] Compiling 5 source files to /usr/java/ep622/com.elasticpath.core/target/WEB-INF/classes
[groovyc] /usr/java/ep622/com.elasticpath.core/WEB-INF/src/main/groovy/com/elasticpath/tags/builder/BuilderString.groovy
[groovyc] /usr/java/ep622/com.elasticpath.core/WEB-INF/src/main/groovy/com/elasticpath/tags/builder/LogicalTreeBuilder.groovy
[groovyc] /usr/java/ep622/com.elasticpath.core/WEB-INF/src/main/groovy/com/elasticpath/tags/engine/LogicalOperator.groovy
[groovyc] /usr/java/ep622/com.elasticpath.core/WEB-INF/src/main/groovy/com/elasticpath/tags/engine/MapRunner.groovy
[groovyc] /usr/java/ep622/com.elasticpath.core/WEB-INF/src/main/groovy/com/elasticpath/tags/engine/OperatorDelegate.groovy
Finding class org.codehaus.groovy.control.CompilationUnit
Loaded from /Users/anarinsky/.m2/repository/groovy/groovy-all-1.0-jsr/05/groovy-all-1.0-jsr-05.jar org/codehaus/groovy/control/CompilationUnit.class
 --------
 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run (compile) on project MyGroovy: An Ant BuildException has occured: java.util.zip.ZipException: error in opening zip file -> [Help 1]
 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal   org.apache.maven.plugins:maven-antrun-plugin:1.3:run (compile) on project MyGroovy: An Ant BuildException has occured: java.util.zip.ZipException: error in opening zip file
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)

Caused by: org.apache.maven.plugin.MojoExecutionException: An Ant BuildException has occured: java.util.zip.ZipException: error in opening zip file
at org.apache.maven.plugin.antrun.AbstractAntMojo.executeTasks(AbstractAntMojo.java:131)
at org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:98)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 19 more
Caused by: java.util.zip.ZipException: error in opening zip file
at org.codehaus.groovy.ant.Groovyc.compile(Groovyc.java:480)
at org.codehaus.groovy.ant.Groovyc.execute(Groovyc.java:376)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.maven.plugin.antrun.AbstractAntMojo.executeTasks(AbstractAntMojo.java:118)
... 22 more
Caused by: java.util.zip.ZipException: error in opening zip file
at org.apache.tools.ant.AntClassLoader.addPathElement(AntClassLoader.java:460)
at org.codehaus.groovy.ant.Groovyc.buildClassLoaderFor(Groovyc.java:511)
at org.codehaus.groovy.ant.Groovyc.compile(Groovyc.java:469)
... 32 more

首先,Groovy JSR jar中的编译类非常古老。我不建议使用它作为您定义Groovyc类的依赖项。这很可能是你的问题的原因。我不知道你到底想干什么。如果您想通过JSR223调用,那么编译就没有意义了。如果你正在编译,使用JSR依赖没有意义。

。下面是一个完整的示例,包括使用最新Groovy版本的测试类路径:https://gist.github.com/keeganwitt/d56b4b81165a264061d5。如果您正在使用Maven,我强烈建议您考虑使用GMavenPlus(如果您愿意,也可以使用Maven的Groovy-Eclipse编译器插件)。我做了一个比较,试图帮助人们在这里选择:http://docs.codehaus.org/display/GMAVENPLUS/Choosing+Your+Build+Tool.

编辑:我也更新了过时的wiki页面,你可能从(http://docs.codehaus.org/display/GROOVY/Compiling+With+Maven2)得到这个。

最新更新