Why Spring Boot parent starter modifies surefire defaults? - spring-boot

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>

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

<testResources> ignored on junits

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>

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.

How to transform web.xml during packaging with maven?

I am porting an ant script to maven, and I am stuck on transforming my web.xml.
My web.xml has the following security constraint set to NONE during development, and to CONFIDENTIAL for the war that is produced for the production server.
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPSOnly</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
I use an XSL script to process web.xml file which is called from ant. I want maven to produce war file where the XSL has been applied to the web.xml and the transformed web.xml is used in the generated war.
I know about the maven XSL plugin and the concept of maven profiles but I am struggling to put it all together and I am not sure what the right maven way is for dealing with situation.
There are many ways you can achieve this. Probably the simplest way is to use maven-antrun-plugin http://maven.apache.org/plugins/maven-antrun-plugin/ and call your existing ant task
Other ways I know is to use a variable / placeholder and Maven filtering (variable substitution), have a look at: http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html
The Maven way is to use filtering.
Example below of regular web.xml filtering, without xsl:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
<filters>
<filter>src/main/filters/filter-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
If you insist on xsl, you could use Maven xml plugin, working from the base of the example:
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/xml</dir>
<stylesheet>src/main/stylesheet.xsl</stylesheet>
</transformationSet>
</transformationSets>
</configuration>
</plugin>
...
</plugins>
</build>
I solved my problem my setting up a prod profile with the pom.xml for the web app, with the following plugins in it. This works for me because when the project is imported into eclipse I really don't want anything fancy to happen and I want everything to work out of the box the developer machine. It is not shown below but I also added a call to NodeJS to build the javascript front end using RequireJS at the generate-sources phase.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>transform</goal>
</goals>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/webapp/WEB-INF</dir>
<includes>
<include>web.xml</include>
</includes>
<stylesheet>src/main/webapp/WEB-INF/web.xsl</stylesheet>
</transformationSet>
</transformationSets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>target/generated-resources/xml/xslt/web.xml</webXml>
<archiveClasses>true</archiveClasses>
<warSourceExcludes>META-INF/context.xml</warSourceExcludes>
<packagingExcludes>
**/**/context.xml
</packagingExcludes>
</configuration>
</plugin>

Resources