I have a mock server which I need to start in Jenkins before running my Automation tests and then stop the mock server after my tests have run.
This mock server is a maven project and is using exec-maven-plugin. I am able to start this server by running the command mvn exec:java. However I am unable to stop this maven project through Jenkins. I have read about it and most answers tell to find the Process ID of this Maven project and then kill it. Is there a cleaner and easier way to stop this project ?
You need to start and stop your sever using goals which are part of the maven life cycle.
An example taken from this answer
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
The key to this issue is indeed that you don't know the processId. Consider using exec:exec instead with async. You'll see that asyncDestroyOnShutdown is set to true by default, meaning it'll shut down once Maven is shutting down.
If you are running linux:
ps aux |grep <project-name> and then kill -9 <process id>
Related
Im looking to see any maven configuration which will enable me to run Sonar Scan on my code for every maven build. I dont want to use a separate goal but somehow enforce it as part of users regular build commands.
You can attach Sonar to a phase (e.g. verify) like this:
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.5.0.1254</version>
<executions>
<execution>
<id>verify-sonar</id>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
This also works with other phases like compile or package.
I am trying to replace maven exec with MavenInvokerPlugin because of problems on Jenkins with forwarding the maven settings file.
So in bash it looks straight:
mvn dependency:copy-dependencies#resolve-maven-deps
My translation to MavenInvokerPlugin configuration is
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<projectsDirectory>${project.basedir}/src/main/docker</projectsDirectory>
<localRepositoryPath>${project.build.mavenDependencies}</localRepositoryPath>
<goal>dependency:copy-dependencies#resolve-maven-deps</goal>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>run</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
It looks like that execution id is completely ignored, because I tried random strings and mvn builds the project with success.
mvn dependency:copy-dependencies#asdfasdfa
So I'd like to know whether this feature is supported at all and what I am doing wrong.
P.S. I know that calling maven out of maven is anti pattern, but here is exactly that rare case when there is no other way.
After looking at projects using maven invoker I figured out the trick.
goal tag is not used, instead provide invokerPropertiesFile:
<pom>${project.basedir}/xxx/pom.xml</pom>
<invokerPropertiesFile>${project.basedir}/invoker.properties</invokerPropertiesFile>
content of the file:
invoker.goals=compile -P resolve-maven-deps
I recently started with osgi. I'm using pax tools to generate and deploy my bundles. I'm trying to create a console application, but everytime I launch pax-provision command gogo shell is fired. I also want to know what is the difference between pax-provision and pax-run ( I don't use pax-run ).
I turned off the gogo shell by editing the pom.xml in the base directory of the projec, by adding --noConsole to configuration :
<plugin>
<groupId>org.ops4j</groupId>
<artifactId>maven-pax-plugin</artifactId>
<version>1.5</version>
<configuration>
<provision>
<param>--platform=felix</param>
<param>--noConsole</param>
</provision>
</configuration>
<executions>
<execution>
<id>ide-support</id>
<goals>
<goal>eclipse</goal>
</goals>
</execution>
</executions>
</plugin>
I am trying to perform integration tasting of the deployment of my application on the top of a custom container. Since my container is custom, I cannot use Maven Cargo plugin to setup the container.
My container:
Has to be started though a proper bat file, which is in the path of the machine where the tests are run.
Can be manually closed since I have a single maven module containing all my integration tests, even if one day I would like to know how to shut the process down after my tests are completed.
My problem is that I have to run my container in a different process, because it needs to keep running while my tests are performed. Furthermorely, I have an API in my tests that let me wait for the container to be ready (a sort of lookup with timeout).
I have added the following lines to my pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>scripts/start-agent.bat</executable>
</configuration>
</plugin>
This will call a script, which contains only
start call gs-agent.bat
However the mvn exec plugin gets stucked and my tests are not run. According to what is suggested in How do I run a batch file from my Java Application? , I have modified my pom.xml as the following:
<configuration>
<executable>cmd</executable>
<arguments>
<argument>/C</argument>
<argument>start</argument>
<argument>gs-agent.bat</argument>
</arguments>
</configuration>
But this does not seem to solve the issue:
exec plugin is not able to do this, and I found the issue for it, too: http://jira.codehaus.org/browse/MEXEC-87 (link now dead due to codehaus rampdown, can be found from web archive)
In the jira issue linked above, there is a mention and a link of a fork for exec plugin that would have the functionality.
Other than that, I think you'll need to use an antrun-plugin for the time being.
Here's an example taken from working configuration and run with mvn verify. This needs to be in the <plugins>, not <pluginManagement> (exec could reside in pluginmanagement just fine).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<configuration>
<target>
<exec executable="cmd.exe"
spawn="true">
<arg value="/c"/>
<arg value="D:\myfolder\test.bat"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Note that spawn="true" is key here if you don't want the execution to block, like specified in the question. If you do want it to block and see the output immediately, set it to false.
See this question: How do I run a batch file from my Java Application?
Windows Batch Files are not executable. They are scripts that are run by the cmd executable.
More Information
Exec plugin source code reveals that Apache Commons Executor is used to actually execute the command line.
There is a lot of reading you can do here, i.e. in the Apache Commons Executor documentation and their JIRA issues, but the short version is: this isn't a problem with "Maven," it's a problem with the platform-dependent nature of executing an exec() command.
I've tackled this sort of problem before, and the solution I always devise is to deconstruct the .bat script into its actual commands and launch it directly from the exec plugin, rather than calling the script.
The Fallowing Code works for me
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<exec dir="${project.basedir}" executable="Script.bat" failonerror="true">
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Source https://maven.apache.org/guides/mini/guide-using-ant.html
In my case I had trouble with npm and ng.. the underlying problem was exec-maven-plugin was trying to execute the sh scripts (which had no extensions).
Renaming
C:\Users\User\AppData\Roaming\npm\ng to C:\Users\User\AppData\Roaming\npm\ng.sh
and
C:\apps\nodejs\npm to C:\apps\nodejs\npm.sh
solved the problem.
If you want to use exec-maven-plugin here is an example to run a batch script with arguments. E.g. to run something like:
.\test.bat arg1 arg2 arg3
add the following to pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>exec-test</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>cmd</executable>
<workingDirectory>./</workingDirectory>
<arguments>
<argument>/c</argument>
<argument>start</argument>
<argument>""</argument>
<argument>exec.bat</argument>
<argument>arg1</argument>
<argument>arg2</argument>
<argument>arg3</argument>
</arguments>
</configuration>
</plugin>
I am trying to execute the following scenario using maven :
pre-integration-phase : Start a java based application using a main class (using exec-maven-plugin)
integration-phase : Run the integration test cases (using maven-failsafe-plugin)
post-integration-phase: Stop the application gracefully (using exec-maven-plugin)
Here is pom.xml snip:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>launch-myApp</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-DMY_APP_HOME=/usr/home/target/local</argument>
<argument>-Djava.library.path=/usr/home/other/lib</argument>
<argument>-classpath</argument>
<classpath/>
<argument>com.foo.MyApp</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<forkMode>always</forkMode>
</configuration>
</plugin>
</plugins>
If I execute mvn post-integration-test, my application is getting started as a child process of the maven process, but the application process is blocking the maven process from executing the integration tests which comes in the next phase. Later I found that there is a bug (or missing functionality?) in maven exec plugin, because of which the application process blocks the maven process. To address this issue, I have encapsulated the invocation of MyApp.java in a shell script and then appended “/dev/null 2>&1 &” to spawn a separate background process. Here is the snip (this is just a snip and not the actual one) from runTest.sh:
java - DMY_APP_HOME =$2 com.foo.MyApp > /dev/null 2>&1 &
Although this solves my issue, is there any other way to do it? Am I missing any argument for exec-maven-plugin?
You can use the async configuration parameter to achieve what you require. Also see asyncDestroyOnShutdown which will shutdown the app on JVM exit by default.
https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#async
If set to true the child process executes asynchronously and build execution continues in parallel.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>launch-myApp</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-DMY_APP_HOME=/usr/home/target/local</argument>
<argument>-Djava.library.path=/usr/home/other/lib</argument>
<argument>-classpath</argument>
<classpath/>
<argument>com.foo.MyApp</argument>
</arguments>
<async>true</async>
</configuration>
</plugin>
(Unix only)
Use exec:exec and start a script.
This script should call
java -DMY_APP_HOME =$2 com.foo.MyApp > /dev/null 2>&1 &
first, and check the startup before the script finishes.
Did the program crash in 2 seconds?
When your java app needs some time to start you can add a script that waits for a complete startup.
retrymax=30
retry=0
RETURNCODE=1
while [ ${retry} -lt ${retrymax} ]; do
todo_your_test
RETURNCODE=$?
if [ ${RETURNCODE} -eq 0 ]; then
echo MyApp started
break
else
(( retry = retry + 1 ))
sleep 2
fi
done
exit ${RETURNCODE}
I am using the https://github.com/bazaarvoice/maven-process-plugin plugin while this issue is still open https://github.com/mojohaus/exec-maven-plugin/issues/18
There's a possible solution in this thread using Ant: Possible solution here: Maven and Exec: forking a process?
The same disadvantage as in your current solution is that it's platform dependent as well. Your current solution will only run in Unix/Linux-like environments, while the Ant-based solution is starting an instance of cmd, which only works on Windows. Using Maven profiles, one could create a configuration that uses the OS it's running on to determine which command shell to start, of course.