How do I make this plugin run only on non-Windows platforms? - maven

I'm using Maven 3.0.3. For our project integration testing, we require a virtual frame buffer to be created, using Unix commands. However, when we run our project on the Windows machines, we don't need this. We use
<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">
<arg value=":0.0" />
</exec>
</tasks>
</configuration>
</execution>
<execution>
<id>shutdown-xvfb</id>
<phase>post-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Ending xvfb ..." />
<exec executable="killall">
<arg value="Xvfb" />
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
How can I make the above run when the platform isn't windows and suppress the running of the plugin otherwise? Thanks, - Dave

You can do this with profiles. There is profile activation switch for OS settings. And the plugin is so intelligent, you can use negation.
<profiles>
<profile>
<activation>
<os>
<family>!windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
...
</plugin>
</plugins>
</build>
...
</profile>
</profiles>
Further information about profiles can be found here, and about the values you can use here.

Related

Maven plugin maven-antrun-plugin copy option does not overwrite

I have a problem with this maven plugin and I don't really know how to solve it.
I am trying to copy some resources to "${basedir}/../server/a/base-store" to "${basedir}/../resources/store/base_certificate_store_prod/base-store"
However I use "overwrite" on copy. But at the final end under "${basedir}/../server/a/base-store" the files are added and the others which were before still exist.
I would like the hole "base-store" directory to be replaced.
<profile>
<id>PROD</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-prod-base-store</id>
<phase>compile</phase>
<configuration>
<target>
<copy todir="${basedir}/../server/a" overwrite="true">
<fileset dir="${basedir}/../resources/store/base_certificate_store_prod" includes="**/*"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Just delete the directory first and then copy.

How can a maven property be used in a maven attribute?

For a previously defined property <scala.js.opt> I'd like to be able to include it like so in a pom.xml file:
<arg value="${scala.js.opt}"/>
However, this doesn't seem to work. Is it even possible, or are there any workarounds?
The particulars of my specific case is that these are arguments to an executable in the maven-antrun-plugin, should this be easier with my specific case.
Detailed example follows
In master pom file:
<properties>
<scala.js.opt>fastOptJS</scala.js.opt>
</properties>
Then in child pom file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<target>
<exec executable="cmd.exe"
spawn="true">
<arg value="/c"/>
<arg value="C:\Program Files (x86)\sbt\bin\sbt.bat"/>
<arg>${scala.js.opt}</arg>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
When run using e.g., mvn install, this produces:
org.apache.tools.ant.types.Commandline$Argument doesn't support nested text data ("fastOptJS")

how to generate jmeter dashboard report through maven project

