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?
This is because the spring-boot:repackage goal takes the existing JAR or WAR archive as the source and repackages all the project runtime dependencies inside the final artifact together with project classes. In this way, the repackaged artifact is executable using the command line java -jar JAR_FILE. jar.
Spring is used for dependency injection. Maven is used to supply dependencies.
When you run mvn spring-boot:run , you launch a Maven build that will: Run the test-compile lifecycle goals, by default it will be resources:resources , compiler:compile , resources:testResources , compiler:testCompile goals of the Maven Resources and Compiler plugin.
The Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”. To use it, you must use Maven 3.2 (or later).
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With