Hot questions for Spring Boot Maven Plugin

Top 10 Java Open Source / Spring / Spring Boot Maven Plugin

Question:

I have a Spring Boot application and I have created a Jar out of that. Following is my pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- WebJars -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

I want to use this Jar in my other application so added this jar to my application. But when I am calling a method in that Jar, it is throwing a ClassNotFoundException.

How can I fix this issue? How can I add a dependency to a Spring Boot JAR?


Answer:

By default, Spring Boot repackages your JAR into an executable JAR, and it does that by putting all of your classes inside BOOT-INF/classes, and all of the dependent libraries inside BOOT-INF/lib. The consequence of creating this fat JAR is that you can no longer use it as a dependency for other projects.

From Custom repackage classifier:

By default, the repackage goal will replace the original artifact with the repackaged one. That's a sane behaviour for modules that represent an app but if your module is used as a dependency of another module, you need to provide a classifier for the repackaged one.

The reason for that is that application classes are packaged in BOOT-INF/classes so that the dependent module cannot load a repackaged jar's classes.

If you want to keep the original main artifact in order to use it as a dependency, you can add a classifier in the repackage goal configuration:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>1.4.1.RELEASE</version>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <classifier>exec</classifier>
      </configuration>
    </execution>
  </executions>
</plugin>

With this configuration, the Spring Boot Maven Plugin will create 2 JARs: the main one will be the same as a usual Maven project, while the second one will have the classifier appended and be the executable JAR.

Question:

I'm trying to load properties from pom.xml into application.properties. I want to create two profiles: dev and prod to use different database urls. I'm using Jenkins as CI, in all my apps (Spring MVC mainly, without Boot project) are using maven profiles to deploy to Tomcat. There is any posibility to do this using maven properties? I tried something like that: spring.datasource.url=${jdbc.url}


Answer:

Before you do it, consider externalizing the properties file out of your deployable package. This way you can deploy the same compilation on every environment. It will save your Jenkins some work that is actually unnecessary. The best practice is to build your application only once, however, if you are not convinced, here is how to do it.

  1. In your pom.xml define the profiles with appropriate values for the property.

    <profile>
        <id>dev</id>
       <properties>
           <jdbc.url>your_dev_URL</jdbc.url>
       </properties>
    </profile>
    
  2. Setup the Maven Resources Plugin to filter the directory which contains your application.properties file.

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        ...
    </build>
    
  3. If you use Spring Boot 1.3 or more, you should be aware of the fact that to avoid conflicts between Spring Boot placeholders and tokens filtered by the Maven Resources Plugin, the framework introduced a solution that requires using a different syntax for filtered values.

    Now, instead ${property.key} you should use @property.key@. In this case, your application.properties must contain the following sample to work as you expect:

    spring.datasource.url=@jdbc.url@
    

You can also check out a post about separating Spring properties files for different Maven profiles. That way you will externalize the values from your pom.xml.

Question:

Reload of multi-module maven project changes

Setting

Imagine a multi-module maven-project. The project structure is:

pom.xml //parentpom
   |
  pom.xml //submodule_1
   |
  pom.xml //submodule_2
   .
   .
   .
  pom.xml //submodule_7

For example submodule_5 has submodule_6 and submodule_7 as dependencies. The submodule_5 can be build to construct a War-file which can be deployed. Spring-Boot-Devtools provide the feature of automatic-restart whenever there is a change to submodule_5 it's classpath.

Whenever the application is run using:

mvn spring-boot:run

And changes are made to submodule_5 (depending on which IDE you use the classpath get changed. (for Eclipse automaticaly / for InteliJ when pressing Ctrl+F9)) spring-boot automaticaly restarts the application and changes are added. Changes which happen to submodule_6 or submodule_7 don't trigger the automatic restart.


Questions
  1. Is there a way to make it so that whenever you make changes in submodule_6 or submodule_7 to have them force a restart and there-for apply the changes?
  2. Spring-boot-devtools uses two classloaders: "The Base Classloader" & "The Restart Classloader". Is it so that on initial start of the application submodule_6 and submodule_7 get added to "The Base Classloader" whilst submodle_5 is kept in the "The Restart Classloader"? Making it so that whenever submodule_5 forces a restart it uses the versions of submodule_6 and submodule_7 out of "The Base Classloader"?

Answer:

You may specify additional folders to be watched by spring-boot-devtools, in application.properties:

spring.devtools.restart.additional-paths=../submodule_6,../submodule_7

See Spring's documentation on using-boot-devtools-restart-additional-paths.

Question:

