2

编辑2:我发现了问题。<id>快速的答案是我新配置的执行缺少一个导致问题的原因。我会把这个问题留在这里,以防它帮助别人。

我有一个 ruby​​ 脚本,它生成我的一些 jUnit 源文件。

我正在尝试使用exec-maven-plugin在默认生命周期的 generate-sources 阶段调用此 ruby​​ 脚本。这是我添加到我的 POM 中以实现此目的的内容:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <executable>ruby</executable>
            <workingDirectory>supporting_files/ruby</workingDirectory>
            <arguments>
                <argument>CreateUnitTests.rb</argument>
            </arguments>
        </configuration>
    </plugin>          

当我在netbeans()中执行“清理和构建主项目”时,这似乎有效clean install,但是当我运行项目时(process-classes org.codehaus.mojo:exec-maven-plugin:1.1.1:exec使用属性:)

exec.classpathScope=runtime
exec.args=-enableassertions -classpath %classpath org.example.MyProject.App
exec.executable=java

运行失败,因为它试图ruby用作 exec.executable(正如我在 POM 中告诉它的那样)。

那么,我如何ruby临时使用(ruby supporting_files/ruby/CreateUnitTests.rb在运行 jUnit 测试之前运行),但在java其他情况下使用? 在 generate-test-sources 阶段调用脚本的“正确”方式是什么?

编辑:问题似乎不仅仅是改变正在调用的可执行文件......

我编写了一个快速的 java 程序,它只调用 ruby​​ 解释器,将它作为命令行参数接收(ruby 文件名)传递:

import java.io.IOException;

public class RunRuby {
    public static void main(String args[]) throws IOException {        
        Runtime run = Runtime.getRuntime();
        run.exec("ruby "+args[0]);
    }
}

这使我可以避免更改 POM 中的可执行文件:

    <plugin>
        <!-- use ruby to generate some jUnit tests -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
            <execution>
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <executable>java</executable>
            <workingDirectory>supporting_files/ruby</workingDirectory>
            <arguments>
                <argument>RunRuby</argument>                    
                <argument>CreateUnitTests.rb</argument>
            </arguments>
        </configuration>
    </plugin>          

丑陋,我知道。但无论如何,清理/构建仍然可以按预期工作,但“运行”仍然失败!这是错误消息:

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.1.1:exec (default-cli) on project MyProject: Result of cmd.exe /X /C "java -enableassertions -classpath C:\Dropbox\dev\java\MyProject\target\classes;C:\Users\username\.m2\repository\LOTS\OF\JARS org.example.MyProject.App" execution is: '-1'. -> [Help 1]

所以,它恢复运行java,但仍然失败。我注意到的一件奇怪的事情是它正在执行目标org.codehaus.mojo:exec-maven-plugin:1.1.1:exec,即使在 POM 中我告诉它使用版本1.2......

4

1 回答 1

1

缺少一个<id>导致我的自定义执行成为默认值。这是修复:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
            <execution>
                <!-- use ruby to generate some jUnit tests during generate-test-sources -->
                <id>generate-test-sources</id>
                <configuration>
                    <executable>ruby</executable>
                    <workingDirectory>supporting_files/ruby</workingDirectory>
                    <arguments>
                        <argument>CreateUnitTests.rb</argument>
                    </arguments>
                </configuration>                    
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
    </plugin>          
于 2011-08-12T15:22:10.700 回答