{"id":7016,"date":"2020-10-01T07:47:02","date_gmt":"2020-10-01T05:47:02","guid":{"rendered":"https:\/\/blog.via-internet.de\/?p=7016"},"modified":"2020-10-01T07:47:02","modified_gmt":"2020-10-01T05:47:02","slug":"maven-getting-started","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2020\/10\/01\/maven-getting-started\/","title":{"rendered":"Maven | Getting Started"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install on macOS<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ brew install maven<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Maven Commands<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Maven Commands<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s look into some popular and must know maven commands. We will use a sample Maven project to showcase the command output.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. mvn clean<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command cleans the maven project by deleting the target directory. The command output relevant messages are shown below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn clean\n...\n[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-example-jar ---\n[INFO] Deleting \/Users\/pankaj\/Desktop\/maven-examples\/maven-example-jar\/target\n...<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2. mvn compiler:compile<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">This command compiles the java source classes of the maven project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn compiler:compile\n...\n[INFO] --- maven-compiler-plugin:3.8.1:compile (default-cli) @ maven-example-jar ---\n[INFO] Changes detected - recompiling the module!\n[INFO] Compiling 1 source file to \/Users\/pankaj\/Desktop\/maven-examples\/maven-example-jar\/target\/classes\n...<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. mvn compiler:testCompile<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command compiles the test classes of the maven project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn compiler:testCompile\n...\n[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-cli) @ maven-example-jar ---\n[INFO] Changes detected - recompiling the module!\n[INFO] Compiling 1 source file to \/Users\/pankaj\/Desktop\/maven-examples\/maven-example-jar\/target\/test-classes\n...<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. mvn package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command builds the maven project and packages them into a JAR, WAR, etc.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn package\n...\n[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---\n[INFO] Changes detected - recompiling the module!\n[INFO] Compiling 1 source file to \/Users\/pankaj\/Desktop\/maven-examples\/maven-example-jar\/target\/classes\n...\n-------------------------------------------------------\n T E S T S\n-------------------------------------------------------\nRunning com.journaldev.maven.classes.AppTest\nTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec\n\nResults :\nTests run: 1, Failures: 0, Errors: 0, Skipped: 0\n\n[INFO] \n[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar ---\n[INFO] Building jar: ...\/maven-examples\/maven-example-jar\/target\/maven-example-jar-0.0.1-SNAPSHOT.jar\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n...\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output shows the location of the JAR file just before the \u201cBUILD SUCCESS\u201d message. Notice the package goal executes compile, testCompile, and test goals before packaging the build.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5. mvn install<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command builds the maven project and installs the project files (JAR, WAR, pom.xml, etc) to the local repository.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn install\n...\n[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-example-jar ---\n...\n[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---\n...\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6. mvn deploy<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command is used to deploy 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 is used to provide authentication details.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">7. mvn validate<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command validates the maven project that everything is correct and all the necessary information is available.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8. mvn dependency:tree<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command generates the dependency tree of the maven project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn dependency:tree\n...\n[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Mockito-Examples ---\n[INFO] com.journaldev.mockito:Mockito-Examples:jar:1.0-SNAPSHOT\n[INFO] +- org.junit.platform:junit-platform-runner:jar:1.2.0:test\n[INFO] |  +- org.apiguardian:apiguardian-api:jar:1.0.0:test\n[INFO] |  +- org.junit.platform:junit-platform-launcher:jar:1.2.0:test\n...\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">9. mvn dependency:analyze<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command analyzes the maven project to identify the unused declared and used undeclared dependencies. It\u2019s useful in reducing the build size by identifying the unused dependencies and then remove it from the pom.xml file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn dependency:analyze\n...\n[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ Mockito-Examples ---\n[WARNING] Used undeclared dependencies found:\n[WARNING]    org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test\n[WARNING]    org.mockito:mockito-core:jar:2.19.0:test\n[WARNING] Unused declared dependencies found:\n[WARNING]    org.junit.platform:junit-platform-runner:jar:1.2.0:test\n...<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">10. mvn archetype:generate<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Maven archetypes is a maven project templating toolkit. We can use this command to generate a skeleton maven project of different types, such as JAR, web application, maven site, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Recommended Reading:&nbsp;<a href=\"https:\/\/www.journaldev.com\/33593\/creating-java-project-maven-archetypes\" rel=\"noreferrer noopener\" target=\"_blank\">Creating a Java Project using Maven Archetypes<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">11. mvn site:site<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command generates a site for the project. You will notice a \u201csite\u201d directory in the target after executing this command. There will be multiple HTML files inside the site directory that provides information related to the project.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">12. mvn test<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command is used to run the test cases of the project using the&nbsp;<code>maven-surefire-plugin<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn test\n...\n[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ Mockito-Examples ---\n[INFO] \n[INFO] -------------------------------------------------------\n[INFO]  T E S T S\n[INFO] -------------------------------------------------------\n[INFO] Running TestSuite\nfirst-element\nsecond-element\nEmployee setName Argument = Pankaj\n...\n[INFO] Results:\n[INFO] \n[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0\n...<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">13. mvn compile<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s used to compile the source Java classes of the project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn compile\n...\n[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ Mockito-Examples ---\n[INFO] Changes detected - recompiling the module!\n[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!\n[INFO] Compiling 10 source files to \/Users\/pankaj\/Desktop\/maven-examples\/Mockito-Examples\/target\/classes\n...<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">14. mvn verify<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command build the project, runs all the test cases and run any checks on the results of the integration tests to ensure quality criteria are met.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Maven Options<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Maven provides a lot of command-line options to alter the maven build process. Let\u2019s look at some of the important maven options.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">15. mvn -help<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command prints the maven usage and all the available options for us to use.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">16. mvn -f maven-example-jar\/pom.xml package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command is used to build a project from a different location. We are providing the pom.xml file location to build the project. It\u2019s useful when you have to run a maven build from a script.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">17. mvn -o package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command is used to run the maven build in the offline mode. It\u2019s useful when we have all the required JARs download in the local repository and we don\u2019t want Maven to look for any JARs in the remote repository.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">18. mvn -q package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Runs the maven build in the quiet mode, only the test cases results and errors are displayed.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn -q package         \n\n-------------------------------------------------------\n T E S T S\n-------------------------------------------------------\nRunning com.journaldev.maven.classes.AppTest\nTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec\n\nResults :\n\nTests run: 1, Failures: 0, Errors: 0, Skipped: 0<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">19. mvn -X package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Prints the maven version and runs the build in the debug mode. It\u2019s opposite of the quiet mode and you will see a lot of debug messages in the console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"caption-attachment-33651\">mvn -X Debug Mode<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">20. mvn -v<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used to display the maven version information.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mvn -v\nApache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)\nMaven home: \/Users\/pankaj\/Downloads\/apache-maven-3.6.3\nJava version: 13.0.1, vendor: Oracle Corporation, runtime: \/Library\/Java\/JavaVirtualMachines\/jdk-13.0.1.jdk\/Contents\/Home\nDefault locale: en_IN, platform encoding: UTF-8\nOS name: \"mac os x\", version: \"10.15.1\", arch: \"x86_64\", family: \"mac\"<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">21. mvn -V package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command prints the maven version and then continue with the build. It\u2019s equivalent to the commands&nbsp;<code>mvn -v;mvn package<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">22. mvn -DskipTests package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>skipTests<\/code>&nbsp;system property is used to skip the unit test cases from the build cycle. We can also use&nbsp;<code>-Dmaven.test.skip=true<\/code>&nbsp;to skip the test cases execution.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">23. mvn -T 4 package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This command tells maven to run parallel builds using the specified thread count. It\u2019s useful in multiple module projects where modules can be built in parallel. It can reduce the build time of the project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">See also<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/gist.github.com\/michaellihs\/b08c89581ec597fa198cf74e2239f4a6\" data-type=\"URL\" data-id=\"https:\/\/gist.github.com\/michaellihs\/b08c89581ec597fa198cf74e2239f4a6\" target=\"_blank\" rel=\"noreferrer noopener\">Maven Cheat Sheet<\/a><\/li><li><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Trobleshooting<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/stackoverflow.com\/questions\/50104172\/could-not-find-or-load-main-class-org-apache-maven-wrapper-mavenwrappermain\">Could not find or load main class org.apache.maven.wrapper.MavenWrapperMain<\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re missing the&nbsp;<code>.mvn<\/code>&nbsp;folder in your git repository. You should have a folder called&nbsp;<code>.mvn<\/code>&nbsp;which contains the following files<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>wrapper\/maven-wrapper.jar<\/code>,&nbsp;<code>wrapper\/maven-wrapper.properties<\/code><\/li><li><code>jvm.config<\/code>. <\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Try doing&nbsp;<code>git add -f .mvn<\/code>&nbsp;from the command line then commit and push.<a href=\"https:\/\/stackoverflow.com\/posts\/52750359\/timeline\"><\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is how to create .mvn directory with needed jar inside.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mvn -N io.takari:maven:wrapper<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can also specify the version of Maven:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mvn -N io.takari:maven:wrapper -Dmaven=3.5.2<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Installation Install on macOS Maven Commands Maven Commands Let\u2019s look into some popular and must know maven commands. We will use a sample Maven project to showcase the command output. 1. mvn clean This command cleans the maven project by deleting the target directory. The command output relevant messages are shown below. 2. mvn compiler:compile This command compiles the java source classes of the maven project. 3. mvn compiler:testCompile This command compiles the test classes of the maven project. 4. mvn package This command builds the maven project and packages them into a JAR, WAR, etc. The output shows the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7020,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[37,48],"tags":[],"class_list":["post-7016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-maven"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/7016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=7016"}],"version-history":[{"count":0,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/7016\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=7016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=7016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=7016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}