In my project I want to use environment specific property file. For example if I am running it into development it should use application.dev.properties, for production it should use application.prod.properties and so on.

I have below two files in my resources folder.

  1. application.properties (For production)
  2. application.dev.properties (For development)

I have one properties like below in each file.

For Prod

server.database.host=192.168.1.1

For Dev

server.database.host=192.168.12.125 

And I have a class like below

 public class DataSource {

     @Value(${server.database.host})
     String host;

The above code always takes prod setting (application.properties) file even though I supply proper argument for dev like --spring.profiles.active=dev

Below is the command I am using to load the dev properties file.

java -jar myjar.jar --spring.profiles.active=dev

It also prints that active profile is dev but it always connect to prod db.


Answer:

A few issues I noticed:

  1. @Value property name should be a String like @Value("${server.database.host}")
  2. Profile specific property files should follow application-{profile}.properties format, e.g. application-dev.properties
  3. Try passing the profile with -D like java -Dspring.profiles.active=dev -jar app.jar

Question:

When running my jar file : java -jar target/places-1.0-SNAPSHOT.jar I'm getting the next error :

no main manifest attribute, in target/places-1.0-SNAPSHOT.jar

My pom.xml contains the spring-boot-maven-plugin :

 <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.places.Main</mainClass>
            </configuration>
 </plugin>

I also tried to create a MANIFEST.MF FILE and specifying the class but it didnt help.

In addition, I also tried :

<properties>
      <!-- The main class to start by executing "java -jar" -->
      <start-class>com.places.Main</start-class>
</properties>

My main :

@SpringBootApplication
 public class Main {
public static void main(String[] args) throws InterruptedException {
    SpringApplication.run(Main.class,args);
  }
  }

any idea what else can I try ?


Answer:

During the Maven package phase, the jar archive is enhanced by Spring Boot Maven Plugin and the original jar file (taht should haven been built using the standard maven-jar-plugin) is replaced with an enhanced executable jar.

Hence you have either to issue the spring-boot:repackage goal yourself when building your module:

mvn package spring-boot:repackage

Or add the goal explicitly within the plugin configuration:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.places.Main</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

You can find more details about the Spring Boot Maven Plugin repackage goal within the official documentation.

Question:

I am using Spring-Boot v1.3.0.M5 with Maven v3.3.3. I used to be able to run my Spring Boot (boot) application from the console with this command.

mvn clean package spring-boot:run

However, I've had to revise my pom.xml to account for different environment builds. In particular, I am using Maven profiles to modify the properties files of boot application. Now when I run the previously mentioned command, the boot application fails to run and complains with the following exception.

Caused by: java.lang.NumberFormatException: For input string: "${MULTIPART.MAXREQUESTSIZE}"

I have a properties file located at src/main/resources/config/application.properties. And this properties file has a bunch of key-value pairs which looks like the following.

multipart.maxFileSize=${multipart.maxFileSize}
multipart.maxRequestSize=${multipart.maxRequestSize}

Then in my pom.xml, my build is defined as follows.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/*.properties</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<profiles>
    <!-- development -->
    <profile>
        <id>env-dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>env</name>
                <value>dev</value>
            </property>
        </activation>
        <properties>
            <multipart.maxFileSize>250MB</multipart.maxFileSize>
            <multipart.maxRequestSize>250MB</multipart.maxRequestSize>
        </properties>
    </profile>
    <!-- staging -->
    <profile>
        <id>env-stg</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <property>
                <name>env</name>
                <value>stg</value>
            </property>
        </activation>
        <properties>
            <multipart.maxFileSize>500MB</multipart.maxFileSize>
            <multipart.maxRequestSize>500MB</multipart.maxRequestSize>
        </properties>
    </profile>
<profiles>

I noticed that if I type in mvn clean package and look inside the jar file, the application.properties file is inside the jar.

However, if I type in mvn clean package spring-boot:run, then the applications.properties file is not inside the jar. In fact, nothing under src/main/resources makes it into the jar file.

This problem is a little annoying for me because if I want to run my boot application from the command line, I have to do two steps now.

  1. mvn clean package
  2. java -jar ./target/app-0.0.1-SNAPSHOT.jar

Any ideas on what I am doing wrong?


Answer:

As described in the documentation mvn spring-boot:run adds src/main/resources in front of your classpath to support hot reload by default. You can turn this off easily

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>1.2.7.RELEASE</version>
      <configuration>
        <addResources>false</addResources>
      </configuration>
    </plugin>
    ...
  </plugins>
  ...
</build>

Question:

I'm using spring-boot-maven-plugin to package my REST service. I'm building the jar using mvn clean install or mvn clean package. After I decompile the jar, I don't find any of the dependencies added (I was expecting it to be a fat jar with all dependencies)

 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.9.RELEASE</version>
    <executions>
        <execution>
           <phase>install</phase>
           <goals>
              <goal>repackage</goal>
              <goal>build-info</goal>
           </goals>
        </execution>
    </executions>
    <configuration>
        <executable>true</executable>
        <finalName>myapp</finalName>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

When I run the spring boot using java -jar myapp.jar -Drun.jvmArguments="-Dspring.profiles.active=qal" I'm getting ClassNotFoundException for many of the classes. It's clear that artifact didn't build as expected. However, if I start spring boot application using maven ./mvnw spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=qal" I guess, it finds all the dependencies in target folder hence works fine. How can I fix the build issue so that I can start app using java -jar command.

EDIT: It's multi-module maven project


Answer:

it seems you are using a wrong command. mvn clean package is maven command, you should use command 'repackage', it used for

Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar

as it mentioned here https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html

Or probably it's plugin configuration issue. Just checked: it works with spring-boot-maven-plugin-2.0.0.RELEASE

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                 <classifier>exec</classifier>
            </configuration>
         </execution>
    </executions>
</plugin>

Question:

For my Spring boot application with embedded tomcat, due to some limitation I need to do away with spring-boot-maven-plugin and need to use maven-shade-plugin. With maven package command I could successfully create the jar file. However, all my controllers stopped working and gives me 404 error. Also static content placed in my resources folder are not being served anymore. Always getting 404 error.

my pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.2.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies> 

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>

            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>

                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>abc.MyMainClass</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>


    </plugins>
</build>

I run the application as usual:

@SpringBootApplication
public class MyMainClass{

    public static void main(String[] args) {
        SpringApplication.run(MyMainClass.class, args);
    }   
}

Also I had to create following EmbeddedServletContainerFactory bean, otherwise an error will be thrown.

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    return tomcat;
}

Finally, I just have a one sample controller,

@RequestMapping(value = "/car", method = RequestMethod.GET)
@ResponseBody
public Car test() {
    return new Car();
}

This same code works without any issue when I use spring-boot-maven plugin. Soon after I started using maven-shade-plugin, tomcat boots up successfully. however, all controllers gives me 404 error. Please guide me if I'm doing anything wrong.


Answer:

It is very simple if you want to migrate from spring-boot-maven plugin to maven-shade plugin. I've added following plugin information to pom.xml and problem resolved.

(https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.2.6.RELEASE/spring-boot-starter-parent-1.2.6.RELEASE.pom)

Dependency management:

    <dependencyManagement>
<dependencies>
    <dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.2.7.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>

Build plugins:

<build>


    <plugins>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>1.2.7.RELEASE</version>
                </dependency>
            </dependencies>
            <configuration>
                <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                <resource>META-INF/spring.factories</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>sg.butterfly.emenu.api.config.EmenuApp</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

Question:

NOTE: Please, before marking this question as a duplicate make sure you know the difference between executable JAR and fully executable SpringBoot JAR.

The official Spring Boot documentation describes how to build fully executable JAR. Then generated JAR file can be linked from /etc/init.d/ and started/stopped/restarted/statused as a normal unix service without additional scripts or tools like JSVC.

But the generated JAR contains all libraries and can be big enough in size (in my case 70Mb+).

I want to generate such fully executable JAR without libraries, but then to be able to run it as SystemV service on Linux and link external libraries (JARs) somehow.

UPDATE

I want to reduce the artifact size in order to speed up deploy->test->fix cycle. Sometimes I'm working via mobile network and big file size can decrease my job speed dramatically.

In case there is no a simple configuration property or a profile or a command line option I would use a kind of hack.

At the beginning, I can generate a build containing all dependencies. Then I can unzip it and move all libraries to a special folder.

Then I need to pack it again as fully executable somehow and run with pointing to the folder with libraries.

I don't think this can be done with jar utility because file utility recognizes fully executable jar as data

$ file fully-executable.jar
file fully-executable: data

unlike the usual jar

$ file usual.jar
usual.jar: Java Jar file data (zip)

Answer:

You may want to consider using Spring Boot Thin Launcher. It creates a jar file with your application code but none of its dependencies. It adds a special thin launcher that knows how to resolve your application's dependences from a remote Maven repository or from a local cache when the jar is executed. Judging by the description of what you want to do, you'd utilise the local cache option.

The configuration of Spring Boot's Maven plugin to produce a fully executable jar that uses the thin launcher looks like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot.experimental</groupId>
                    <artifactId>spring-boot-thin-layout</artifactId>
                    <version>1.0.3.RELEASE</version>
                </dependency>
            </dependencies>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

Question:

So I am attempting to add a spring boot executable jar as a dependency in another project (Testing framework).

However once added to the pom and imported. Java imports don't work properly. If I look inside the jar all packages are prepended with:

BOOT-INF/classes.some.package.classname.class

There is also some spring boot related packages, MANIFEST etc etc.

Not if I switch the spring boot app's build to just install and deploy a regular jar using the spring-boot-maven-plugin

This changes and everything works fine. Unfortunately this is not a solution for us as we lean on the executable jar as part of our release process.

Can I build a deploy both versions of the jar and use a classifier to determine each?

Thanks


Answer:

Turns out this exact scenario can be achieved using the spring-boot-maven-plugin.

Spring boot app's pom:

  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.4.1.RELEASE</version>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
        <configuration>
          <classifier>exec</classifier>
        </configuration>
      </execution>
    </executions>
    ...
  </plugin>

project using the spring boot jar can be added as normal:

    <dependency>
        <groupId>com.springboot</groupId>
        <artifactId>app</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>

OR if you want to reference the executible jar

    <dependency>
        <groupId>com.springboot</groupId>
        <artifactId>app</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <scope>test</scope>
        <classifier>exec</classifier>
    </dependency>

Question:

Example

Is it possible to somehow configure the spring boot maven plugin to include resources from a dependency.

E.g. if in my spring boot project I have:

<dependency>
        <groupId>co.company</groupId>
        <artifactId>example</artifactId>
    </dependency>

and in that JAR file there is a properties file such as example.properties

jar -tf example.jar | grep example.properties | wc -l

results in 1.

However, when I build the spring boot JAR, this properties file is not added to the src/main/resources. The JAR containing it is included in BOOT-INF/lib/example.jar

However, in my case. I'd like to extract the content of src/main/resources out into the boot BOOT-INF/classes/ directory of the spring boot JAR so that things like auto configuration can pick it up.

Real World

In the real world, i'm trying to do this with:

  • thymeleaf templates (e.g. my dependency JAR provides the HTML template files but in the deployed boot jar these templates are not resolved)
  • liquibase changelog files (my dependency includes changelog files but these aren't executed - I presume liquibase autoconfig doesn't find the changelog file because it's not in the src/main/resources of the boot JAR).

Answer:

I think the solution for this problem would be very similar to the solution for another question you asked.

You can use the unpack goal of the maven-dependency-plugin in your Spring Boot module:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>module-a</artifactId>
                        <version>${project.version}</version>
                        <includes>**/*.yaml</includes>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>${project.build.directory}/classes/BOOT-INF/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

That will copy the resources from module-a to BOOT-INF directory of your boot-module. I've posted a more complete example on GitHub.

Question:

I have a spring-boot based application, and the pom.xml file is configured as below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <includes>
                        <include>**/IT*.java</include>
                        <include>**/*IT.java</include>
                        <include>**/*ITCase.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

The main method located in class DemoApplication as below

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        System.out.println("This is a demo application+++++++++++++++++++");
        SpringApplication.run(DemoApplication.class, args);
    }
}

And my integration test class called DemoIT as following.

package com.example.demo;

import org.junit.Test;

public class DemoIT {

    @Test
    public void test() {
        System.out.println("This is a integration test.==============");
    }
}

Now here is my question, when I issue mvn clean verify command, the integration class DemoIT is supposed to be executed, and it does. However, my DemoApplication isn't running. So I'm wondering if my integration test needs to be executed under the spring-boot application context (the DemoApplication needs running), what should I do to make it happen?


Answer:

Here is a document for

Spring Boot Maven Plugin Last Published: 2018-05-09| Version: 2.0.2.RELEASE

It says

While you may start your Spring Boot application very easily from your test (or test suite) itself, it may be desirable to handle that in the build itself. To make sure that the lifecycle of you Spring Boot application is properly managed around your integration tests, you can use the start and stop goals as described below:

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.0.2.RELEASE</version>
      <executions>
        <execution>
          <id>pre-integration-test</id>
          <goals>
            <goal>start</goal>
          </goals>
        </execution>
        <execution>
          <id>post-integration-test</id>
          <goals>
            <goal>stop</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
  ...
</build>

For who not familiar with integration test, I found this answer also quite helpful.