Spring boot command line maven commands

Spring boot command line maven commands

 

How to create a spring boot jar file by using mvn command?

Open windows command prompt, go to the project location i.e where pom.xml exist and execute the following command.

In this process, by default, all test cases are also executed so it will take time to create a jar file. If any test case failed then the jar file will not be generated it means that build failed. See in the below how to skip the test case while build jar file.

Command

D:\Projects\pratush_elipse\SpringBoot-Profile-Example>mvn install

The jar will be generated under the target folder

D:\Projects\pratush_elipse\SpringBoot-Profile-Example>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ----------< com.narayanatutorial:SpringBoot-Profile-Example >-----------
[INFO] Building SpringBoot-Profile-Example 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ SpringBoot-Profile-Example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ SpringBoot-Profile-Example ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ SpringBoot-Profile-Example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Projects\pratush_elipse\SpringBoot-Profile-Example\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ SpringBoot-Profile-Example ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ SpringBoot-Profile-Example ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ SpringBoot-Profile-Example ---
[INFO] Building jar: D:\Projects\pratush_elipse\SpringBoot-Profile-Example\target\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.1.RELEASE:repackage (repackage) @ SpringBoot-Profile-Example ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ SpringBoot-Profile-Example ---
[INFO] Installing D:\Projects\pratush_elipse\SpringBoot-Profile-Example\target\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.jar to C:\Users\narayanaswamy.ragi\.m2\repository\com\narayanatutorial\SpringBoot-Profile-Example\0.0.1-SNAPSHOT\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Projects\pratush_elipse\SpringBoot-Profile-Example\pom.xml to C:\Users\narayanaswamy.ragi\.m2\repository\com\narayanatutorial\SpringBoot-Profile-Example\0.0.1-SNAPSHOT\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.936 s
[INFO] Finished at: 2020-04-23T10:23:03+05:30
[INFO] ------------------------------------------------------------------------

 

Maven command to skip test case execution while creating jar

 

Command

D:\Projects\pratush_elipse\SpringBoot-Profile-Example>mvn install -DskipTests

or

D:\Projects\pratush_elipse\SpringBoot-Profile-Example>mvn install -Dmaven.test.skip=true

 

D:\Projects\pratush_elipse\SpringBoot-Profile-Example>mvn install -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ----------< com.narayanatutorial:SpringBoot-Profile-Example >-----------
[INFO] Building SpringBoot-Profile-Example 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ SpringBoot-Profile-Example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ SpringBoot-Profile-Example ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ SpringBoot-Profile-Example ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ SpringBoot-Profile-Example ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ SpringBoot-Profile-Example ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ SpringBoot-Profile-Example ---
[INFO] Building jar: D:\Projects\pratush_elipse\SpringBoot-Profile-Example\target\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.1.RELEASE:repackage (repackage) @ SpringBoot-Profile-Example ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ SpringBoot-Profile-Example ---
[INFO] Installing D:\Projects\pratush_elipse\SpringBoot-Profile-Example\target\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.jar to C:\Users\narayanaswamy.ragi\.m2\repository\com\narayanatutorial\SpringBoot-Profile-Example\0.0.1-SNAPSHOT\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Projects\pratush_elipse\SpringBoot-Profile-Example\pom.xml to C:\Users\narayanaswamy.ragi\.m2\repository\com\narayanatutorial\SpringBoot-Profile-Example\0.0.1-SNAPSHOT\SpringBoot-Profile-Example-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.062 s
[INFO] Finished at: 2020-04-23T10:20:23+05:30
[INFO] ------------------------------------------------------------------------

 

Maven configuration by default to skip test case execution while creating jar

If you want to skip tests by default but want the ability to re-enable tests from the command line, you need to go via a properties section in the pom:

    <project>
      [...]
      <properties>
        <skipTests>true</skipTests>
      </properties>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
            <configuration>
              <skipTests>${skipTests}</skipTests>
            </configuration>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>

 

And enable whenever you want to execute the test case

Command

The below command will run all the test case disabled by default in the above configuration.

D:\Projects\pratush_elipse\SpringBoot-Profile-Example>mvn install -DskipTests=false

 

pom.xml.template to pom.xml

we can generate pom.xml from pom.xml.template by using the pomgenerator command as follows.

D:\Projects\pratush_elipse\SpringBoot-Profile-Example>mvn pomgenerate:generate

 

 

References

Apache Maven Skipping Test

 

 

Leave a Reply