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.
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:
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:
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 ?
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>
How to choose if a dependency will be included in your archive or not ?
When using Eclipse, if you Add a library to the Project build path is will be used just for compiling and it will not be included in your archive (e.g. in the WEB-INF/lib folder of your webapp)
The equivalent in Maven is using the scope provided in a dependency:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>1.1.1.Final</version> <scope>provided</scope> </dependency>
When using Eclipse, if you choose to Add classes to the Deployment Assembly, as shown by the following picture:
it means that the JAR file will be exported in your application. The equivalent in maven is using the compile scope.
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <scope>compile</scope> </dependency>
How to choose the name of your packaged application?
In the <build> section of your pom.xml , using the finalName element:
<build> <finalName>${project.artifactId}</finalName> </build>
In this example, the name of the packaged archive is the project’s artifactId
<artifactId>as7example</artifactId>
How to choose which JDK version will be used for compiling classes ?
You need to use the maven-compiler-plugin into the <build> section of your pom.xml
<build> . . . . <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> . . . . </build>
In this example, the compiler plugin enforces Java 1.6 compatibility.
How to add resources to a JAR ?
When using Eclipse, if you want to add a resource into an archive then you could simply place it into the “src” or “ejbProject” of your project, and it will be packaged together with the application classes, at the same level you have added it into your source folder.
Maven does pretty much the same, just it uses the resources directory structure to store files or resources.
The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
my-app
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- com
| | `-- mycompany
| | `-- app
| | `-- App.java
| `-- resources
| `-- META-INF
| `-- application.properties
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
Therefore, if you package this application, the application.properties file will be stored under the META-INF root folder of your archive.
How to run a JUnit test with Maven
Well at first you need to include the JUnit dependency in your pom.xml:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency>
Next create your test cases reside in the <project_base_dir>/src/test/java folder.
Executing the test build phase from the command line executes the tests and all other phases required for the test phase:
$ mvn test
Alternatively, the test phase is automatically executed in the default build lifecycle. Therefore, executing install, for example, will run all the phases including the test phase:
$ mvn install Apache Maven outputs the test results on the console. You should see something similar to: -------------------------------------------------------T E S T S -------------------------------------------------------Running com.sample.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0