Can somebody tell me if it is possible to spawn a process and then kill that process when integration tests have finished running?
I am currently using the ant run plugin to start a grunt connect server and I am using cargo to deploy my rest app to tomcat, this allows me to integration test against the running angular web app which calls rest services.
I almost have everything how I want it but.. when the build has finished the grunt server is still running because I have set keep alive to true.
Ideally when my build finishes I would like to somehow kill the process the for the server.
I came back to this as the final piece I needed to fix to get my multi-module project building and running integration tests against an angular front end and java back end as my build runs in maven.
The final thing to do to kill the node server which is spawned is to use the ant run plugin to kill it (simple really!).
Anyway hopefull this might help someone else in the future:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>Run grunt integration-test task in pre-integration-test phase</id>
<phase>pre-integration-test</phase>
<configuration>
<target name="starting">
<echo>
</echo>
<exec executable="cmd" spawn="true" dir="${project.basedir}"
osfamily="windows">
<arg line="/c grunt int-test --no-color > grunt.status " />
</exec>
<exec executable="bash" spawn="true" dir="${project.basedir}"
osfamily="unix">
<arg line="grunt int-test --no-color > grunt.status" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>Kill NodeServer in post-integration-test phase</id>
<phase>post-integration-test</phase>
<configuration>
<target name="ending">
<echo>
</echo>
<exec executable="cmd" spawn="true" dir="${project.basedir}"
osfamily="windows">
<arg line="/c Taskkill /IM node.exe /F " />
</exec>
<exec executable="bash" spawn="true" dir="${project.basedir}"
osfamily="unix">
<arg line="kill -9 $(ps aux | grep '\snode\s' | awk '{print $2}')" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Related
I'm starting postgres container with maven docker plugin and then use the DB to generate some artefacts in later steps.
<plugin>
…
<artifactId>docker-maven-plugin</artifactId>
…
<configuration>
<images>
<image>
...
<name>postgres:11</name>
...
</image>
</images>
</configuration>
<executions>
<execution>
<id>start-postgres-container</id>
<phase>generate-sources</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-postgres-container</id>
<phase>process-sources</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
The issue I have is that when there is some error with any operations between start and stop above, then maven leaves the running container and consecutive build attempts will fail so one has to put down the leftover container manually first.
Is there a way/plugin in maven to specify some finally action/phase? So that in case of failure in build scripts one would still be able to release some resources which might've been already reserved?
The goal could be achieved applying similar solution to what testcontainers are doing using Ryuk: https://github.com/testcontainers/moby-ryuk
The image to reap should be labeled, e.g. as:
<autoRemove>true</autoRemove>
<labels>
<killme>true</killme>
</labels>
<wait>
<time>3000</time>
</wait>
The above delay is to give some time for
Ryuk setup. As this one must be started in parallel...
<image>
<alias>ryuk-summoned</alias>
<name>testcontainers/ryuk:0.3.0</name>
<run>
<ports>
<port>ryuk.port:8080</port>
</ports>
<volumes>
<bind>
<volume>/var/run/docker.sock:/var/run/docker.sock</volume>
</bind>
</volumes>
<autoRemove>true</autoRemove>
</run>
</image>
The challange is that there must be feed with a hartbeat towards Ryuk container at least 1 per 10s through a TCP socket containing deathnote, eg. like this: printf "label=killme" | nc localhost 8080.
This is easy to achieve with below maven exec plugin and a simple setup-ryuk.sh script calling the command mentioned above. (NOTE: this provides only single deathnote with no consecutive heartbeats so Ryuk dies by itself in next 10s - at that time it will reap all items for which it received a note).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>stop-postgres</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<target>
<exec executable="bash">
<arg value="setup-ryuk.sh"/>
<arg value="${ryuk.port}"/>
</exec>
</target>
</configuration>
</plugin>
To make this platform independent (as the above is valid for Linux/Mac) and to keep Ryuk alive for longer time the best way seems to be to come up with own maven plugin sending messages to TCP socket.
In Maven, we can use exec-maven-plugin to execute bash commands in the build.
Which plugin of Central Repository can perform the same task?
I ask it because I have to execute a bash command after another plugin that needs to be executed in the same phase only after exec-maven-plugin, so I can't do it directly inside the exec-maven-plugin.
The bash command that I want to execute in the Maven build is the following:
cat file1 >> file2
Thanks in advance.
I managed to solve my issue with maven-antrun-plugin with the <concat> task:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>final step</id>
<phase>install</phase>
<configuration>
<target>
<concat destfile="${project.build.directory}/${project.artifactId}-${project.version}.sh" binary="yes">
<fileset file="${project.build.directory}/script/self-installer.sh" />
<fileset file="${project.build.directory}/${project.artifactId}-${project.version}.tar.gz" />
</concat>
<chmod file="${project.build.directory}/${project.artifactId}-${project.version}.sh" perm="+x"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
This is the equivalent of the bash cat command.
Keep in mind that if you are concatenating a binary file, you have to set binary="yes", otherwise the Ant task will corrupt the final file.
In any case, this is still not a bash-based solution, it's only a trick that uses Ant routines, so it's not a real equivalent of exec-maven-plugin
Is it possible to set the max DFA states before table used rather than inlining in the configuration of the plugin maven antlr3 like the extended option -Xmaxinlinedfastates in the command line of antlr.Tool ?
I find it for ant script but nothing with maven.
If yes, how to ?
Thanks.
Thanks to Jiri Tousek.
I found an alternative to what I call a gap in the maven antlr3 plugin. Using the maven antrun plugin allowed me to solve this problem.
Here is the statement I made in pom.xml and that works for me:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<target>
<!-- Place any Ant task here. You can add anything you can add between
<target> and </target> in a build.xml. -->
<java classname="org.antlr.Tool" fork="true"
failonerror="true">
<arg value="-verbose" />
<arg value="-Xmaxinlinedfastates"/>
<arg value="10"/> <!-- Value for the exended option -Xmaxinlinedfastates -->
<arg path="./src/main/antlr3/*.g" />
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
there is -f option in maven, that allows to specify alternate pom.xml file. Is there a possibility, that I can also bring this behaviour to the executed modules? Now it looks like, that when I have this structure: projectA: pom.xml pom.xml2
projectB: pom.xml pom.xml2
And when I run maven with -f pom.xml2 option as reactor with projectB specified as module, it looks like that it picks pom.xml2 from the projectA, and it picks pom.xml from projectB. Is there a way, how can I propagate the -f option to the modules?
Thanks for answering.
Because we can specified pom file in module definition.1
Here's an example for using alternative pom file in module.
<modules>
<module>child1/pom-jdk14.xml</module>
<module>child2/pom-jdk14.xml</module>
</modules>
As Jörn Horstmann comments I would try lots of things to get this working with profiles in one pom.
If that's not possible the only way I can think of to get this working is to bypass the normal maven mechanism by using a "switching pom" with profiles. This pom is put as pom.xml in each module and has a profile for each of your pom.xml2 (or others) and in that profile executes another maven build f.e. via the antrun-plugin with the -f for the pom you need:
<profile>
<id>xml2</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>build pom.xml2</id>
<phase>prepare-package</phase> <!-- whatever suits you -->
<configuration>
<target>
<echo level="info" message="Building pom.xml2..." />
<exec executable="cmd" dir=".">
<arg value="/c" />
<arg value="mvn" />
<arg value="-f" />
<arg value="pom.xml2" />
<arg value="install" /> <!-- enter which phase you need -->
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
I'm using Maven 3.0.3. I have this antrun task, which uses the "exec" command ...
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>start-xvfb</id>
<phase>process-test-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Starting xvfb ..." />
<exec executable="Xvfb" spawn="true" failonerror="true">
<arg value=":0.0" />
</exec>
</tasks>
</configuration>
</execution>
Although I can see the echo statement in my output, I can't see any of the executables output in standard out. What can I do to redirect it to the same place that the echo message goes to?
Thanks, - Dave
The spawn option is the problem. See the ant exec task documentation:
If you spawn a command, its output will not be logged by ant.
Furthermore, make sure there is no output or output property present, as they will redirect the output to a property or file (see this stackoverflow question).