Maven surefire. Adding timestamp to folder with reports - maven

I'm sure that such a question is already somewhere in internet, but I can't find it so..
I use maven-surefire-plugin.
It generates surefire-reports folder with reports in it.
I want to have different folders for each run.
I'm trying to add timestamp in report name like this:
Pom part:
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
<sourceDirectory>src/main/java</sourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<reportsDirectory>${project.build.directory}_${timestamp}</reportsDirectory>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
But in this case I have no surefire-reports folder at all.
Somewhere is a mistake but I have no idea where.
Can someone give me a clue about this?

That date format is not a valid directory name (especially for the the : char), maybe something like yyyy-MM-dd_HH-mm? also I would go to the target folder:
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd_HH-mm</maven.build.timestamp.format>
<sureFireDir>${project.basedir}/target/${timestamp}</sureFireDir>
</properties>
...
<reportsDirectory>${sureFireDir}</reportsDirectory>

Related

Method filter for Surefire

From this page:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
under "Fully qualified class name" section, It seems possible to specify which test methods to run in pom.xml. However, it is not very clear where to write this tag called "test". Anyone could shed some lights?
Update:
You can define it inside <properties>, as in:
<project>
<properties>
<test>TestCircle#testSlow</test>
</properties>
</project>
Also, you can use includes and excludes as hinted here, e.g.:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>Sample.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
and
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>

maven override/replace file into WEB-INF/classes

I want to override/replace my spring config xml file with specified location file while package as war.
And I do not want to use filter plugin (filter plugin must to use dolloar placeholder, it will run with error while local deploy), is there any plugin or setting I can use to do this?
project structure as follow:
ROOT
----config
----prd
----spring-servlet.xml
----web.xml
----src
----main
----java
----resources
----spring-servlet.xml
----webapp
....
My pom.xml is like as follow:
...
<profiles>
<profile>
<id>prd</id>
<properties>
<filterDir>prd</filterDir>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webXml>${basedir}/config/${filterDir}/web.xml</webXml>
<webResources>
<resource>
<directory>config/${filterDir}</directory>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>mvc-dispatcher-servlet.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
run maven use :mvn clean package -P prd.
Will work fine if not set tartgetPath, file copied to web root.
You can use copy-resources of maven to do this.
Example maven code should be like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>your-target-directory</outputDirectory>
<resources>
<resource>
<directory>source-directory</directory>
<!--You can also mention files too-->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Read more about copy-resources for correct implementation.

Excluding folders/files from specific folder in pom.xml [duplicate]

Trying to exlcude a folder src/main/resources/scripts/ from my build but the following does not work:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>src/main/resources/scripts/</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<excludes>
<exclude>src/main/resources/scripts/</exclude>
</excludes>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
Any ideas?
Instead try:
<exclude>scripts/**</exclude>
The exclude is based on directory, so your construction would exclude
src/main/resources/src/main/resources/scripts
I had a similar problem and found the following issues:
You may have a parent pom which already defines a configuration for the maven-compiler-plugin. For this, add combine.self="override" to the configuration tag. See Maven : Is it possible to override the configuration of a plugin already defined for a profile in a parent POM
It seems the plugin ignores excludes if it needs the excluded classes for the compilation: make sure you are not referencing the excluded classes from other classes which will get compiled. For example, if you exclude Foo.java, but in Bar.java you import Foo; it will (try to) compile Foo.java to compile Bar.java.
For example:
<profiles>
<profile>
<id>myId</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration combine.self="override">
<excludes>
<exclude>**/some/full/directory/*</exclude>
<exclude>**/some/single/File.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profiles>
<profile>
<id>readBuild</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration >
<excludes>
<exclude>**/com/pyramid/controllers/EntitlementWriteController.java</exclude>
<exclude>**/com/pyramid/controllers/ProductWriteController.java</exclude>
</excludes>
<testExcludes>
<testExclude>**/com/pyramid/controllers/EntitlementWriteControllerTest.java</testExclude>
<testExclude>**/com/pyramid/controllers/ProductWriteControllerTest.java</testExclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
<directory>yourDirectory</directory>
</build>
</profile>
It's so very simple and you not need add other plugin:
https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
</excludes>
</resource>
</resources>
</build>

How exclude files/folder from Maven outputDirectory

I am developing maven project using Spring boot and JSF. According to this example, the managed bean .class files should be put into /src/webapp/WEB-INF/classes directory, to be shown on JFS views. This is achieved by adding outputDirectory tag to pom.xml:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>${maven-compiler-plugin.version}</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
But, I have noticed that except *.class files, there is also stored content of src/main/resources directory: i18n folder, templates folder, *.sql files, application.properties etc....
My question: How to exclude these unnecessary files/folders from /src/webapp/WEB-INF/classes directory?
Just add the file you dont want to the maven exludes.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/src/main/java/myClassesToExclude*.java</exclude>
</excludes>
</configuration>
</plugin>
There are different ways of excluding files in Maven. I am pointing out the general ways here.
To exclude a resource, you only need to add an element.
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>[your directory]</directory>
<excludes>
<exclude>[non-resource file #1]</exclude>
<exclude>[non-resource file #2]</exclude>
<exclude>[non-resource file #3]</exclude>
...
<exclude>[non-resource file #n]</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
To include everything except the bitmaps, jpegs, and gifs, we can simply exclude them by:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<excludes>
<exclude>**/*.bmp</exclude>
<exclude>**/*.jpg</exclude>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.gif</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
You can also have both and elements. For example, if you want to include all text files that does not contain the word "test" in their filename.
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
resource maven.apache.org
If you want to exclude from war file then use [warSourceExcludes] or [packagingExcludes] tag as :
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceExcludes>WEB-INF/abc.xml,WEB-INF/pqr.xml</warSourceExcludes>
<packagingExcludes>WEB-INF/abc.xml,WEB-INF/pqr.xml</packagingExcludes>
</configuration>
</plugin>

How to get the icons for the resulted maven-surefire-report-plugin

I'm using maven-surefire-report-plugin in order to generate the unit tests report.
This is working fine however the report contains links to images which are not presented.
How can I make maven copy the icons required for the report?
Should I use skin? I tried it without success.
Here is the maven-surefire-report-plugin definition:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.16</version>
<configuration>
<showSuccess>true</showSuccess>
</configuration>
</plugin>
</plugins>
</reporting>
I tried to add skin plugin but it didn't affect the report:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-application-skin</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
What is missing to present the report with the images?
The question is probably too old, but I had the same problem and I found this answer.
The command mvn site -DgenerateReports=false generates only css and images for surefire-report.html and works fine.
You must genereate the site using the Site Plugin:
mvn site
If you have already generated the reports, then you can speed this up by skipping this:
mvn site -DgenerateReports=false
mvn site -DgenerateReports=false
I did it like this, but it's a hack:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire.version}</version>
<configuration>
<testSourceDirectory>src/test/java</testSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven-surefire.version}</version>
<configuration>
<linkXRef>false</linkXRef>
<showSuccess>true</showSuccess>
</configuration>
<executions>
<execution>
<id>generate-test-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/test/resources/css</directory>
<targetPath>${project.build.directory}/site/css</targetPath>
<includes>
<include>*.css</include>
</includes>
</resource>
</resources>
</build>

Resources