How to create a folder if not exist in Maven? - maven

I am writing pom.xml newly and i am trying to Delete a folder already existing and create a new folder.Is there a way to do this? I am looking something like this:
<executions>
<execution>
<configuration>
<tasks>
<if><!-- I need to check if the folder exists and delete->
<delete>
<fileset dir="target/resources"/>
</delete>
</if>
<mkdir dir="target/resources"/>
</tasks>
</configuration>
</execution>

using maven antrun plugin.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>initialize</phase>
<configuration>
<target>
<mkdir dir="${project.build.directory}/target/resources" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Another option would be to use exec-plugin

Related

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

Maven antrun plugin not executing target [duplicate]

I can pass two properties A and B to maven via
mvn test -DA=true
or
mvn test -DB=true
If either A or B is defined i want a target to be skipped. I found it was possible when only A was considered like this:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>skiptThisConditionally</id>
<phase>test</phase>
<configuration>
<target name="anytarget" unless="${A}">
<echo message="This should be skipped if A or B holds" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Now B has to be considered too. Can this be done?
matthias
I would do that with an external build.xml file allowing you to define multiple target combined with antcall and so using one additional dummy target, just to check the second condition.
pom.xml
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>skiptThisConditionally</id>
<phase>test</phase>
<configuration>
<target name="anytarget">
<ant antfile="build.xml"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
and the build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="SkipIt" default="main">
<target name="main" unless="${A}">
<antcall target="secondTarget"></antcall>
</target>
<target name="secondTarget" unless="${B}">
<echo>A is not true and B is not true</echo>
</target>
</project>
Alternative solution if you only have 2 conditions: using the <skip> configuration attribute for one condition (i.e. maven stuff) and the unless (i.e. ant stuff) for the other condition:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>skiptThisConditionally</id>
<phase>test</phase>
<configuration>
<skip>${A}</skip>
<target name="anytarget" unless="${B}">
<echo>A is not true and B is not true</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

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>

Copy a jar file with maven

I'm trying to copy the .jar, created by Maven 3, to another location.
Currently, I'm using Ant's copy task, but Maven simply doesn't copy the file.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<configuration>
<tasks>
<copy file="target/myfile.jar" tofile="D:/Bukkit/plugins/myfile.jar"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<configuration>
<tasks>
<copy file="target/myfile.jar" tofile="D:/Bukkit/plugins/myfile.jar"/>
</tasks>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

maven-antrun-plugin skip target if any of two possible conditions holds

I can pass two properties A and B to maven via
mvn test -DA=true
or
mvn test -DB=true
If either A or B is defined i want a target to be skipped. I found it was possible when only A was considered like this:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>skiptThisConditionally</id>
<phase>test</phase>
<configuration>
<target name="anytarget" unless="${A}">
<echo message="This should be skipped if A or B holds" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Now B has to be considered too. Can this be done?
matthias
I would do that with an external build.xml file allowing you to define multiple target combined with antcall and so using one additional dummy target, just to check the second condition.
pom.xml
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>skiptThisConditionally</id>
<phase>test</phase>
<configuration>
<target name="anytarget">
<ant antfile="build.xml"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
and the build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="SkipIt" default="main">
<target name="main" unless="${A}">
<antcall target="secondTarget"></antcall>
</target>
<target name="secondTarget" unless="${B}">
<echo>A is not true and B is not true</echo>
</target>
</project>
Alternative solution if you only have 2 conditions: using the <skip> configuration attribute for one condition (i.e. maven stuff) and the unless (i.e. ant stuff) for the other condition:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>skiptThisConditionally</id>
<phase>test</phase>
<configuration>
<skip>${A}</skip>
<target name="anytarget" unless="${B}">
<echo>A is not true and B is not true</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Resources