【问题标题】:Exception in using maven-antrun-plugin for Groovy compilation使用 maven-antrun-plugin 进行 Groovy 编译时出现异常
【发布时间】:2014-06-02 23:33:01
【问题描述】:

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

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

【问题讨论】:

  • 很难帮助 - 需要设置整个混乱。任何最小的工作示例?会更容易尝试。
  • 我试试,有没有使用Maven进行Groovy编译的例子?
  • 不知道有没有真正好的支持。但是,前段时间读过它。也许切换到 gradle?
  • 不知怎的,我让它工作了
  • 怎么样?真的很有趣。

标签: maven groovy ant elasticpath


【解决方案1】:

首先,Groovy JSR jar 中的编译类非常古老。我不建议将其用作定义 Groovyc 类的依赖项。这很可能是您的问题的原因。我不确定你想做什么。如果你想通过 JSR223 调用,编译没有意义。如果你正在编译,使用 JSR 依赖没有任何意义。

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

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

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多