12

I'm using spring-boot-maven-plugin to package my REST service. I'm building the jar using mvn clean install or mvn clean package. After I decompile the jar, I don't find any of the dependencies added (I was expecting it to be a fat jar with all dependencies)

enter image description here

 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.9.RELEASE</version>
    <executions>
        <execution>
           <phase>install</phase>
           <goals>
              <goal>repackage</goal>
              <goal>build-info</goal>
           </goals>
        </execution>
    </executions>
    <configuration>
        <executable>true</executable>
        <finalName>myapp</finalName>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

When I run the spring boot using java -jar myapp.jar -Drun.jvmArguments="-Dspring.profiles.active=qal" I'm getting ClassNotFoundException for many of the classes. It's clear that artifact didn't build as expected. However, if I start spring boot application using maven ./mvnw spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=qal" I guess, it finds all the dependencies in target folder hence works fine. How can I fix the build issue so that I can start app using java -jar command.

EDIT: It's multi-module maven project

Pankaj
  • 3,512
  • 16
  • 49
  • 83
  • Judging by the screenshot, there are a number of problems with the repackaged jar. Where’s the screenshot from? Also, what do command line tools like `jar` and `unzip` show for the jar’s contents? – Andy Wilkinson Mar 31 '18 at 22:45
  • I used JD-GUI (http://jd.benow.ca/) for unzipping the jar and screenshot is after unzipping using jd-gui – Pankaj Apr 01 '18 at 02:11
  • What do command line tools like jar and unzip show for the jar’s contents? – Andy Wilkinson Apr 01 '18 at 05:29
  • Jars are just zip files with manifest so you can use unzip to see the contents. i.e. unzip -l will show the contents. You can also peruse the contents if you know the path from the list. i.e. This will print the manifest: unzip -p target/scila-adapter-service.jar META-INF/MANIFEST.MF – Big Kahuna Jul 01 '20 at 08:27

2 Answers2

13

it seems you are using a wrong command. mvn clean package is maven command, you should use command 'repackage', it used for

Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar

as it mentioned here https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html

Or probably it's plugin configuration issue. Just checked: it works with spring-boot-maven-plugin-2.0.0.RELEASE

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                 <classifier>exec</classifier>
            </configuration>
         </execution>
    </executions>
</plugin>
JGleason
  • 3,067
  • 6
  • 20
  • 54
krund
  • 740
  • 1
  • 7
  • 16
  • 1
    I used "mvn clean package spring-boot:repackage". I see all project classes are compiled and added to the jar but dependencies are still missing and I continue to get the ClassNotFound error for dependencies – Pankaj Mar 31 '18 at 19:40
  • 2
    Found the reason why it wasn't working. My project is multi module maven project. Need to use classifier as per this documentation. It works with 1.5.9.RELEASE version as well. https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html – Pankaj Apr 04 '18 at 23:18
  • 1
    The link posted in the answer is broken – carlos palma Dec 14 '21 at 14:38
  • Fixed link: [repackage-classifier](https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/maven-plugin/examples/repackage-classifier.html) – Martin P. Mar 22 '23 at 14:14
-2

Use this one

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>${start-class}</mainClass>
    <executable>true</executable>
    <fork>true</fork>
    <!-- Enable the line below to have remote debugging of your application on port 5005
         <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
     -->
  </configuration>
</plugin>
frido
  • 13,065
  • 5
  • 42
  • 56
test1
  • 1