Maven Tutorial – 3

Lets create a simple application as we have seen in Tutorial -2.

Execute the following command. Ensure you have jdk bin and maven bin folders in path!

$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.grassfield.app -DartifactId=my-app

We are generating a simple project based on generate architype – a simple template. After issuing the above command, you will see a list of lines and characters are running on the screen. You will be asked some questions, just press enter!

maven01

You can see a new folder is created with your artifact id, which is ‘my-app’ here! If you browse through this folder, you will see the following.

$ ls
my-app
$ cd my-app/
$ ls
pom.xml src
$ cd src
$ ls
main test
$ cd main
$ ls
java
$ cd java
$ ls
org
$ cd org/grassfield/app/
$ ls
App.java

App.java – Created by Maven

maven02

Lets modify it now.

Here it is!

maven03

Lets see what’s written inside pom.xml

pandian@pandian-SH560:~/pandian/my-app$ cat 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.grassfield.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>my-app</name>
<url>http://maven.apache.org</url&gt;

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

POM refers to junit alone as its dependency!

Now lets compile it.

$ mvn compile

maven04

Done!

Lets test it now! The following is the maven test case.

maven05

$ mvn test

maven06

Lets package it now!

$ mvn package

maven08

Console is interesting. But you love Eclipse IDE like, right? Lets create the Eclipse project structure for this artifact.

$ mvn eclipse:eclipse

maven09

Now import the java project from the location highlighted above.

maven10 - Eclipse IDE

Lets see how to work with Eclipse using Maven in my next post.

Related posts:

Maven – 1

Maven – 2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s