How to build project with Maven

To build a Maven based project, open your console, change to your project folder where pom.xml file is placed, and issue this command :

mvn package

This will execute the Maven “package” phase.

Maven build lifecycle
Maven is run by phases, read this default Maven build lifecycle article for more detail. So, when the “package” phase is executed, all its above phases – “validate“, “compile” and “test“, including the current phase “package” will be executed orderly.

“mvn package” example

When you run “mvn package” command, it will compile source code, run unit test and pack it depends on your “packaging” tag in pom.xml file. For example,

1. If “packaging” = jar, it will package your project into a “jar” file and put it into your target folder

File : pom.xml

<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>Maven Example</artifactId>
	<packaging>jar</packaging>
	...

2. If “packaging” = war, it will package your project into a “war” file and put it into your target folder.

File : pom.xml

<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>Maven Example</artifactId>
	<packaging>war</packaging>
	...
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: electrician a good career()

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: mr g locksmith()

  • Pingback: site here()

  • Pingback: this content()

  • Pingback: electrician training()

  • Pingback: water ionizer loans()

  • Pingback: water ionizer machines()

  • Pingback: stop parking()

  • Pingback: mobile porn()

  • Pingback: laan penge nu uden sikkerhed()

  • Pingback: car parking()

  • Pingback: lan nu og her()

  • Pingback: Website()

  • Pingback: tv packages()

  • Pingback: TV for Businesses()

  • Pingback: enagic()

  • Pingback: alkaline water benefits()

  • Pingback: alkaline water()

  • Pingback: water ionizer()

  • Pingback: watch tv show episodes()

  • Pingback: watch movies online free()

  • Pingback: watch movies online free()

  • Pingback: Click here to download()

  • Pingback: Blue Coaster33()

  • http://wiredtreecouponcode2013.drupalgardens.com/content/wiredtree-coupon-code-2013-jawdropping-promotion http://wiredtreecouponcode2013.drupalgardens.com

    Normally I do not learn article on blogs, however I wish to say that
    this write-up very forced me to check out and do it! Your
    writing taste has been amazed me. Thank you, quite nice post.

  • Akram

    Hi mkyong, I have a question please, we have an ant build.xml, we want to migrate to maven, how to convert targets from build.xml to maven.
    thanks, your help is appreciated.

  • rohit

    how to genrate ear file from Maven?

  • Siva

    when i run mvn package. i am getting the following error..

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
     
    C:\Users\namasivayams&amp;gt;cd C:\test\deleteLater\testMVNProject\mkyongweb-core
     
    C:\test\deleteLater\testMVNProject\mkyongweb-core&amp;gt;mvn package
    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Building mkyongweb-core Maven Webapp
    [INFO]    task-segment: [package]
    [INFO] ------------------------------------------------------------------------
    [WARNING] POM for 'org.apache.maven.wagon:wagon-file:pom:1.0-beta-2:runtime' is
    invalid.
     
    Its dependencies (if any) will NOT be available to the current build.
    [WARNING] POM for 'org.apache.maven.wagon:wagon-ssh-external:pom:1.0-beta-2:runt
    ime' is invalid.
     
    Its dependencies (if any) will NOT be available to the current build.
    [WARNING] POM for 'org.apache.maven.wagon:wagon-ssh:pom:1.0-beta-2:runtime' is i
    nvalid.
     
    Its dependencies (if any) will NOT be available to the current build.
    [WARNING] POM for 'org.codehaus.plexus:plexus-interpolation:pom:1.6:runtime' is
    invalid.
     
    Its dependencies (if any) will NOT be available to the current build.
    [INFO] [resources:resources {execution: default-resources}]
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
    i.e. build is platform dependent!
    [INFO] Copying 0 resource
    [WARNING] POM for 'org.codehaus.plexus:plexus-container-default:pom:1.0-alpha-8:
    runtime' is invalid.
     
    Its dependencies (if any) will NOT be available to the current build.
    [WARNING] POM for 'org.codehaus.plexus:plexus-utils:pom:1.0.5:runtime' is invali
    d.
     
    Its dependencies (if any) will NOT be available to the current build.
    [INFO] [compiler:compile {execution: default-compile}]
    [INFO] No sources to compile
    [INFO] [resources:testResources {execution: default-testResources}]
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
    i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory C:\test\deleteLater\testMVNProject\mk
    yongweb-core\src\test\resources
    [INFO] [compiler:testCompile {execution: default-testCompile}]
    [INFO] No sources to compile
    [WARNING] POM for 'org.apache.maven.surefire:surefire-api:pom:2.4.3:runtime' is
    invalid.
     
    Its dependencies (if any) will NOT be available to the current build.
    -----------------------------------------------------
    this realm = app0.child-container[org.apache.maven.plugins:maven-surefire-plugin
    :2.4.3]
    urls[0] = file:/C:/Users/namasivayams/.m2/repository/org/apache/maven/plugins/ma
    ven-surefire-plugin/2.4.3/maven-surefire-plugin-2.4.3.jar
    urls[1] = file:/C:/Users/namasivayams/.m2/repository/org/apache/maven/surefire/s
    urefire-booter/2.4.3/surefire-booter-2.4.3.jar
    urls[2] = file:/C:/Users/namasivayams/.m2/repository/org/apache/maven/surefire/s
    urefire-api/2.4.3/surefire-api-2.4.3.jar
    urls[3] = file:/C:/Users/namasivayams/.m2/repository/org/codehaus/plexus/plexus-
    utils/1.5.1/plexus-utils-1.5.1.jar
    Number of imports: 10
    import: org.codehaus.classworlds.Entry@a6c57a42
    import: org.codehaus.classworlds.Entry@12f43f3b
    import: org.codehaus.classworlds.Entry@20025374
    import: org.codehaus.classworlds.Entry@f8e44ca4
    import: org.codehaus.classworlds.Entry@92758522
    import: org.codehaus.classworlds.Entry@ebf2705b
    import: org.codehaus.classworlds.Entry@bb25e54
    import: org.codehaus.classworlds.Entry@bece5185
    import: org.codehaus.classworlds.Entry@3fee8e37
    import: org.codehaus.classworlds.Entry@3fee19d8
     
     
    this realm = plexus.core
    urls[0] = file:/C:/Downloads/apache-maven-2.2.1/lib/maven-2.2.1-uber.jar
    Number of imports: 10
    import: org.codehaus.classworlds.Entry@a6c57a42
    import: org.codehaus.classworlds.Entry@12f43f3b
    import: org.codehaus.classworlds.Entry@20025374
    import: org.codehaus.classworlds.Entry@f8e44ca4
    import: org.codehaus.classworlds.Entry@92758522
    import: org.codehaus.classworlds.Entry@ebf2705b
    import: org.codehaus.classworlds.Entry@bb25e54
    import: org.codehaus.classworlds.Entry@bece5185
    import: org.codehaus.classworlds.Entry@3fee8e37
    import: org.codehaus.classworlds.Entry@3fee19d8
    -----------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [ERROR] BUILD ERROR
    [INFO] ------------------------------------------------------------------------
    [INFO] Internal error in the plugin manager executing goal 'org.apache.maven.plu
    gins:maven-surefire-plugin:2.4.3:test': Unable to load the mojo 'org.apache.mave
    n.plugins:maven-surefire-plugin:2.4.3:test' in the plugin 'org.apache.maven.plug
    ins:maven-surefire-plugin'. A required class is missing: org/apache/maven/surefi
    re/util/NestedCheckedException
    org.apache.maven.surefire.util.NestedCheckedException
    [INFO] ------------------------------------------------------------------------
    [INFO] For more information, run Maven with the -e switch
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2 seconds
    [INFO] Finished at: Fri Aug 03 08:42:38 EDT 2012
    [INFO] Final Memory: 9M/109M
    [INFO] ------------------------------------------------------------------------
    C:\test\deleteLater\testMVNProject\mkyongweb-core&amp;gt;
    • http://www.mkyong.com mkyong

      Are you using surefire to generate unit test report? What is your Maven version? Try use the latest.

      You look have many dependencies not available errors, try comment them and run again.

  • venkatesh

    i am runing the server in debuging mode i am getting this error:Could not initialize the ServletContextListener