Maven 3 Tutorial – Project Setup

June 11th, 2011 by David Kessler Leave a reply »

New Project

For this example I will be using Eclipse for my IDE.

  1. Download Eclipse IDE for Java EE Developers from http://www.eclipse.org/downloads/
  2. Unzip the contents in your root Eclipse folder.  In my case I unzipped it in c:/java/eclipse
  3. Run the ‘eclipse.exe’ in [eclipse root]/eclipse
  4. Select a workspace location for Eclipse to store your projects.  In my case I used the default location
  5. m3-02

  6. Click on the ‘Workbench’ icon
  7. m3-03[1]

  8. Select Help -> Install New Software
  9. m3-04[1]

  10. Click the ‘Add…’ button
  11. Name: m2eclipse
  12. Location: http://m2eclipse.sonatype.org/sites/m2e
  13. Click ‘OK’
  14. m3-05

  15. Check the ‘Maven Integration for Eclipse’ option
  16. Click ‘Next’
  17. m3-06

  18. Click ‘Next >’
  19. m3-07

  20. Select ‘I accept the terms of the license agreements’
  21. Click ‘Finish’
  22. m3-08

  23. Click ‘Restart Now’
  24. m3-09
    m3-10

  25. Select Window -> Preferences
  26. m3-11

  27. Add the Maven 3 location the you installed earlier
  28. m3-12

  29. Create a new Maven project by selecting File -> New -> Other…
  30. m3-13

  31. Select Maven -> Maven Project
  32. m3-14

  33. Check ‘Create a simple project (skip archetype selection)’
  34. Click ‘Next >’
  35. m3-24

  36. Enter Project Information
    1. Enter Group Id. This is the domain where the project is hosted. For example if the ‘Awesome’ project was hosted at GitHub then the Group Id would be ‘com.github.awesome’.
    2. Enter Artifact Id. This is the name of the resulting artifact. For example if Artifact Id is ‘awesome’ and the packaging is JAR the artifact will be named ‘awesome.jar’.
    3. The version can remain 0.0.1-SNAPSHOT
    4. Select the packaging that you desire
    5. Enter a Name. This name will show up in the console when you run Maven. This will also show up in the documentation that Maven generates
    6. Enter a Description. This is used in the documentation
    7. Click ‘Next >’
  37. m3-16

  38. Add a dependency on JUnit. One of the best features of m2eclipse plugin is the ability to search for available dependencies.  (This feature does not work if you configure ‘M3_HOME’ instead of ‘M2_HOME’)
  39. m3-17

  40. Click ‘Finish’
    • src – source and resource files
    • target – compiled files, reports and generated artifacts
    • main – application/library files
    • test – test files
    • java – java files
    • resources – non java files like XML
  41. m3-18
    Maven’s default package structure:
    m3-19

  42. Open the pom.xml file.
  43. Add the compiler plugin so you can configure your JDK version. Search for the maven-compiler-plugin.
  44. m3-20

  45. For this example set the JDK version to 1.6
  46. m3-21

    <configuration> 
    	<source>1.6</source> 
    	<target>1.6</target> 
    </configuration>
  47. Run ‘Maven install’ to verify that the project and environment is setup correctly
  48. m3-22

  49. The build was successful
  50. m3-23

Advertisement

