3

Recently, I wrote a Spring-Boot project and I wanted that Maven will be able to create a jar file that I'll be able to run by the command "java -jar ".

This is the pom.xml I wrote:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>SpringBootGame</groupId>
    <artifactId>SpringBootGame</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>    
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.3.RELEASE</version>
                <configuration>
                    <mainClass>com.game.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

In order to build the jar file, I had to run the command: "mvn clean package spring-boot:repackage".

My questions are:

  1. Why must I add spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?

  2. Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?

Thanks

1

2 Answers 2

3

Why must I add spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?

Because the plugin is what adds Maven support for Spring Boot

Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?

It looks like you are missing the <packaging>jar</packaging> element in between your <project> </project> element.

The reason you had to run that lengthy command, is because you did not include the <executions></executions> element when including the plugin. Please see section 71.1 of the following docs:

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

71.2 elaborates on the <packaging> element.

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

Comments

-1

In the configuration of the maven plugin you have already in place, you need to add the executable block like :

<configuration>
   <executable>true</executable>
</configuration>

this will create in the target folder the runnable jar that can be run by using java -jar command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.