【问题标题】:How do I call a ruby script as part of the Maven build process?如何在 Maven 构建过程中调用 ruby​​ 脚本?
【发布时间】:2011-10-24 07:05:03
【问题描述】:

编辑 2:我发现了问题。快速回答是我新配置的执行缺少<id> 导致了问题。我会把问题留在这里,以防对其他人有帮助。

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

我正在尝试使用exec-maven-plugindefault lifecycle 的生成源阶段调用此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(在运行jUnit测试之前运行ruby supporting_files/ruby/CreateUnitTests.rb),否则使用java 什么是“正确”的调用方式生成测试源阶段的脚本

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

我编写了一个快速的 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...

【问题讨论】:

  • 似乎问题不只是 exec.executable,我将编辑我的问题..

标签: java unit-testing build-process maven-3 exec-maven-plugin


【解决方案1】:

缺少&lt;id&gt; 导致我的自定义执行成为默认执行。这是修复:

    <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>          

【讨论】:

  • 感谢 Drews,为我工作得很好!我想要一些东西来运行我的 rspec 测试作为构建的一部分,如果它们不运行,它们就会失败。
猜你喜欢
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多