如何在 Maven 构建过程中调用 ruby​​ 脚本?

发布于 2024-11-28 16:07:56 字数 3865 浏览 1 评论 0原文

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

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

我正在尝试使用 exec-maven-plugin 在 默认生命周期。以下是我在 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 测试之前运行 rubysupporting_files/ruby/CreateUnitTests.rb),但使用 java 否则?在生成测试源阶段调用脚本的“正确”方法是什么

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

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

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

edit 2: I found the problem. The quick answer is that the lack of an <id> for my newly configured execution was causing the problem. I'll leave the question here in case it helps someone else.

I have a ruby script which generates some of my jUnit source files.

I am trying to use the exec-maven-plugin to call this ruby script during the generate-sources phase of the default lifecycle. Here's what I've added to my POM to achieve this:

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

This seems to be working when I'm doing a "Clean and Build Main Project" in netbeans (clean install), but when I run the project (process-classes org.codehaus.mojo:exec-maven-plugin:1.1.1:exec with the properties:)

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

The run fails, because it tries to use ruby as the exec.executable (as I tell it to in the POM).

So, how do I use ruby temporarily (to run ruby supporting_files/ruby/CreateUnitTests.rb before running jUnit tests), but use java otherwise? What's the "proper" way to call scripts during the generate-test-sources phase?

edit: the problem seems to be more than just changing which executable is being called...

I wrote a quick java program which just calls the ruby interpreter, passing it (ruby filename) it received as a command line argument:

import java.io.IOException;

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

which allowed me to avoid changing the executable in my 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>          

Ugly, I know. But anyway, a clean/build still works as expected, but the "run" is still failing! Here's the error message:

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]

So, it's back to running java, but still failing. One odd thing I'm noticing is that it's executing the goal org.codehaus.mojo:exec-maven-plugin:1.1.1:exec even though in the POM I'm telling it to use version 1.2...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卖梦商人 2024-12-05 16:07:57

缺少 导致我的自定义执行成为默认执行。这是修复方法:

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

The lack of an <id> caused my customized execution to become the default. Here's the fix:

    <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>          
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文