Home > Maven > Maven notes – Exercise

Maven notes – Exercise

Setup Maven

  • Download latest Maven (3.3.9) from http://maven.apache.org/download.cgi
  • Unzip it to the folder you want to install Maven.
  • Add both M2_HOME and MAVEN_HOME variables in the Windows environment, and point it to your Maven folder (C:\Apache Maven\apache-maven-3.3.9).
  • Update PATH variable, append Maven bin folder – %M2_HOME%\bin, so that you can run the Maven’s command everywhere.
  • Restart/log off windows if necessary.
  • To verify it, run mvn –version in the command prompt.

Setup local repository path

<localRepository>D:\MAVEN_REPO</localRepository>

Default location for local repository is ${user.home}/.m2/repository. So I change it.

Create Simple Java Project

mvn archetype:generate -DgroupId={project-packaging} 
   -DartifactId={project-name} 
   -DarchetypeArtifactId=maven-archetype-quickstart 
   -DinteractiveMode=false

mvn archetype:generate -DgroupId=com.pegipegi 
   -DartifactId=MavenHelloWorld 
   -DarchetypeArtifactId=maven-archetype-quickstart 
   -DinteractiveMode=false

This tells Maven to create a Java project from the Maven maven-archetype-quickstart template. With mvn archetype:generate + maven-archetype-quickstart template, the following project directory structure is created. All source code puts in folder /src/main/java/, all unit test code puts in /src/test/java/. In additional, a standard pom.xml is generated.

maven directory structure

<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>com.pegipegi</groupId>
<artifactId>MavenHelloWorld</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MavenHelloWorld</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

As you can see the default packaging is jar. To build we use package phase (compile code and also package it).

mvnpackage

Run it

java -cp target/MavenHelloWorld-1.0-SNAPSHOT.jar com.pegipegi.App

Convert Maven based Java Project to support Eclipse IDE

mvn eclipse:eclipse

maveneclipse

Include custom library into maven local repository

There are still many jars that doesn’t support Maven. For the example oracle jdbc (previously not in maven central repo, but now there is).

Download jar from http://www.oracle.com/technetwork/apps-tech/jdbc-112010-090769.html

Create dependency

<groupId>com.oracle</groupId><artifactId>ojdbc14</artifactId><version>10.2.0.3.0</version>

Add it to local respository

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar -DgeneratePom=true

 

Categories: Maven
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: