How to use spring boot making a common library

The Spring documentation addresses this concern exactly and shows the correct way of implementing a common library with/for Spring boot:

https://spring.io/guides/gs/multi-module/

As the documentation states: Although the Spring Boot Maven plugin is not being used, you do want to take advantage of Spring Boot dependency management.


Spring Lemon would be a good example for this. It uses Spring Boot, and is meant to be included in other Spring Boot applications. This is what we did to create it:

  1. Created a Spring Boot application, using the Spring Boot Starter Wizard of STS.
  2. Removed the generated application and test class.
  3. Removed spring-boot-maven-plugin, i.e. the build and the pluginRepositories sections in pom.xml. (See how a pom.xml would look without these sections).

I had a similar need as yours, so far I managed to build a library usable on other projects with following configuration:

`

<modelVersion>4.0.0</modelVersion>
<groupId>mx.grailscoder</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<name>My Custom Library built on Spring Boot</name>
<description>Spring Boot Project library</description>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <logentries-appender>RELEASE</logentries-appender>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

`

It's important to mention I skipped the repackage task since my library didn't have any main class, then issuing the mvn install task does not fail.