使用maven-antrun-plugin进行Groovy编译时出现异常

时间:2014-06-02 23:33:02

标签: maven groovy ant elasticpath

我有一堆Groovy文件需要与Java类一起包含在jar文件中。首先,我为Groovy编译创建了单独的Maven xml文件。结果是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

1 个答案:

答案 0 :(得分:1)

首先,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

编辑:我还更新了您可能从(http://docs.codehaus.org/display/GROOVY/Compiling+With+Maven2)得到的过时维基页面。