I am able to generate Jmeter dashboard report manually using this command jmeter -g /path/to/jtl/file -o /where/you/want/to/store/dashboard
but I want to generate it through maven project.
Is there any way?
Below is the plugin ex:
<goals>
<goal>jmeter</goal>
</goals>
<configuration>
<propertiesUser>
<jmeter.save.saveservice.output_format>csv</jmeter.save.saveservice.output_format>
<jmeter.save.saveservice.bytes>true</jmeter.save.saveservice.bytes>
<jmeter.save.saveservice.label>true</jmeter.save.saveservice.label>
<jmeter.save.saveservice.latency>true</jmeter.save.saveservice.latency>
<jmeter.save.saveservice.response_code>true</jmeter.save.saveservice.response_code>
<jmeter.save.saveservice.response_message>true</jmeter.save.saveservice.response_message>
<jmeter.save.saveservice.successful>true</jmeter.save.saveservice.successful>
<jmeter.save.saveservice.thread_counts>true</jmeter.save.saveservice.thread_counts>
<jmeter.save.saveservice.thread_name>true</jmeter.save.saveservice.thread_name>
<jmeter.save.saveservice.time>true</jmeter.save.saveservice.time></propertiesUser>
<propertiesSaveService>
<output_format>csv</output_format>
</propertiesSaveService>
This is how I create the HTML report with mvn using mvn ant plugin.
I have my report-template and reportgenerator.properties under src/testresources.
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.0.3</version>
<configuration>
<testResultsTimestamp>false</testResultsTimestamp>
<propertiesUser>
<jmeter.save.saveservice.output_format>csv</jmeter.save.saveservice.output_format>
<jmeter.save.saveservice.bytes>true</jmeter.save.saveservice.bytes>
<jmeter.save.saveservice.label>true</jmeter.save.saveservice.label>
<jmeter.save.saveservice.latency>true</jmeter.save.saveservice.latency>
<jmeter.save.saveservice.response_code>true</jmeter.save.saveservice.response_code>
<jmeter.save.saveservice.response_message>true</jmeter.save.saveservice.response_message>
<jmeter.save.saveservice.successful>true</jmeter.save.saveservice.successful>
<jmeter.save.saveservice.thread_counts>true</jmeter.save.saveservice.thread_counts>
<jmeter.save.saveservice.thread_name>true</jmeter.save.saveservice.thread_name>
<jmeter.save.saveservice.time>true</jmeter.save.saveservice.time>
</propertiesUser>
</configuration>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<configuration>
<tasks>
<mkdir dir="${basedir}/target/jmeter/results/dashboard" />
<copy file="${basedir}/src/test/resources/reportgenerator.properties"
tofile="${basedir}/target/jmeter/bin/reportgenerator.properties" />
<copy todir="${basedir}/target/jmeter/bin/report-template">
<fileset dir="${basedir}/src/test/resources/report-template" />
</copy>
<java jar="${basedir}/target/jmeter/bin/ApacheJMeter-3.0.jar" fork="true">
<arg value="-g" />
<arg value="${basedir}/target/jmeter/results/*.jtl" />
<arg value="-o" />
<arg value="${basedir}/target/jmeter/results/dashboard/" />
</java>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
First of all you will need to configure JMeter Maven Plugin to store the test results in the format, suitable for HTML Reporting dashboard generation, i.e. add the next few lines to your pom.xml file:
<jmeter.save.saveservice.output_format>csv</jmeter.save.saveservice.output_format>
<jmeter.save.saveservice.bytes>true</jmeter.save.saveservice.bytes>
<jmeter.save.saveservice.label>true</jmeter.save.saveservice.label>
<jmeter.save.saveservice.latency>true</jmeter.save.saveservice.latency>
<jmeter.save.saveservice.response_code>true</jmeter.save.saveservice.response_code>
<jmeter.save.saveservice.response_message>true</jmeter.save.saveservice.response_message>
<jmeter.save.saveservice.successful>true</jmeter.save.saveservice.successful>
<jmeter.save.saveservice.thread_counts>true</jmeter.save.saveservice.thread_counts>
<jmeter.save.saveservice.thread_name>true</jmeter.save.saveservice.thread_name>
<jmeter.save.saveservice.time>true</jmeter.save.saveservice.time>
I believe the most straightforward way would be using Exec Maven Plugin, something like:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>${basedir}/target/jmeter/bin/ApacheJMeter-3.0.jar</argument>
<argument>-g</argument>
<argument>${basedir}/target/jmeter/results/${maven.build.timestamp}-example.jtl</argument>
<argument>-o</argument>
<argument>${basedir}/target/dashboard</argument>
</arguments>
</configuration>
</plugin>
You might need to copy reportgenerator.properties file and report-template folder to "target/jmeter/bin" directory of your Maven project (it will not survive "clean" phase) or duplicate the properties as it described in Adding Additional Properties To chapter.
See Five Ways To Launch a JMeter Test without Using the JMeter GUI article for more information on different options of executing a JMeter test

How to set a property based on environment value?

