Maven Build & Run

Below are the various commands which can be used to Build and Run the Maven Project in Windows.

Maven Build

Maven comes with the pre-defined goals, which can be used to run with mvn command and a Maven project can be built.
To Build a Maven project you need to:

  • Download all the dependencies
  • Compile the Project
  • Generate the target folder
# Clean target folder (if already exists), compile the project and generate a fresh target folder
mvn clean install

If you are looking to package the Project, you can use:

mvn package

Maven Skip Test

If you want to Skip the tests execution while generating the package, you can use:

mvn -Dmaven.test.skip=true package

Maven Run

Below are the Different ways you can Run the Maven Application.

Using the Main Class in Application, i.e. consider the Main Class in Application is com.example.App

# Run the Main Class in the Application
mvn exec:java -Dexec.mainClass=com.example.App

Running Maven Project after package, from a Jar file. Consider the Jar file name as myapp-0.1.jar.

# Run Application using Jar, required package command to be executed before this
java -jar target/myapp-0.1.jar

Using the Maven Command for Spring Boot Project to Run:

# Run the Main Class in the Application
mvn spring-boot:run

If you have generated the Maven Project using Spring Initializr, they you must be having a mvnw file, which you can use to run the Application.

# Run as a Spring Boot Application
./mvnw spring-boot:run

If you want to know more Maven Commands, then Follow the Apache Maven – Getting Started – Guide.

Advertisement