exec-maven-plugin无法找到或加载主类,但输出在命令行上运行正常

时间:2014-01-06 12:02:13

标签: java xml maven exec-maven-plugin

我尝试使用exec-maven-plugin来运行Java程序。

我使用以下pom片段:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
             <configuration>
                    <executable>java</executable>
                        <arguments>
                         <argument>-Dmyproperty=myvalue</argument>
                            <argument>-cp</argument>
                            <argument>"/home/vogella/libs/*"</argument>
                            <argument>com.vogella.test.Main</argument>
                        </arguments>
    </configuration>


</plugin>

com.vogella.test.Main包含在位于/ home / vogella / libs / *中的一个jar文件中。如果我运行mvn -X clean install exec:exec命令,我会看到以下错误消息:

  

[DEBUG]执行命令行:java -Dmyproperty = myvalue -cp   “/ home / vogella / libs / *”com.vogella.test.Main       错误:无法找到或加载主类com.vogella.test.Main

如果我在从中启动Maven构建的shell中复制命令行(java -Dmyproperty=myvalue -cp "/home/vogella/libs/*" com.vogella.test.Main),那么Java程序将正确执行。

知道我的Maven设置有什么问题吗?

4 个答案:

答案 0 :(得分:4)

使用CLI,/ home / vogella / libs / *表达式通过bash扩展并解析为文件列表。使用Maven,表达式直接执行而不是扩展。所以它仍然是&#34; / home / vogella / libs / *&#34;这不是一个有效的jar文件。 通过使用antrun插件并在脚本中使用java Ant任务,您可能会获得更多成功。 Ant比其他任何东西都更了解通配符。

答案 1 :(得分:1)

您需要通过依赖项设置类路径。使用命令行参数-cp可以显式设置类路径,但这对maven cp不起作用。这是通过依赖关系构建的。

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includeProjectDependencies>false</includeProjectDependencies>
                <includePluginDependencies>true</includePluginDependencies>
                <mainClass>org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher</mainClass>
                <arguments>
                    <argument>${project.basedir}/src/my/mavenized/GenerateHeroLanguage.mwe2</argument>
                </arguments>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.xtext</groupId>
                    <artifactId>org.eclipse.xtext.xtext</artifactId>
                    <version>2.5.0-SNAPSHOT</version>
                </dependency>
                <dependency>
                    <groupId>org.eclipse.xtext</groupId>
                    <artifactId>org.eclipse.xtext.xbase</artifactId>
                    <version>2.5.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>

答案 2 :(得分:0)

如前面的答案中所提到的,Maven并不能很好地处理外卡。您应该按照“maven方式”逐个传递它们,或者您可以尝试使用“commandlineArgs”。

答案 3 :(得分:0)

我不确定这是否有效,但也许你可以再试一次使用:

<commandlineArgs> 

代替。我遇到了与此相关的JIRA问题,听起来就像你需要的那样。