How to set up an environment variable in mvn pom? - maven

How can i set up an environment variable (in other words internally accessible by System.getenv("APP_HOME") in a pom file?
APP_HOME=/path/home
I realize i can set it up in .profile, but wonder if pom can do the same trick.
Per bmargulies's suggestion below, i tried the following, without luck
<build>
<finalName>KvpStore</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<includes>
<include>**/*Test*.java</include>
</includes>
<environmentVariables>
<APP_NAME>blah_blah</APP_NAME> <------------------------
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>

Some plugins, like surefire, let you set them. There's no way, in general, in the pom.
The doc for surefire is is here. Surefire will set environment variables for the duration of the run of the tests, not for anything else. And you have to make surefire fork.
In the configuration ...
<configuration>
<forkMode>always</forkMode>
<environmentVariables>
<var1>val1</var1>
</environmentVariables>
</configuration>

The documentation of the maven-surefire-plugin show examples and describes how to do such things of setting up system properties.
<configuration>
<systemPropertyVariables>
<propertyName>propertyValue</propertyName>
<buildDirectory>${project.build.directory}</buildDirectory>
[...]
</systemPropertyVariables>
</configuration>
It might be better to use them instead of environment variable, cause it's simpler to use them, cause env variable needed to setup correctly and the cmd.exe and the jvm must be restarted to get them working.
It is not necessary to configure the includes for the tests, cause maven-surefire-plugin has already the following defaults:
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>

Related

How can I exclude certain overlay-derived directories from my war using maven?

We build a webapp using maven 3.5.0 and the maven-war-plugin. We have many overlays, many of which have a tests directory that contains JSP files used for testing certain things in a dev environment. We don't want these included in our war.
We tried the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<overlays>...</overlays>
<warSourceExcludes>tests</warSourceExcludes>
</configuration>
</plugin>
This didn't work, nor did a couple of configuration variations:
<packagingExcludes>tests</packagingExcludes>
and
<packagingExcludes>**/${project.artifactId}/tests/**</packagingExcludes>
Is this due to my naive misuse of configuration options, or does it have to do with how overlays are processed?
You should use <excludes>..</excludes> (see https://maven.apache.org/plugins/maven-war-plugin/overlays.html) instead of <packagingExcludes>..</packagingExclude>...
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<overlays>
<overlay>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<excludes>
<exclude>WEB-INF/classes/images/sampleimage-dependency.jpg</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
The dependentWarExcludes element worked to eliminate the tests directories and their contents:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<overlays>...</overlays>
<dependentWarExcludes>tests/**</dependentWarExcludes>
</configuration>
</plugin>

Maven + Surefire: Overwrite temp and output directory

I am trying to overwrite temporary and output directory for surefire plugin. I have following plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<tempDir>c:/tmp/a-tests-temp</tempDir>
<outputDirectory>c:/tmp/a-tests-out</outputDirectory>
</configuration>
</plugin>
It does not work. It continue using /target/surefire and target/surefire-reports.
The idea is to split different concurrency maven test runs over the same project directory for quick tests on the developer workstation.
Sorry for late reply, ive only managed to use relative path location to change the path for the temp folder
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<testClassesDirectory>${project.buildDirectory}/test-classes</testClassesDirectory>
<reportsDirectory>${project.buildDirectory}/surefire-reports</reportsDirectory>
<classesDirectory>${project.buildDirectory}/test-classes</classesDirectory>
<tempDir>../${project.buildDirectory}/surefire</tempDir>
<useFile>false</useFile>
<includes>
<include>**/*Spec.*</include>
</includes>
<systemPropertyVariables>
<geb.env>${geb.env}</geb.env>
<geb.build.reportsDir>${project.buildDirectory}/${geb.build.reportsDir}</geb.build.reportsDir>
<com.athaydes.spockframework.report.outputDir>${project.buildDirectory}/spock-reports</com.athaydes.spockframework.report.outputDir>
<com.athaydes.spockframework.report.aggregatedJsonReportDir>${project.buildDirectory}/spock-reports</com.athaydes.spockframework.report.aggregatedJsonReportDir>
</systemPropertyVariables>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

How to implement Integration test using not current Spring 4.0.7 RELEASE version

Welcome,
We are trying to add integration tests to our Spring project.
Here are listed current Maven dependencies.
-Spring Version: **4.0.7 RELEASE**
-JUnit: **4.8**
Unit tests already exist. They are executed with: #RunWith(SpringJUnit4ClassRunner.class)
We tried to use Maven configuration to exclude IntegrationTests while executing unit tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</plugin>
Unfortunately it worked only partialy, beacuse now integration tests are not executed at all. I would be grateful if anyone could help use solving this issue.
Thank You in advance!
You need to add goal into your failsafe configuration. Please see below: http://maven.apache.org/components/surefire/maven-failsafe-plugin/usage.html

maven-surefire-plugin doesn't run parallel tests

I have a project Serenity+Java+JUnit and I try to run my tests in parallel.
I paste this into my pom and after mvn integration-test, it is still run in a chain :(
What I have done wrong?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<parallel>all</parallel>
<forkMode>perthread</forkMode>
<threadCount>4</threadCount>
</configuration>
</plugin>
Have resolved it by adding another plugin instead of surefire-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<systemPropertyVariables>
<databaseSchema>MY_TEST_SCHEMA_5</databaseSchema>
</systemPropertyVariables>
<workingDirectory>FORK_DIRECTORY_5</workingDirectory>
</configuration>
</plugin>
Not really sure if what your answer suggests is correct. You have placed concurrency configuration in Maven Failsafe, a plugin which is responsible for safely terminating the test execution once an exception has occurred therein. Although, as you claim it this has resolved your issue, it might impact your project in the long run.
According to Maven's website, the below code should be placed under Maven Surefire configuration.
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
Having followed these guidelines, I was able to successfully run tests in parallel with only updating Surefire plugin.

Maven Profile does not run appropriate tests

I have configured a profile for running only integration tests but it is still running all tests.
This is the configuration:
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
I tried with regex like explained at http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html but I got this not working.
How do I get only tests running that end with IT.java?
You have not reconfigured the surefire run, but instead added another one to the lifecycle. That way, surefire:test runs twice, once with default execution id (default-test) and once with your new execution (integration-test)
If you simply want to change the tests being run, you could use:
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Which would only reconfigure the existing execution.
Still, as khmarbaise already wrote in a comment, it makes much more sense to simply use the maven-failsafe-plugin. If you really wanted to skip normal unit test (why would you want that, anyway?), you could explicitly disable surefire in your profile (using skipTests-configuration).

Resources