Monday, December 27, 2010

How To Create An Empty Java Project With Maven

To create an empty maven-managed java project simply type:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

You will be asked for the groupId and artifactId for your project, the project version and the main package for your code.

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId': : cloudobjects
Define value for property 'artifactId': : demoProject
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  cloudobjects: : de.cloudobjects
Confirm properties configuration:
groupId: cloudobjects
artifactId: demoProject
version: 1.0-SNAPSHOT
package: de.cloudobjects
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: cloudobjects
[INFO] Parameter: packageName, Value: de.cloudobjects
[INFO] Parameter: package, Value: de.cloudobjects
[INFO] Parameter: artifactId, Value: demoProject
[INFO] Parameter: basedir, Value: /Users/helmut
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: /Users/helmut/demoProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.293s
[INFO] Finished at: Mon Dec 27 15:08:58 CET 2010
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------


Now you have a simple maven-managed java project.

If you are using eclipse you have (at least) two options:

To create an eclipse project from your maven project cd into the project-directory and type:

$ mvn eclipse:eclipse

If you are using the M2Eclipse-Plugin, you can import the maven project directly into eclipse.

To import a maven project into eclipse choose "File -> Import" and select "Maven -> Existing Maven Projects":


In the next step select the newly created project folder and the projects pom.xml:


Now you have an eclipse-project with full maven dependency management.

More information about the M2Eclipse-Plugin can be found in the sonatype M2Eclipse manual.

3 comments:

  1. It might be better to use M2 Eclipse Plugin and just import the project as an Maven Project so you have the support of many features for Maven inside Eclipse.

    ReplyDelete
  2. @Karl Heinz Marbaise
    Thank you for the hint. I updated the article with information about the M2Eclipse Plugin.

    ReplyDelete
  3. Lifesaver. Thank you for your terseness!

    ReplyDelete