Maven and Exec: forking a process? - maven

I'm trying to use Maven to start an application prior to running some integration tests on it. I'm on Windows. My Maven plugin configuration looks like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>start-my-application</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>start_application.bat</executable>
<workingDirectory>./path/to/application</workingDirectory>
</configuration>
</execution>
<executions>
<plugin>
and my batch file looks like this:
start myApplication.exe
When run in isolation, the batch file spawns a separate window to run the application and immediately returns control.
However, when run from Maven, the build waits for the process in the separate window to finish before continuing. This somewhat defeats the point of the integration testing phase...
Any ideas how I can start a truly separate process in Maven to allow the build to continue alongside it?

For the record, a rather hackish solution is to use maven-antrun-plugin to call Ant, which is capable of spawning separate processes:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<configuration>
<target>
<exec executable="cmd"
dir="./path/to/application"
spawn="true">
<arg value="/c"/>
<arg value="start_application.bat"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Try this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>start-my-application</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>call</executable>
<arguments>
<argument>start_application.bat</argument>
</arguments>
<workingDirectory>./path/to/application</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>

Related

Execute multiple goals with parameters in maven

I am trying to execute multiple goals in maven
I have my Pom.xml like
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
<configuration>
<testFilesIncluded>
<jMeterTestFile>${mytest}</jMeterTestFile>
</testFilesIncluded>
<propertiesUser>
<hostName>${myhost}</hostName>
<port>${myport}</port>
<protocol>${myprotocol}</protocol>
</propertiesUser>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>de.codecentric</groupId>
<artifactId>jmeter-graph-maven-plugin</artifactId>
<version>0.1.0</version>
<executions>
<execution>
<id>create-graphs</id>
<goals>
<goal>create-graph</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>runcommand</id>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>**com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx**</argument>
<argument>de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph#create-graphs</argument>
</arguments>
</configuration>
</plugin>
</plugins>
I have two arguments mentioned in the org.codehaus plugin.
running the below command with argument1 disabled works fine.
mvn org.codehaus.mojo:exec-maven-plugin:1.4.0:exec#runcommand
But when I run the command with both are arguments enabled gives me an error
failed to execute goal com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx
running goal1 and goal2 indivudally with parameters from cmd line works fine.
mvn de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph#create-graphs
mvn com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests "-Dmyhost=hix.qa.com" "-Dmyport=80" "-Dmyprotocol=http" "-Dmythreads=5" "-Dmyloopcount=20" "-Dmyrampup=1" "-Dmytest=ScreenerAPI.jmx"
How to pass parameters to one goal from command line while running multiple goals?
You want to execute multiple commands, so you want to have multiple executions of the plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<!-- First execution executing jmeter-maven-plugin -->
<execution>
<id>runcommand1</id>
<goals>
<goal>exec</goal>
</goals>
<phase> <!-- you need to write a phase here --> </phase>
<configuration>
<arguments>
<argument>com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx</argument>
</arguments>
</configuration>
</execution>
<!-- Second execution executing jmeter-graph-maven-plugin -->
<execution>
<id>runcommand2</id>
<goals>
<goal>exec</goal>
</goals>
<phase> <!-- you need to write a phase here --> </phase>
<configuration>
<arguments>
<argument>de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph#create-graphs</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<executable>mvn</executable>
</configuration>
</plugin>
This configuration does multiple things:
It configures 2 executions for the 2 commands you want to invoke. They are both configured with the correct <argument> attribute.
It factors out the common configuration of those two inside the global configuration element. In this case, this is simply the executable.
There's an important factor though: those executions need to be bound to a phase if you want to execute both of them in a single build. As as example, if you bind them to verify then invoking mvn verify will invoke the two executions.

maven wsdl2java configuration not working properly

