Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Maven
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

As you are discussing maven 2, shouldn't the environment variable in the first paragraph be M2_HOME instead of MAVEN_HOME?

Posted by Paul on Monday, 05.12.08 @ 20:45pm | #59499

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.