How to use spring boot making a common library

JavaSpringSpring Boot

Java Problem Overview


Now I want to develop a common mail service for our systems. As we design, we want to develop a RabbitMQ producer and consumer. On consumer side, we could develop and deploy a Spring Boot or Spring Cloud application, but on producer side we want to give a common mail client like the interface below and make a jar dependency for other system.

interface MailClient {
  ListentableFuture send(Message message);
}

But i see spring boot and spring cloud using many declarative method and seems must use a application class, but i just want a class reference and not need deploy. i do not know how to implement it.

Java Solutions


Solution 1 - Java

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).

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

According to the documentation:

> Spring Boot makes it easy to create stand-alone, production-grade > Spring based Applications that you can "just run".

But Spring Boot is not the silver bullet which matches all situations. If you want to create a library, you can create a regular spring project which builds a jar and stores it in your company's repository, if you have one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrapeBaBaView Question on Stackoverflow
Solution 1 - JavaSanjayView Answer on Stackoverflow
Solution 2 - Javajava-addict301View Answer on Stackoverflow
Solution 3 - Javadarkstar_mxView Answer on Stackoverflow
Solution 4 - JavajnyView Answer on Stackoverflow