Installing and Getting Hands on Maven

Now that we have sufficient understanding of the pom.xml file, let us shift on trying something out. Download Maven from maven.apache.org, unzip the archive into your local directory. Set the JAVA_HOME variable to point to the JDK installation and MAVEN_

Installing and Getting Hands on Maven

Installing and Getting Hands on Maven

     

Now that we have sufficient understanding of the pom.xml file, let us shift on trying something out. Download Maven from maven.apache.org, unzip the archive into your local directory. Set the JAVA_HOME variable to point to the JDK installation and MAVEN_HOME to point to the Maven directory and add the MAVEN_HOME/bin to the PATH environment variable.

1. To test whether the path has been set properly.  Type mvn -version on the command prompt.

C:\>mvn -version
Maven version: 2.0.7
Java version: 1.5.0
OS name: "windows 2000" version: "5.0" arch: "x86"

2. Note create a working directory

C:\>md maventest
C:\>cd maventest

3. Create your first project. In order to create the simplest of Maven projects, execute the following from the command line:

C:\maventest>mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes 
-DgroupId=net.roseindia.maven.quickstart  -DartifactId=HelloMaven

Once you have executed this command, you will notice a few things have happened. First, you will notice that a directory named HelloMaven has been created for the new project, and this directory contains a file named pom.xml that should look like this

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>net.roseindia.maven.quickstart</groupId>
<artifactId>HelloMaven</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>HelloMaven</name>
<url>http://maven.apache.org</url>bn 

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>

After the archetype generation of your first project, you can see the project created from the archetype has a POM, a target directory, a source tree for your application's sources and a source tree for your test sources. This is the standard layout for Maven projects. The application sources reside in ${basedir}/src/main/java and test sources reside in ${basedir}/src/test/java, where ${basedir} represents the directory containing pom.xml.

you will notice that the following directory structure has been created:

HelloMaven
|-- pom.xml
|-- target
`-- src
  |-- main
  |   `-- java
  |   `-- net
  |   `-- roseindia
  |   `-- maven
  | `-- quickstart
  |   `-- Calculator.java
  `-- test
  `-- java
  `-- net
  `-- roseindia
   `-- maven
  `-- quickstart
  `-- TestCalculator.java

 

4. Now to compile the application sources. Change to the directory where pom.xml is created and execute the "mvn compile" command to compile your application sources:

C:\maventest>cd HelloMaven
C:\maventest\HelloMaven>mvn compile

Upon executing this command you should see output like the following:

C:\maventest\HelloMaven>mvn compile
[INFO] Scanning for projects...
[INFO] -----------------------------------------------------------------
[INFO] Building HelloMaven
[INFO] task-segment: [compile]
[INFO] -----------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] -----------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -----------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sat Jul 21 17:33:27 GMT+05:30 2007
[INFO] Final Memory: 2M/5M
[INFO] -----------------------------------------------------------------

The first time you execute this command, Maven will download all the plugins and related dependencies it requires to fulfill the command. From a clean installation of Maven this can take quite a while (in the output above, it took almost 4 minutes). If you execute this command again, Maven will now have what it needs, so it won't need to download anything new and will be able to execute the command much more quickly.

As you can see from the output, the compiled classes were placed in ${basedir}/target/classes, which is another standard convention employed by Maven. So, if you're a keen observer, you'll notice that by using the standard conventions the POM above is very small and you haven't had to tell Maven explicitly where any of your sources are or where the output should go. 

5. To compile your test sources and run your unit tests. Now you've got some unit tests that you want to compile and execute. Execute the following command:

C:\maventest\HelloMaven>mvn test
C:\maventest\HelloMaven>mvn test[INFO]
Scanning for projects...[INFO]
-----------------------------------------------------------------------------------------
[INFO] Building HelloMaven[INFO] task-segment: [test][INFO]
-----------------------------------------------------------------------------------------
[INFO] [resources:resources][INFO] Using default encoding to
copy filtered resources.[INFO] [compiler:compile][INFO]
Nothing to compile - all classes are up to date[INFO]
[resources:testResources][INFO] Using default encoding to
copy filtered resources.[INFO] [compiler:testCompile][INFO]
Nothing to compile - all classes are up to date[INFO]
[surefire:test][INFO] Surefire report directory:
C:\maventest\HelloMaven\target\surefire-reports


-------------------------------------------------------------------------------
T E S T S
-------------------------------------------------------------------------------
[surefire] Running
net.roseindia.maven.quickstart.TestCalculator[surefire] Tests
run: 1, Failures: 0, Errors: 0, Time elapsed: 0.001 sec Results
:[surefire] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO]
-------------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL[INFO][INFO] Total time: 2
seconds[INFO] Finished at: Sat Jul 21 11:34:53 GMT+05:30
2007[INFO] Final Memory: 3M/7M[INFO]
------------------------------------------------------------------------------

6. Now you need to create a jar. Making a JAR file is straight forward.
Just execute the following command:

C:\maventest\HelloMaven>mvn package

7. Now  to install the artifact you've generated (the JAR file) in your local repository (~/.m2/repository is the default location). If you take a look at the POM for your project you will notice the packaging element is set to jar. This is how Maven knows to produce a JAR file from the above command . You can now take a look in the ${basedir}/target directory and you will see the generated JAR file.  let's move on to installing our artifact. To do so execute the following command:

C:\maventest\HelloMaven>mvn install