Mvn run command?
We will learn how to:
All of these tasks will be done on the command line, so that you can have a better idea on what’s going on under the hood, and how you can run a java application in environments that don’t have a full-featured IDE like Eclipse or IntelliJ.
First, let’s create a new project folder using the maven command line tools:
This generates a new project folder with the following structure:
App.java contains simple code that prints Hello World! when run.
Before running a Java application in production, we’ll need to compile the Java code into byte-code that can be run on the JVM.
If we have multiple classes and folder (which we most likely will), we have to package the compiled code into a common format (like a .jar file).
We can perform compilation and packaging by running the following command:
We can combine these two commands by running mvn package.
Running this command will create a bunch of files in a new target directory:
The JAR file is the final output that can be executed by the JVM. However, we still have to perform some additional steps before we can run our code.
We can use the java command to execute our JAR file:
If we run this now, we will get the following error:
This is because the JAR file doesn’t know the entry point, so it has no idea where the main method is.
We can make use of the Maven JAR plugin, which gives us additional capabilities to build JAR files.
We can add the following configuration as a child of the
We can now rebuild the project by running:
Next, we can execute the JAR file by running:
Which will give us the output:
Maven can also be used to run tests that we’ve defined in our project.
By convention, all tests reside within the src/test directory.
For the purpose of illustration, let’s create a static method to add two numbers in the App class:
We can now create a unit test for this method within src/test/java/com/sohamkamani/AppTest.java:
To run tests, we can run the mvn test command - this will run all tests, tell us how many passed and failed, and give us more information about the failed tests.
Let’s look at how to add dependencies and package them in our JAR file.
For most applications need external libraries (like Spring Boot or Apache Commons) to implement common functionality. Maven allows us to install these dependencies by specifying them in our pom.xml file.
For this example, let’s install the Cowsay library, which will display our output as a quote from a friendly cartoon figure of a cow.
First, we have to add Cowsay as a dependency in our pom.xml file:
Next, we can use the Cowsay.say method within our main method to print the final output string:
However, there’s a problem - If we recompile our code and try to run the app now, we will get an error:
It looks like the Java class loader couldn’t find the classes for the Cowsay library, even though we added it as a dependency in the pom.xml file.
This happens because by default, maven doesn’t bundle the dependency class files along with the application code. To enable this, we can use the maven-assembly-plugin.
This plugin includes all of our applications dependencies into the JAR file. This increases its overall size, but ensures that we can run it as a standalone executable using the java -jar command.
Let’s add the Maven assembly plugin in the pom.xml build definition:
To assemble our JAR file, we can run the assembly:single goal after the compile goal:
This creates a new JAR file in the target directory that you can run using the java -jar command:
You can view the complete example code for this post on Github
Maven is one of the most popular project and dependency management tools for Java applications. Maven provides a lot of commands and options to help you in your day to day tasks.
This cheat sheet uses a sample Maven project to demonstrate some useful Maven commands. It was originally written for OpenJDK 13.0.1 and Maven 3.6.3. These commands have been verified with OpenJDK 19.0.1 and Maven 3.8.7.
This command cleans the Maven project by deleting the target directory:
Example of the output:
This command compiles the Java source classes of the Maven project:
Example of the output:
This command compiles the test classes of the Maven project:
Example of the output:
This command builds the Maven project and packages it into a JAR, WAR, etc.:
Example of the output:
The output shows the location of the JAR file just before the "BUILD SUCCESS" message. Notice the package goal executes compile, testCompile, and test goals before packaging the build.
This command builds the Maven project and installs the project files (JAR, WAR, pom.xml, etc.) to the local repository:
Example of the output:
This command deploys the artifact to the remote repository:
The remote repository should be configured properly in the project pom.xml file distributionManagement tag. The server entries in the Maven settings.xml file are used to provide authentication details.
This command validates the Maven project to ensure that everything is correct and all the necessary information is available:
This command generates the dependency tree of the Maven project:
Example of the output:
This command analyzes the maven project to identify the unused declared and used undeclared dependencies:
Example of the output:
It’s useful in reducing the build size by identifying the unused dependencies and removing them from the pom.xml file.
This command generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc:
Example of the output:
Recommended Reading: Creating a Java Project using Maven Archetypes
This command generates a site for the project:
You will notice a site directory in the target directory after executing this command.
There will be multiple HTML files inside the site directory that provide information related to the project.
This command runs the test cases of the project:
Example of the output:
This command compiles the source Java classes of the project:
Example of the output:
It is similar to the previous mvn compiler:compile command, but runs the entire Maven lifecycle up to compile.
This command builds the project, runs all the test cases and run any checks on the results of the integration tests to ensure quality criteria are met:
Maven provides a lot of command-line options to alter the Maven build process:
This command-line option prints the Maven usage and all the available options:
Example of the output:
This command-line option builds a project from a different location:
Provide the pom.xml file location to build the project. It’s useful when you have to run a Maven build from a script.
This command-line option runs the Maven build in offline mode:
It’s useful when you have all the required JARs downloaded in the local repository and you don’t want Maven to look for any JARs in the remote repository.
This command-line option runs the Maven build in quiet mode, so that only the test case results and errors are displayed:
This command-line option prints the Maven version and runs the build in debug mode, so that all messages are displayed:
Example of the output:
This command-line option displays the Maven version information:
Example of the output:
This command-line option prints the Maven version and then continues with the build:
It’s equivalent to the commands:
This command-line option applies the skipTests system property to skip the unit test cases from the build cycle:
You can also skip the test cases execution:
This command-line option tells Maven to run parallel builds using the specified thread count:
It’s useful in multiple module projects where modules can be built in parallel. It can reduce the build time of the project.
- Edit the pom.xml file: View Code.
org.apache.maven.plugins . - Review the code. It should look like the following: View Image. In the Maven project, you specify the main class details by updating the pom. xml file.
- Press Ctrl+S and close the file.