开发者

How do I call a ruby script as part of the Maven build process?

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 IOEx开发者_JS百科ception {        
        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...


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