首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在maven中运行groovy脚本时显示错误,而在IDE中运行

在maven中运行groovy脚本时显示错误,而在IDE中运行
EN

Stack Overflow用户
提问于 2016-03-30 09:05:05
回答 1查看 6.2K关注 0票数 3

这是一个maven项目。要构建和运行groovy脚本,需要使用"gmavenplus“插件。当我在maven中运行脚本时,即使我应用了-X选项,错误如下所示:

代码语言:javascript
复制
mvn -X clean install gplus:execute

错误未能在项目org.codehaus.gmavenplus:gmavenplus-plugin:1.5:execute上执行目标users_rest_updater:错误发生在从类路径调用一个Groovy类的方法时。org.apache.maven.lifecycle.LifecycleExecutionException::没有这样的属性: args for class: Script1 -> Help 1 InvocationTargetException未能执行项目users_rest_updater: Error上的目标org.codehaus.gmavenplus:gmavenplus-plugin:1.5:execute (默认-cli),因为从类路径调用一个Groovy类的方法时发生了错误。在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) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)在org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106) at org.apache.maven.cli.MavenCli在org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286) at org.apache.maven.cli.MavenCli.main(MavenCli.java:197) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)在org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)中,在从类路径调用Groovy类的方法时发生了org.apache.maven.plugin.MojoExecutionException:错误。在org.codehaus.gmavenplus.mojo.ExecuteMojo.doExecute(ExecuteMojo.java:128) at org.codehaus.gmavenplus.mojo.ExecuteMojo.execute(ExecuteMojo.java:83) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208) . 20多个原因是: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.gmavenplus.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:118) at org.codehaus.gmavenplus.mojo.ExecuteMojo.executeScripts(ExecuteMojo.java:195) at org.codehaus.gmavenplus.mojo.ExecuteMojo.doExecute(ExecuteMojo.java:124)23更多原因是: org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53):org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307) at Script1.run(Script1.groovy:4),在groovy.lang.GroovyShell上运行(Script1.groovy:4)。groovy.lang.GroovyShell.evaluate(GroovyShell.java:623) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)的评估(GroovyShell.java:585)

我从IDE (Intellij )运行相同的脚本,但是在控制台中它写的是完全错误的,在我的例子中是:

捕获: java.lang.NoClassDefFoundError: org/apache/ Caused /cli/ParseException java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException at MainApp.run(MainApp.groovy:3),由: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException .1引起

当前pom文件:

代码语言:javascript
复制
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.6</version>
            <!--<scope>runtime</scope>-->
        </dependency>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.16.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.16.Final</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>3.0.16.Final</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson2-provider</artifactId>
            <version>3.0.16.Final</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>test</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addSources</goal>
                            <goal>addTestSources</goal>
                            <goal>compile</goal>
                            <goal>testGenerateStubs</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scripts>
                        <script>file:///${project.basedir}/src/main/groovy/MainApp.groovy</script>
                    </scripts>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.4.6</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>MainApp</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

我怎样才能强迫maven显示准确的错误?没有它,我就找不到办法找出出了什么问题。对于java代码,maven总是工作得很好。你总能看到问题的主要原因。可能是gmavenplus maven插件的问题,我不确定。

EN

回答 1

Stack Overflow用户

发布于 2018-10-18 00:46:36

如果您只是在IntelliJ中以Groovy脚本的形式运行该脚本,它将不知道您的任何项目依赖关系。GMavenPlus在用于执行脚本的类路径中包含了Maven依赖项。要作为独立脚本运行,您必须添加葡萄所需的依赖项。注意,在GMavenPlus执行的脚本中使用葡萄也是有效的。

如果您试图读取从命令行传递的参数,参数将由Maven使用,GMavenPlus将没有机会读取它们。如果您需要将脚本作为独立的或者通过Maven调用,那么您应该为Maven的使用设置属性,并在解析args之前签入脚本。

如果args给您带来麻烦,您可以尝试this.args,它们是等价的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36304224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档