Maven for Eclipse Users Tutorials

In this article we will try to provide a kickstart guide for Maven newbies, using the familiar concepts inherited from the Eclipse IDE such as Build Path, Deployment Assembly and so on. Kind of Rosetta Stone for Eclipse users !

How to create a new Project ?

For Eclipse users creating a new project is a piece of cake, just choose one of the avaiable projects from the File | New Menu and follow the intuitive wizard.

maven tutorial jboss

On Maven that's not complex at all: you will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:


mvn  archetype:generate  -DarchetypeArtifactId=jboss-javaee6-webapp   -DarchetypeGroupId=org.jboss.spec.archetypes  -DgroupId=com.mastertheboss -DartifactId=as7example  -DinteractiveMode=false


So what did we do ? we have created a new project from an archetype. An archetype is a very simple artifact, that contains the project prototype you wish to create. There are lots of prototypes available so you will need to provide the archetypeArtifactId and the archetypeGroupId. In our example we will choose the jboss-javaee6-webapp archetypeArtifactId and the org.jboss.spec.archetypes archetypeGroupId.
This command will create for you the following Java EE 6 project:

C:\MAVEN\AS7EXAMPLE
¦ pom.xml
+---src
+---main
¦ +---java
¦ ¦ +---com
¦ ¦ +---mastertheboss
¦ ¦ +---controller
¦ ¦ ¦ MemberRegistration.java
¦ ¦ ¦
¦ ¦ +---data
¦ ¦ ¦ MemberListProducer.java
¦ ¦ ¦
¦ ¦ +---model
¦ ¦ ¦ Member.java
¦ ¦ ¦
¦ ¦ +---rest
¦ ¦ ¦ JaxRsActivator.java
¦ ¦ ¦ MemberResourceRESTService.java
¦ ¦ ¦
¦ ¦ +---util
¦ ¦ Resources.java
¦ ¦
¦ +---resources
¦ ¦ ¦ import.sql
¦ ¦ ¦
¦ ¦ +---META-INF
¦ ¦ persistence.xml
¦ ¦
¦ +---webapp
¦ ¦ index.html
¦ ¦ index.xhtml
¦ ¦
¦ +---resources
¦ ¦
¦ +---WEB-INF
¦ ¦ beans.xml
¦ ¦ faces-config.xml
¦ ¦
¦ +---templates
¦ default.xhtml
¦
+---test
+---java
¦ +---com
¦ +---mastertheboss
¦ +---test
¦ MemberRegistrationTest.java
¦
+---resources
arquillian.xml

How to compile classes ?

Eclipse side:

maven jboss tutorial

The Maven compile lifecycle will compile your classes in the target folder of your project


$ mvn compile
[INFO] Scanning for projects...
. . . . .
-
[INFO] Compiling 6 source files to C:\maven\as7example\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.326s
[INFO] Finished at: Thu Dec 13 19:11:59 CET 2012
[INFO] Final Memory: 6M/20M
[INFO] ------------------------------------------------------------------------

How to clean your project classes/artifacts ?

Eclipse side:

maven jboss tutorial
The clean lifecycle handles the project cleaning:

$ mvn clean

How to package your classes into an archive ?

There are several ways to package your classes in an archive. The simplest one is to use the package lifecycle:


$ mvn package

[INFO] Webapp assembled in [516 msecs]
[INFO] Building war: C:\maven\as7example\target\as7example.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.762s
[INFO] Finished at: Thu Dec 13 19:55:22 CET 2012
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------

The other option which can be used to package your classes is mvn install which also copies your packaged Maven module to your local repository (by default, in ~/.m2/repository), to be accessed by other local Maven builds.

$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Java EE 6 webapp project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
. . . .
[INFO] Packaging webapp
[INFO] Assembling webapp [as7example] in [C:\maven\as7example\target\as7example]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\maven\as7example\src\main\webapp]
[INFO] Webapp assembled in [428 msecs]
[INFO] Building war: C:\maven\as7example\target\as7example.war
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ as7example ---

[INFO] Installing C:\maven\as7example\target\as7example.war to C:\Users\emaifro\.m2\repository\com\as7example\1.0-SNAPSHOT\as7example-1.0-SNAPSHOT.war
[INFO]  Installing C:\maven\as7example\pom.xml to  C:\Users\emaifro\.m2\repository\com\as7example\1.0-SNAPSHOT\as7example-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.565s
[INFO] Finished at: Thu Dec 13 19:14:37 CET 2012
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------

How to add a library to your project ?

jboss maven tutorial
Maven has not the concept of library but use “dependencies”. A dependency is an artifact which is contained in a repository (local or remote). Each dependency has the following attributes:

  • groupId will identify your project uniquely across all projects, so we need to enforce a naming schema (e.g. org.hibernate).
  • artifactId is the name of the jar without version. (e.g. hibernate-jpamodelgen)
  • version if you distribute it then you can choose any typical version with numbers and dots

ex:

<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-jpamodelgen</artifactId>
 <version>1.1.1.Final</version>
</dependency>

 

Follow us on Twitter