Import spring boot app into another project
Asked Answered
D

2

13

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

Dovelike answered 1/11, 2016 at 19:50 Comment(1)
"If I look inside the jar" <- are you referring to the jar that is built by maven? Can you post your pom.xml. You are building the jar with mvn install?Touristy
D
15

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>
Dovelike answered 1/11, 2016 at 19:59 Comment(2)
Worked for me. It saved my day :)Hexagonal
Do I import the project on the ClassPath of the second project or not?Cathouse
E
0

If you are using Spring Boot Starter Parent dependency then simply adding a repackage goal will not work. Because Spring automatically adds a repackage goal with ID repackage. This built in goal must be overridden with the classifier EXACTLY as below

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

Reference to official documentation

Expedient answered 27/4 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.