I try to create a maven script which takes care of my packaging for different environments dev, test and production.
I'd like to make some kind of template which copies certain files into a specific directory. Something like this:
<profile>
<id>default-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>prepare-package</id>
<phase>prepare-package</phase>
<configuration>
<target>
<echo message="Copying configuraiton files .."/>
<copy file="${package-mode.config-directory}/x.xml" todir="src/main/webapp/WEB-INF/config" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
My current problem is that I need ${package-mode.config-directory} to be set according to the environment which I want to build for.
I am currently using the maven-enforcer-plugin in order to require an environment variable to be set:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>package-mode</property>
<message>You must set a "package-mode" property!</message>
<regex>(test|dev|deploy)</regex>
<regexMessage>
[ ########### ] "package-mode"" must be either "test", "dev" or "deploy".
</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
But I'm now facing the problem that I need some kind of conditional build for ${package-mode.config-directory}. I would need something like this:
<plugin>
<!-- ... -->
<property>package-mode</property>
<condition>
<equals>test</equals>
<package-mode.config-directory>src/main/assembly/test</package-mode.config-directory>
</condition>
<condition>
<equals>dev</equals>
<package-mode.config-directory>src/main/assembly/dev</package-mode.config-directory>
</condition>
<condition>
<equals>deploy</equals>
<package-mode.config-directory>src/main/assembly/deploy</package-mode.config-directory>
</condition>
</plugin>
Is it possible to do that?
Turns out I could simply use the already enforced environment variable package-mode and just name the directories to its valid values. One can argue about whether this is "nice" or not but it works and it builds my war file:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>prepare-package</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/webapp/WEB-INF/config</outputDirectory>
<resources>
<resource>
<directory>src/main/assembly/${package-mode}/config</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Maven antrun plugin set property based on OS

I am converting an ant script to Maven and decided to use Maven ant run plugin. I am on the good way, but I hit the following problem. Ant source script uses target definition as follows:
<condition property="isWindows">
<os family="windows"/>
</condition>
<condition property="isUnix">
<os family="unix"/>
</condition>
<target name="init-windows" depends="" if="isWindows">
<property file="${user.home}/abc.properties"/>
</target>
<target name="init-unix" depends="" if="isUnix">
<property name="abc.home" value="${env.ABC_HOME}"/>
</target>
The point is to use property value
abc.home
later in the build cycle which is dependent on OS (Win,Linux).
In the ant script it is ok,but maven ant run plugin does not enable using the multiple targets. I do not want to use Maven profile tags. I would like to use ant tag for this if there is any? Does anybody have a hint?
I think an easier solution would be not to use the maven-antrun-plugin and use profiles instead.
Profiles are a good way to have a different of properties depending of some activation property. One of those activation property can be the OS which is running the build.
Consider the following POM:
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${user.home}/abc.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</profile>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<abc.home>${env.ABC_HOME}</abc.home>
<properties>
</profile>
</profiles>
When run on a Windows machine, the windows profile will be activated. Reading a property file is done with the properties-maven-plugin and its content will be placed in properties, just like the Ant <property> task.
When run on an Unix machine, the unix profile will be activated and the abc.home property will be set to ${env.ABC_HOME}.
Then, in your Maven build, you can use ${abc.home} without having to worry in which profile you are in.
Having said that, another solution would be to run multiple executions of the maven-antrun-plugin. In the first execution, you decide whether the build in running in a Windows or Unix machine and then skip the following executions accordingly. This would be a sample configuration, where myPhase would be the phase you want this to run.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>myphase</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isUnix">
<os family="unix" />
</condition>
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
</execution>
<execution>
<phase>myphase</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="init-windows" depends="">
<property file="${user.home}/abc.properties" />
</target>
<exportAntProperties>true</exportAntProperties>
<skip>${isUnix}</skip>
</configuration>
</execution>
<execution>
<phase>myphase</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="init-unix" depends="">
<property name="abc.home" value="${env.ABC_HOME}" />
</target>
<exportAntProperties>true</exportAntProperties>
<skip>${isWindows}</skip>
</configuration>
</execution>
</executions>
</plugin>

Resources