Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a dependency to a Spring Boot Jar in another project?

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?

like image 278
sparrow Avatar asked Oct 17 '16 14:10

sparrow


People also ask

How will you add one project as dependency in another project?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.


1 Answers

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.

like image 116
Tunaki Avatar answered Sep 20 '22 08:09

Tunaki