Starting a Java Project with Maven – Step by step Guide for Mac

Apache Maven Logo

Thanks to my friend, Gandos, he got me interested to use Maven after I read his blog post (It’s in Indonesian). After consulting to another friend, I think it’s a good idea to use Maven for the LabGence project.

The idea is also influenced from this question that I have, “What is the best practice to upload an open-source project?” and “What files that I should and probably shouldn’t upload?”.

I believe Maven can standardize the issue of libraries needed by the project, and also the build script needed. So I decided to give it a try.

Fortunately I found the best and brief introduction tutorial to Maven on JavaBrains.org, you might want to check it out.

I also created the following step by step notes for Mac OSX (which might be applicable also to Linux and other Unix OS).

Maven Installation
Steps:
1. Download Maven binary from the official website. I use the Maven 3.0.3 as of this writing.

2. Extract the compressed files.

3. Execute these commands:
# export M2_HOME=[path to the Maven extracted directory]
example: # export M2_HOME=/home/djitz/dev/java/maven

# export PATH=[path to the Maven extracted directory]/bin:${PATH}
example: # export PATH=/home/djitz/dev/java/maven/bin:${PATH}

4. Test the maven installation, execute:
# mvn --v

If it shows something like:

Apache Maven 3.0.2 (r1056850; 2011-01-08 19:58:10-0500)
Java version: 1.6.0_24, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.6.5", arch: "x86_64", family: "mac"

then we’re done for the installation.

Creating Project
Steps:
1. Go to the directory where we want to have the project.

2. Execute
# mvn archetype:generate

It will download files because it’s the first time we’re running it.

3. After it finished the download, it will ask for number, and show us the default number. Scroll up and we can see hundreds of different project templates that Maven knows.

For now, just press Enter to accept the default one.

4. Choose the Maven archetype quickstart version, just press Enter to accept the default.

5. For the groupId, input your package name.
example: com.djitz.labgence

6. For the artifactId, input your application name.
example: LabGence

7. Type the version, just press Enter to accept the default.

8. Type the package name, it will copy the value from step #5.

9. Confirm the values from step 5 – 8.

That’s it, we successfully created a new project using Maven.

Some more commands to try, go to the created project directory, then do the following commands:

# mvn compile
# mvn package

There are a lot more functions of Maven, but this can get us started. Sonatype.com does a lot of work with Maven, and they have a free Maven reference book in here. (http://www.sonatype.com/books/mvnref-book/reference/index.html
)

Until the next post fellas! :)


Popularity: 3% [?]

Tags: ,

Related Posts


1 comment so far ↓

#1 Eric F on 01.06.12 at 1:08 pm

Word!

Leave a Comment