41

After reading Spring documentation and some other articles on web, I am still confused what is the difference between Spring Boot Maven plugin's spring-boot:repackage and a regular mvn package.

I've thought that mvn package creates a jar with all dependencies included, so what is really the reason to use the plugin by Spring?

2
  • 8
    I've thought that mvn package creates a jar with all dependencies included. <- this is wrong, mvn package only packages the module resources and classes. Commented Mar 21, 2017 at 8:24
  • 3
    Adding to @Tome 's response: "..unless your POM is a child of spring-boot-starter-parent", in which case mvn package will run the spring-boot:repackage goal thus creating the fat JAR. Commented Jun 22, 2020 at 13:29

3 Answers 3

27

The maven package goal and the spring-boot:repackage goal are different in nature. The spring-boot repackage goal is mainly intended to make a JAR or WAR executable from the command line itself using java -jar *.jar while the maven package goal take the compiled code and package it in its distributable format, such as a JAR.It is the spring-boot repackage goal that repackages the JAR produced by maven to specify the main class and make it executable using an embedded container.

Maven Package

  • The first, and most common way, to set the packaging for your project via the equally named POM element . Some of the valid packaging values are jar, war, ear and pom. If no packaging value has been specified, it will default to jar.

  • When a package is defined,each packaging contains a list of goals to bind to a particular phase ,the jar packaging will bind the following goals to build phases of the default lifecycle : process-resources,compile,process-test-resources,test-compile,test,package,install,deploy.

Spring-boot:repackage

Plugin to be included is :

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.4.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 

The configuration repackages a jar or war that is built during the package phase of the Maven lifecycle.

So,Once spring-boot-maven-plugin has been included in your pom.xml, it automatically tries to rewrite archives to make them executable by using the spring-boot:repackage goal. You should configure your project to build a jar or war (as appropriate) by using the usual packaging element.

Reference : https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

Sign up to request clarification or add additional context in comments.

1 Comment

Why it will automatically bind to package phase if set the repackage goal here?
6

Spring repackage a jar or war that is built during the package phase of the Maven lifecycle. The following example shows both the repackaged jar, as well as the original jar, in the target directory:

$ mvn package $ ls target/*.jar target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original 

If you don’t include the configuration, you can run the plugin on its own (but only if the package goal is used as well). For example:

$ mvn package spring-boot:repackage $ ls target/*.jar target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original 

See more details from spring website using the link

2 Comments

If you use the <packaging>jar</packaging> directive, you can generate an executable self-sufficient jar: java -jar target/mymodule-0.0.1-SNAPSHOT.jar
sorry, I didn't get the difference here...so you jave the same output in both cases?
3

mvn package creates a jar or war.

The spring boot plugin takes that jar or war and repackages them to make them executable from the command line (i.e. no app server needed).

From the plugin docs:

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

1 Comment

Ok, so I thought that this is done by regular package as well. Am I correct now, that you cannot just java -jar a regular package? What else should be done with that regular package in order for it to be executable? What is it that spring-boot plugin does to that regular package exactly? I am sorry if I seem confused I just didn't understand it from their documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.