22 comments

  1. Akrem Saed says:

    I always wanted to start using Maven along with Archiva for some time now, and this just helped me to jump start that … excellent post, thanks david

  2. mark says:

    acording to mvn -help it should be “mvn –version” instead of “mvn -version”

  3. mark says:

    I guess that’s a wordpress issue (- – instead of -)

  4. mark says:

    ‘eclipze.exe’ should be ‘eclipse.exe’. and thanks thanks thanks for the extremely helpful post

  5. David Kessler says:

    I fixed ‘eclipze.exe’. Nice catch. Thank you.

  6. David Kessler says:

    Both ‘mvn –version’ and ‘mvn -version’ work for me. I updated it to the latter. Thank you.

  7. greg says:

    correcting the corrector

    acording to mvn -help it should be “mvn –version” instead of “mvn -version”

  8. Akrem Saed says:

    one little issue I encountered was maven was not downloading dependencies from repository locations defined in a project pom.xml

    the pom had the following :
    <repositories>
    <repository>
    <id>springbyexample.org</id>
    <name>Spring by Example</name>
    <url>http://www.springbyexample.org/maven/repo</url>
    </repository>

    <repository>
    <id>com.springsource.repository.bundles.release</id>
    <name>SpringSource Enterprise Bundle Repository – SpringSource Bundle Releases</name>
    <url>http://repository.springsource.com/maven/bundles/release</url>
    </repository>
    <repository>
    <id>com.springsource.repository.bundles.external</id>
    <name>SpringSource Enterprise Bundle Repository – External Bundle Releases</name>
    <url>http://repository.springsource.com/maven/bundles/external</url>
    </repository>
    <repository>
    <id>spring.maven.milestone</id>
    <name>Spring Milestone Maven Repo</name>
    <url>http://repository.springsource.com/maven/bundles/milestone</url>
    </repository>

    <repository>
    <id>maven2-repository.dev.java.net</id>
    <name>Java.net Maven 2 Repository</name>
    <url>http://download.java.net/maven/2</url>
    </repository>
    </repositories>

    but since I configured Archiva to ONLY mirror central maven2-repository.dev.java.net but in the maven settings.xml file we used
    <mirror>
    <id>archiva.default</id>
    <url>http://localhost:8095/archiva/repository/internal/</url>
    <mirrorOf>*</mirrorOf>
    </mirror>

    the * was the problem because we are telling maven to always use archiva as the mirror , but Archiva is only mirroring two repositories . To fix this , we changed the last one to

    <mirror>
    <id>archiva.default</id>
    <url>http://localhost:8095/archiva/repository/internal/</url>
    <mirrorOf>central,maven2-repository.dev.java.net</mirrorOf>
    </mirror>

  9. john says:

    Your screenshots are .bmp files. I just can’t believe it. Page 3 of this article nearly weights 100 MB. I’m glad you made this tutorial but, seriously, .bmp?

  10. David Kessler says:

    Good catch. I updated it with smaller .jpg files. Thank you.

  11. Andi says:

    after many days – with this tutorial maven run , thanks

  12. Apache Omega says:

    I’ve been trying for days to get maven running and it just wont work.
    I type in mvn -version and it says mvn is not recognized
    what the hell am i doing wrong?

  13. Travis Klotz says:

    Omega,

    It sounds like you do not have M2_HOME/bin on your Path. So when you try to execute the command, it had no idea where the program is.

    T

  14. Adel M says:

    Very nice overview of Maven! but please do something about the missing xml tags on the xml examples. Thanks!

  15. David Kessler says:

    Thank you. Nice catch. I’m not sure how the xml tags got stripped off of this post but I added them back.

  16. Alvi says:

    Hei,

    I follow the tutorial very well but when i run mvn install i got following error. Can some one tell me what i miss.. thanks

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project onlineweb: Error assembling WAR: webxml attribute i
    s required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project inmo
    bilweb: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    Caused by: org.apache.maven.plugin.MojoExecutionException: Error assembling WAR:
    webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    at org.apache.maven.plugin.war.WarMojo.execute(WarMojo.java:175)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    … 19 more
    Caused by: org.codehaus.plexus.archiver.ArchiverException: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    at org.codehaus.plexus.archiver.war.WarArchiver.initZipOutputStream(WarArchiver.java:149)
    at org.codehaus.plexus.archiver.zip.AbstractZipArchiver.createArchiveMain(AbstractZipArchiver.java:346)
    at org.codehaus.plexus.archiver.zip.AbstractZipArchiver.execute(AbstractZipArchiver.java:250)
    at org.codehaus.plexus.archiver.AbstractArchiver.createArchive(AbstractArchiver.java:871)
    at org.apache.maven.archiver.MavenArchiver.createArchive(MavenArchiver.java:543)
    at org.apache.maven.plugin.war.WarMojo.performPackaging(WarMojo.java:225)
    at org.apache.maven.plugin.war.WarMojo.execute(WarMojo.java:159)
    … 21 more
    [ERROR]
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

  17. David Kessler says:

    I was able to reproduce this issue with the following steps.
    1. Create new Maven Project
    2. check ‘Create a simple project (skip archetype selection)
    3. next
    4. add groupId/artifactId
    5. change the packaging to ‘war’
    6. finish
    7. Run As -> Maven install

    The reason that you’re getting this message is that there isn’t a ‘web.xml’ file in ’src/main/java/webapp/WEB-INF’

    You can either add a ‘web.xml’ file to this directory or you can use a Maven archetype. To use an archetype follow these steps.

    1. Create new Maven Project
    2. uncheck ‘Create a simple project (skip archetype selection)
    3. next
    4. select ‘All Catalogs’
    5. Filter = ‘maven-archetype-webapp’
    6. select ‘maven-archetype-webapp’
    7. next
    8. add groupId/artifactId
    9. finish

    This will create a ‘web.xml’ file for you.

    Hope this helps.

  18. Alvi says:

    Thanks David kessler now its work

  19. This post is great I’m glad I found it

  20. Tory Barren says:

    This is simply supurb, very indepth, thank you.

  21. Mara says:

    very good submit, i definitely love this website, carry on it

  22. Abdul Mannan Khan says:

    kudos.. Great tutorial for start-up.

Leave a Reply