We have seen in the last post , An introduction to Maven, an overall perspective about Maven and its main features.
Maven allows us to create a project from either command line or with our prefered IDE. But before starting, it is a good idea remenbering what an archetype is, an archetype is an easy artefact that creates a project’s skeleton (prototype).With Maven we can create new projects through an archetype.
From command line.
First at all, we are going to create a new project from command line. For that purpose we are going to modify the following line depending of the project type we want to build.
mvn archetype:generate -DarchetypeGroupId= -DarchetypeArtifactId= -DarchetypeVersion= -DgroupId= -DartifactId=
The easiest archetype we can use to create a new Maven project is the maven-archetype-quickstart one. We are going to execute the next line.
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE -DgroupId=com.hop2croft.example -DartifactId=QuickstartExample
The generated output will be the following. In first place, we will download the plugins, dependencies or resources that we need (those that we don’t have previously in our local repository).
hop2croft-e105bb50cb>$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE -DgroupId=com.hop2croft.example -DartifactId=QuickstartExample[INFO] Scanning for projects... Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-site-plugin/2.0.1/maven-site-plugin-2.0.1.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-site-plugin/2.0.1/maven-site-plugin-2.0.1.pom (14 KB at 11.0 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom (5 KB at 6.9 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-release-plugin/2.0/maven-release-plugin-2.0.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-release-plugin/2.0/maven-release-plugin-2.0.pom (8 KB at 11.6 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/release/maven-release/2.0/maven-release-2.0.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/release/maven-release/2.0/maven-release-2.0.pom (7 KB at 10.0 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (643 B at 1.1 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.0/maven-archetype-plugin-2.0.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.0/maven-archetype-plugin-2.0.pom (6 KB at 8.4 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/maven-archetype/2.0/maven-archetype-2.0.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetype/maven-archetype/2.0/maven-archetype-2.0.pom (9 KB at 19.5 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.0/maven-archetype-plugin-2.0.jar Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.0/maven-archetype-plugin-2.0.jar (70 KB at 49.5 KB/sec) [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 --- Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/archetype-common/2.0/archetype-common-2.0.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetype/archetype-common/2.0/archetype-common-2.0.pom (16 KB at 34.4 KB/sec) Downloading: http://repo1.maven.org/maven2/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.pom Downloaded: http://repo1.maven.org/maven2/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.pom (2 KB at 2.3 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker/2.0.10/maven-invoker-2.0.10.pom Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker/2.0.10/maven-invoker-2.0.10.pom (6 KB at 10.0 KB/sec) Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-velocity/1.1.8/plexus-velocity-1.1.8.pom Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-velocity/1.1.8/plexus-velocity-1.1.8.pom (2 KB at 3.4 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/archetype-common/2.0/archetype-common-2.0.jar Downloading: http://repo1.maven.org/maven2/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.jar Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-velocity/1.1.8/plexus-velocity-1.1.8.jar Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-5/plexus-interactivity-api-1.0-alpha-5.jar Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar Downloaded: http://repo1.maven.org/maven2/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.jar (26 KB at 8.6 KB/sec) Downloading: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker/2.0.10/maven-invoker-2.0.10.jar Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-5/plexus-interactivity-api-1.0-alpha-5.jar (14 KB at 4.2 KB/sec) Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-velocity/1.1.8/plexus-velocity-1.1.8.jar (8 KB at 2.1 KB/sec) Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar (262 KB at 70.4 KB/sec) Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker/2.0.10/maven-invoker-2.0.10.jar (28 KB at 18.3 KB/sec) Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetype/archetype-common/2.0/archetype-common-2.0.jar (214 KB at 40.0 KB/sec)
After this we are going to create our project from the selected archetype. Maven shows us an available archetype list.
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> docbkx-quickstart-archetype (-)
2: remote -> multi (-)
3: remote -> simple (-)
4: remote -> apparat-archetype-asm (-)
5: remote -> apparat-archetype-tdsi (-)
6: remote -> gquery-archetype (-)
7: remote -> gquery-plugin-archetype (-)
8: remote -> jdbc-proc-archetype (Creates simple project with jdbc-proc support)
9: remote -> liferay-layouttpl-archetype (Provides an archetype to create Liferay layout templates.)
10: remote -> liferay-portlet-archetype (Provides an archetype to create Liferay portlets.)
...
100: remote -> maven-archetype-mojo (An archetype which contains a sample a sample Maven plugin.)
101: remote -> maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
102: remote -> maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site. This archetype can be layered upon an
existing Maven plugin project.)
103: remote -> maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
104: remote -> maven-archetype-profiles (-)
105: remote -> maven-archetype-quickstart (An archetype which contains a sample Maven project.)
106: remote -> maven-archetype-site (An archetype which contains a sample Maven site which demonstrates some of the supported document types like
APT, XDoc, and FML and demonstrates how to i18n your site. This archetype can be layered
upon an existing Maven project.)
....
381: remote -> spring-osgi-bundle-archetype (Spring OSGi Maven2 Archetype)
382: remote -> spring-ws-archetype (Spring Web Services Maven2 Archetype.)
383: remote -> syncope-archetype (-)
384: remote -> trails-archetype (-)
385: remote -> trails-secure-archetype (-)
386: remote -> tynamo-archetype (-)
387: remote -> wicket-scala-archetype (-)
388: remote -> wikbook.archetype (-)
389: remote -> circumflex-archetype (-)
390: remote -> javg-minimal-archetype (-)
Choose a number: 105:
Selected the archetype, we add the rest of the requiered information to our project
Choose version: 1: 1.0-alpha-1 2: 1.0-alpha-2 3: 1.0-alpha-3 4: 1.0-alpha-4 5: 1.0 6: 1.1 Choose a number: 6: 6 Define value for property 'groupId': : com.hop2croft.example Define value for property 'artifactId': : QuickstartExample Define value for property 'version': 1.0-SNAPSHOT: : Define value for property 'package': com.hop2croft.example: : Confirm properties configuration: groupId: com.hop2croft.example artifactId: QuickstartExample version: 1.0-SNAPSHOT package: com.hop2croft.example Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.hop2croft.example [INFO] Parameter: packageName, Value: com.hop2croft.example [INFO] Parameter: package, Value: com.hop2croft.example [INFO] Parameter: artifactId, Value: QuickstartExample [INFO] Parameter: basedir, Value: /Users/Ivan/Desktop/maven [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/Ivan/Desktop/maven/QuickstartExample [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2:31.014s [INFO] Finished at: Tue Apr 26 01:23:24 CEST 2011 [INFO] Final Memory: 7M/81M [INFO] ------------------------------------------------------------------------ ivan-e105bb50cb:maven Ivan$
We have created a project called Quickstart that contains a normal java class (a Hello World example) and an easy test class. If we want to compile it and test it, we simply have to execute mvn install.
ivan-e105bb50cb:QuickstartExample Ivan$ mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building QuickstartExample 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ QuickstartExample --- [INFO] [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ QuickstartExample --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/Ivan/Desktop/maven/QuickstartExample/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ QuickstartExample --- [INFO] Compiling 1 source file to /Users/Ivan/Desktop/maven/QuickstartExample/target/classes [INFO] [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ QuickstartExample --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/Ivan/Desktop/maven/QuickstartExample/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ QuickstartExample --- [INFO] Compiling 1 source file to /Users/Ivan/Desktop/maven/QuickstartExample/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ QuickstartExample --- [INFO] Surefire report directory: /Users/Ivan/Desktop/maven/QuickstartExample/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.hop2croft.example.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.022 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ QuickstartExample --- [INFO] Building jar: /Users/Ivan/Desktop/maven/QuickstartExample/target/QuickstartExample-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ QuickstartExample --- [INFO] Installing /Users/Ivan/Desktop/maven/QuickstartExample/target/QuickstartExample-1.0-SNAPSHOT.jar to /Users/Ivan/.m2/repository/com/hop2croft/example/QuickstartExample/1.0-SNAPSHOT/QuickstartExample-1.0-SNAPSHOT.jar [INFO] Installing /Users/Ivan/Desktop/maven/QuickstartExample/pom.xml to /Users/Ivan/.m2/repository/com/hop2croft/example/QuickstartExample/1.0-SNAPSHOT/QuickstartExample-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 9.767s [INFO] Finished at: Tue Apr 26 01:43:22 CEST 2011 [INFO] Final Memory: 9M/81M [INFO] ------------------------------------------------------------------------
We can see int the text console how the project has been built and the test passed. At the same time a project’s jar file was created.
From Eclipse or STS.
Now we are going to generate the same project from STS or Eclipse. We select the menu option File -> Others. We write down Maven to filter the type of project we want (a Maven</strong project in our case).
We don’t need to select the simple project option as long as we are going to use an archetype for it.
In the next screen we can select the maven-archetype-quickstart archetype from the showed list.
Lastly we will fill all the requiered parameters of the project (our groupId and artifactId).
Our project is already created in STS or Eclipse.

If we want to use Maven with our favourite IDE is recommeded to install an Eclipse plugin called m2eclipse. It is really helpful if you work with Maven within your IDE.
In this post we have seen to ways to creae a Maven project using an archetype. Next post will speak about the basic structure of a Maven project.





2 respuestas a Creating a new Java project with Maven