Difference between spring-boot: repackage and mvn package

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

I thought the mvn package creates a jar with all dependencies included, so what's the real reason for using the plugin under Spring?

+7


source to share


5 answers


Spring repackage jar or war that is generated during the package phase of the Maven lifecycle. The following example shows both a packed jar and a source 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 configuration, you can run the plugin yourself (but only if the package target is used). For example:



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

      

See spring website for more details using link

+3


source


mvn package creates a jar or war.

The spring boot plugin takes this jar or war and repackages it to make it 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 run from the command line using java -jar."

+1


source


Maven package target and spring-loaded target: repackaging are of a different nature. The download repackage target is mainly intended to make a JAR or WAR executable from the command line using java -jar *.jar

while the target maven package

takes the compiled code and packages it into its redistributable format such as JAR .. This is the download repackage target which repackages JAR generated by maven to specify the main class and make it executable using an inline container .

Maven Package

  • The first and most common way is to set the packaging for your project through the POM element of the same name. Some of the valid packaging values ​​are can, war, ear and pom. If no packing value is specified, jar is used by default.

  • When a package is defined, each package contains a list of targets to tie to a specific phase, the jar packing will be tied to the following targets to create the default lifecycle phases: processes-resources, compilation, processes-test-resources, testing compilation, testing, package, installation , deployment.

Spring Boot: Repackaging

Plugin to enable:

<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 config repackages the jar or war file generated during the packaging phase of the Maven lifecycle.

So, after the spring-boot-maven-plugin has been included in your pom.xml, it automatically tries to rewrite the archives to make them executable using the spring-boot: repackage target . You must configure your project to build a jar or war (as the case may be) using a regular packaging item.

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

+1


source


Maven package target and spring-loaded target: repackaging are of a different nature. The purpose of the repackage on download is mainly to make a JAR or WAR executable from the command line using, java -jar *.jar

and the purpose is maven package

to take the compiled code and package it into your redistributable format such as JAR..It is the purpose of the repackage on download, which repackages the JAR. created by maven to specify the main class and make it executable using the built-in container .

Maven Package

  • The first and most common way is to set the packaging for your project through the POM element of the same name. Some of the valid packaging values ​​are can, war, ear and pom. If no packing value is specified, jar is used by default.
  • When a package is defined, each package contains a list of targets to tie to a specific phase, the jar packing will be tied to the following targets to create the default lifecycle phases: processes-resources, compilation, processes-test-resources, testing compilation, testing, package, installation , deployment.

Spring Boot: Repackaging

Plugin to enable:

<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 previous configuration repackages the jar or war generated during the packaging phase of the Maven lifecycle. The following example shows both the repackaged jar and the source jar in the target directory:

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

      

What the above config does is that it takes the original maven generated jar and repackages it to create another jar that can be directly executed from the command line using the command java -jar

. The original jar is not deleted and renamed to .jar.original. Therefore, you can find two banks in the bank.

If you didn't include configuration as shown in the previous example, you can run the plugin yourself (but only if the package target is also used), as shown in the following example:

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

      

In this case, when you build the project, since you did not specify the target in the plugin, you will have to specify it in the command itself, as shown above.

So, after the spring-boot-maven-plugin has been included in your pom.xml, it automatically tries to rewrite the archives to make them executable using the spring-boot: repackage target . You have to set up your project to build a jar or war (depending on the situation) using a regular packaging item,

0


source


If the spring-boot-maven-plugin is present in your pom as shown below:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

      

Then the command is mvn package

exactly the same as spring-boot:repackage

, both will generate an executable jar.

If the maven plugin is not present in your pom then the generated jar will not be executable (normal jar) since mvn package

.

spring-boot:repackage

explicitly specifies the spring-boot-maven-plugin on the command line.

-2


source







All Articles