<testResources> ignored on junits - spring-boot

I must be going wrong somewhere - but when i add snippet below and run a junit, i am not seeing the classpath getting updated to include the directory. Eg. I am doing a getClass().getResource("..") and getting a NPE. I was doing the same flow in standard spring apps with no issues, and only noticed after migrating to boot.
Is test resource supported when running mvn surefire:test ? Eg. I have some stub results that I used only in junits, and prefer to package next to the code that is leveraging rather then another folder.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> </configuration>
</plugin>
</plugins>
<!-- support for eclipse and maven junit -->
<testResources>
<testResource>
<directory>${project.basedir}/src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
</build>

Related

How can I exclude application.properties from fat JAR using Maven?

I try to build a Spring Boot project, using Maven (latest versions). It makes no sense to include my dev settings in a JAR that is aimed to be deployed in production.
The question is: How can I exclude application.properties from the resulting fat JAR?
If there are better approaches to this problem, feel free to give some hints.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<!-- Exclude application.properties (?) -->
</plugin>
</plugins>
</build>
You could configure an exclusions of parts of your resources like this:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*-dev*.properties</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
you need to customize the excludes depending on your requirements.

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

Why Spring Boot parent starter modifies surefire defaults?

I'm migrating existing application to Spring Boot. To simplify project configuration I decided to setup spring-boot-starter-parent as parent.
The problem is that surefire configuration
<configuration>
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
is different than default surefire settings and broke my build.
How can I "undo" includes/excludes from Spring Boot? Not overwrite, but go back to surefire defaults.
I think that spring-boot-starter-parent should be surefire-neutral.
PS. Sooner or later I will solve my problem myself. Current issue is rather question to Spring Boot
I restored surefire defaults for includes and excludes with
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
<excludes>
<exclude>**/*$*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Maven war packaging, include classes ignores excluded resources

I have the following pom
<project>
....
<packaging>war</packaging>
....
<build>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<warSourceExcludes>WEB-INF/sass/**</warSourceExcludes>
<attachClasses>true</attachClasses>
<webResources>
<resource>
<directory>src/main/resources/config</directory>
<excludes>
<exclude>**/*</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
...
</project>
As you can see, I package a WAR while my .class files are not in my WEB-INF/classes folder - they are packaged into a JAR instead.
Now, I am desperately trying to exclude some resources from my JAR - but it does not work. When I run mvn jar:jar - the resources are excluded, however when I run mvn package the resources are there.
Please help.
It seems that #user944849 is right - indeed, the war plugin does not use the jar plugin in order to achieve the JAR packaging.
However, his answer gave me a wrong result still as it will simply create 2 jars - one will be with the resources and the other without. The WAR will still use the wrong one.
The correct answer is to use the new maven resources tag.
The one that corresponds to my configuration looks as follows
<build>
....
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
....
</build>
You do not have the jar:jar goal bound to a lifecycle phase. When you run jar:jar from the command line, the exclusions happen fine; when you run mvn clean package I suspect jar:jar is not executing. Try binding the goal to a phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals><goal>jar</goal></goals>
<phase>prepare-package</phase>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
FYI, the archiveClasses feature of the war:war plugin goal does something similar to what I think you're trying to achieve without requiring a separate plugin config.

exclude file from maven build

I've a web application with file src/main/webapp/META-INF/context.xml which contains some configuration for testing database. On production server this file is in $TOMCAT_HOME/conf/Catalina/localhost/ROOT.xml and I'm testing with embedded tomcat so I don't want to package this file. I'd like to exclude this file from maven build. I tried following:
<build>
...
<resources>
<resource>
<directory>src/main/webapp/META-INF</directory>
<filtering>true</filtering>
<excludes>
<exclude>context.xml</exclude>
</excludes>
</resource>
</resources>
</build>
and also following:
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>src/main/webapp/META-INF</directory>
<filtering>true</filtering>
<excludes>
<exclude>context.xml</exclude>
</excludes>
</resource>
</resources>
</configuration>
</plugin>
</build>
But the file still gets packaged in war and in build directory (eg. target/myapp-1.0-SNAPSHOT/META-INF/context.xml). What am I doing wrong?
You can try using the packagingExcludes parameter
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>META-INF/context.xml</packagingExcludes>
</configuration>
</plugin>
To exclude a resource from build, the first snippet in the question looks fine, except that the absolute path of the resource directory should be specified. For instance
<directory>${basedir}/src/main/webapp/META-INF</directory>
The others have answered the main question but one other detail I noticed from your original attempted solution -
<filtering>true</filtering>
In Maven, "resource filtering" doesn't mean what you probably think it means. It's not about including/excluding resources but rather whether they should be processed to fill in embedded variable references.
See http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
Recently I had a similar problem with persistence.xml. Try putting this into your POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>META-INF/context.xml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
If it does not help, try replace maven-jar-plugin with maven-war-plugin.

Resources