Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use spring boot making a common library

People also ask

Can I use a spring boot jar as a dependency?

You cannot use a spring-boot-plugin-manipulated-jar as a "normal" dependency, as its structure has been changed to be "startable" as a standalone JAR for Spring.

Is it possible to use spring boot in the same way as any standard Java library?

You can use Spring Boot in the same way as any standard Java library. To do so, include the appropriate spring-boot-*. jar files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor.

Is Springboot a framework or library?

A framework is a known programing environment, such as Spring Boot. When a software lead sets out to build a new enterprise application, they must decide which set of libraries and frameworks they want to use.


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

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.


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.