5

我尝试使用 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命令,我会看到以下错误消息:

[调试] 执行命令行: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

4 回答 4

4

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

于 2014-01-06T12:12:33.930 回答
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>
于 2014-01-06T12:34:47.953 回答
0

我不确定这是否可行,但也许您可以使用以下方法重试:

<commandlineArgs> 

反而。我遇到了与它相关的这个 JIRA 问题,这听起来像是你需要的。

于 2014-01-06T18:38:35.827 回答
0

正如前面的答案中提到的,Maven 不能很好地处理通配符。您应该遵循“maven way”并一一传递它们,或者您可以尝试使用“ commandlineArgs ”。

于 2014-01-06T12:45:06.813 回答