I am looking to execute wsdl2java via maven and have tried several different methods with no full success. The first way I was doing it:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.apache.axis.wsdl.WSDL2Java</mainClass>
<arguments>
<argument>-client</argument>
<argument>-o</argument>
<argument>gensrc</argument>
<argument>wsdl/JobAPIWebWrapped.wsdl</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
This version will create the exact structure that I am looking for due to the call to org.apache.axis.wsdl.WSDL2Java, but will not continue with any other maven plugins beyond that. It ends the log with executing main or something to that effect.
The other method I have tried:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>gensrc</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>wsdl/JobAPIWebWrapped.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
The problem with this execution is that it generates a lot more java files than the previous execution stated higher up. I have checked the compatibility of this larger fileset and found it will work fine, but would like to find a way to force it to execute with the same java class as the first example. This version will, however, complete and allow me to move on to the following plugins called by maven.
Third:
<plugin>
<groupId>org.apache.axis</groupId>
<artifactId>wsdl2java-maven-plugin</artifactId>
<version>1.4.1-SNAPSHOT</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<implementationClassName>org.apache.axis.wsdl.WSDL2Java</implementationClassName>
</configuration>
</execution>
</executions>
</plugin>
This version isn't even being recognized... wondering if I am calling the plugin incorrectly because it isn't even showing up anywhere with verbose logging.
I have been searching quite a bit and have yet to find a successful answer. I am very close to just writing a shell script to run the maven set-up by calling the first example then moving on. Any help is greatly appreciated. Thanks.
Instead of using the exec-maven-plugin invoking WSDL2Java, you should use axistools-maven-plugin. Your pom would look like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<wsdlFiles>
<wsdlFiles>wsdl/JobAPIWebWrapped.wsdl</wsdlFiles>
</wsdlFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
By the way, Apache Axis is quite old and broken. You should think about moving to Apache CXF which is more recent and more robust.
I gave in and ended up using the antrun plugin for maven:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<configuration>
<tasks>
<java classname="org.apache.axis.wsdl.WSDL2Java" fork="true">
<arg value="-client"/>
<arg value="-o"/>
<arg value="gensrc"/>
<arg value="wsdl/JobAPIWebWrapped.wsdl"/>
<classpath refid="maven.compile.classpath"/>
</java>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

How to have exec-maven-plugin:exec run before 'site' phase?

I have this script I need to run before site phase to fix an issue (http://jira.codehaus.org/browse/MSITE-640).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3</version>
<inherited>false</inherited>
<executions>
<execution>
<id>workaround-MSITE-640</id>
<!--it should be 'site' but maven doesn't pick up there-->
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<failWithEmptyArgument>false</failWithEmptyArgument>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>./workaround-MSITE-640.sh</executable>
<arguments>
<argument>${settings.localRepository}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
But I can't seem to run it on that phase. I tried to put it under reporting -> plugins but it can't have configuration there.
It is idempotent, so no problem in running it more than once, but would be nice if I could tie it to the correct phase.
Thanks

what is the equivalent of "ant -f" command in maven?

I have an xml file called MakeJar.xml in the location ${basedir}/codebase.
On my shell if I have to run that file i used to use the command "ant -f MakeJar.xml".
Now if I have to run this file using pom.xml how can I do that?
I prepared a following pom.xml. But it dosent work!!!
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<ant antfile="${basedir}/codebase/MakeJar.xml"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Looking at the ant task definition the attribute antfile is described as "the buildfile to use. Defaults to "build.xml". This file is expected to be a filename relative to the dir attribute given."
So you probably have to use:
<ant dir="${project.basedir}" antfile="codebase/MakeJar.xml" />
Also do not forget to specify a <phase> in the plugin section (is missing in your code). The following definition works for me:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<ant
dir="${project.basedir}"
antfile="codebase/MakeJar.xml" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

how to execute multiple command prompt commands using maven in single pom.xml

I want to run multiple command prompt commands in maven using single pom.xml. How can I do that?
For ex: I have 2 commands to execute. I am executing the first command by using exec-maven-plugin.
Below is the portion of my pom.xml to execute the first command:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>load files</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>windchill</executable>
<arguments>
<argument>wt.load.LoadFileSet</argument>
<argument>-file</argument>
<argument>${basedir}/fileSet.xml</argument>
<argument>-UNATTENDED</argument>
<argument>-NOSERVERSTOP</argument>
<argument>-u</argument>
<argument>wcadmin</argument>
<argument>-p</argument>
<argument>wcadmin</argument>
</arguments>
</configuration>
</plugin>
For this the build is success.
Is it possible to execute one more command just like above in the same pom.xml? I was not able to do that. So someone please help how to add it in pom.xml
The answer can be found in the FAQ.
The full answer is here: http://article.gmane.org/gmane.comp.java.maven-plugins.mojo.user/1307
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>id1</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd1</executable>
</configuration>
</execution>
<execution>
<id>id2</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd2</executable>
</configuration>
</execution>
</executions>
</plugin>
Then you specify execution ids as:
mvn exec:exec#id2
But this syntax is possible starting from maven 3.3.1

Resources