Maven: change the directory in which tests are executed - maven

In my code I have set to create some files in ./ directory, because when the software will be deployed in the installation machine, all the configuration files will be created in ./ if it's the first run
Unfortunately when I build the project, and Maven executes tests, all the files are created in the project directory.
I want those files to be created in target/test-run/
How can I do it?
Thanks

If you are using the surefire plugin to execute tests (which is the default), then you can set working directory like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<workingDirectory>${java.io.tmpdir}</workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
Reference: http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html
To set to the specific directory in the question:
<workingDirectory>${project.build.directory}/test-run</workingDirectory>

Related

GeoLite2 database gets corrupt when added to war

Like the question GeoLite2 database gets corrupt when added to jar, I got the error from the WAR file with the following steps:
mvn package to copy GeoLite2-City.mmdb from src/main/resources/GeoLite2-City.mmdb to war file under WEB-INF/classes/GeoLite2-City.mmdb
GeoLite2-City.mmdb was changed in the first step mvn package
I tried the plugin maven-resources-plugin as https://stackoverflow.com/a/34454312/1086907, but GeoLite2-City.mmdb is still changed by mvn package
How to troubleshoot the issue?
The maven-war-plugin can actually also filter that file.
Just like for the maven-resources-plugin, you can add that mmdb file extension as an additional extension not to be filtered:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>mmdb</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>

Run files terminating in "Spec" as tests

Whenever I run mvn test in my Maven project, only the test files ending in Test are executed.
However, all my test files in the current project are ending in Spec and I don't want to change their names.
So how can I configure maven to execute files ending in Spec as well?
By default, the Maven Surefire Plugin will automatically include files respecting these patterns:
**/Test*.java
**/*Test.java
**/*TestCase.java
To add more patterns, and specifically yours, just configure the plugin in the following way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
See also the complete documentation about this kind of configuration.

Spring Boot Maven - Include native library

In my project i'm using Sigar library that require some native library. I would like to include all the files (.ddl, .so etc - that are platform specific) inside the JAR or, in the same directory where the jar will be run.
Sigar search those library in the java.library path, but i cant include all of them in the system PATH (my application will be executed in some production machines, and i can't touch the PATH).
In order to do this, i've this build phase in my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<workingDirectory>target</workingDirectory>
<argLine>-Djava.library.path=${baseDir}/src/main/resources/lib</argLine>
</configuration>
</plugin>
</plugins>
</build>
When i run the pack goal (that is included in spring-boot-maven-plugin) and i run the jar (with java -jar myJar.jar) i got this error:
Unable to open nested entry 'lib/libsigar-amd64-freebsd-6.so'. It has
been compressed and nested jar files must be stored without
compression. Please check the mechanism used to create your executable
jar file
Someone can tell me how i can build this JAR ? I've just used spring-boot-maven-plugin because is included in Spring Boot Starter project.

maven-surefire-report-plugin generates report not in right format

I started to use maven-surefire-report-plugin for generating test reports.
My project is composed of parent and two children.
--helloworld--pom.xml
--app1--pom.xml
--app2--pom.xml
app1 generates sources for app2 and app2 contains sources for creating war including tests.
Parent pom.xml file looks
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.7.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
When I run
mvn verify
it builds the project and run tests in the integration phase which is correct. If I browse the app2/target/site it contains report,but a little corrupted. The problem is that css and images folders are missing and that is why the report looks incomplete.
Second problem is when I run
mvn site
it generates html project reports for every folder (helloworld,app1,app2), but they are not linked correctly. I mean when I open index.html in parent folder and want to click on link app1 it gets lost,because it points wrongly. Is there any solution how to overcome these two problems?
Well I am not insisting on maven surefire report plugin for generating reports for test. Thanks in advance!

IntelliJ: Copy war file after build

I'm using IntelliJ + Maven to generate war files.
The war is always generated in ProjectDirectory/target/projectname-version.war
After the build process is done, I want to copy the generated war file into a different location (something like cp output X:/remote/tomcat_webapps/projectname.war).
I already tried to configure the directory where maven builds the project (within the pom.xml). However, maven always deletes the containing folder and all its contents, so that is not an option.
How can I automatically copy the generated war file into a different location?
you can modify the maven war plugin in your pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>X:/remote/tomcat_webapps</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
I'm not quite sure, if it is the outputDirectory or should it be webappDirectory, like in the documentation
https://maven.apache.org/plugins/maven-war-plugin/usage.html

Resources