Top

MAVEN - A Java build system

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted the by admin

Maven will help you to automate boring tasks of a java project. It will take in charge librairies versions, Eclipse project files, builds, and unit testing validation. This article intends to give you an overview of Maven usage in a simple project. Instead of its old parent ANT, Maven wants you to describe tasks to do, and not the way to do them. I believe this is a more interesting way to work.

1) Installing maven :

Installing Maven on Windows is really easy. It just needs to download the Install from the official website (http://maven.apache.org/), and to add its bin directory to your path. Now you can test your new Maven install with the following command line :

>Maven

During the installation, You have to creat Env variable for Maven :

MAVEN_HOME -> C:\Program Files\Apache Software Foundation\Maven 1.0.2

You should also configure in eclipse the following variable in order to have a good communication beetween Eclipse and Maven:

Window -> Preferences -> Java -> Build Path -> Classpath Variables -> New

ajouter : MAVEN_REPO -> your repository

(In my computer : C:`\Documents and Settings\Eric\.maven\repository)

Warning : The directory will be created at the first Maven execution.
2) Creating the project directory structure : Maven project has to follow a common directory structure: Maven Conventions Here is the target structure :

/+- src
/|  +- main
/|  |  +- java
/|  |  |  +- ...|  |  +- resources
/|  |     +- ...|  +- test
/|  |  +- java
/|  |  |  +- ...|  |  +- resources
/|  |     +- ...|  +- site
/|     +- xdoc
/|        +- ...+- target
/|  +- ...+- project.xml
+- README.tx
+- LICENSE.txt

3) Maven configuration file (project.xml) : project.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>	
	<!--		
		POM : Project Object Model.	
	-->	
	<pomVersion>1</pomVersion>	
	<groupId>tellaw.org</groupId>	
	<currentVersion>0.1-DEMO</currentVersion>	
	<organization />	
	<repository/>	
	<name>TagLib Sample</name>	
 
	<!--		
		This is the artifact to create
		In Maven, delivery item are artifacts.	
	-->	
	<artifactId>taglib_sample</artifactId>	
 
	<!--		
		Dependencies : Maven will keep them up to date in its local repository.		
		- It will download the correct version from a distant repository.		
		- Look at the property - WAR.BUNDLE = true, which let's you embed the dependency inside a war. 	
	-->	
	<dependencies>		
 
		<dependency>			
			<groupId>log4j</groupId>			
			<artifactId>log4j</artifactId>			
			<version>1.2.8</version>			
			<properties>				
				<war.bundle>true</war.bundle>			
			</properties>		
		</dependency>		
 
		<dependency>			
			<id>junit</id>			
			<version>3.8.1</version>		
		</dependency>		
 
		<dependency>			
			<groupId>servletapi</groupId>			
			<artifactId>servletapi</artifactId>			
			<version>2.4-20040521</version>		
		</dependency>		
 
		<dependency>			
			<groupId>jspapi</groupId>			
			<artifactId>jsp-api</artifactId>			
			<version>2.0-20040521</version>		
		</dependency>	
 
	</dependencies>	
 
	<!--		
		Project definition.	
	-->	
	<build>		
		<sourceDirectory>src/main/java</sourceDirectory>		
		<unitTestSourceDirectory>src/test/java</unitTestSourceDirectory>		
		<resources>			
			<resource>				
				<directory>src/main/webapp</directory>				
				<filtering>false</filtering>			
			</resource>		
		</resources>		
		<unitTest>			
			<includes>				
				<include>**/*Test.java</include>			
			</includes>		
		</unitTest>	
	</build>
</project>

4) Configuration of the project for eclipse workspace :

Each eclipse user know how boring it is to relink lost dependencies during project creation, and to check that their releases number is the good one for everybody

Maven can generate for you the “.project” and “.classpath” files of eclipse:

maven eclipse

Result :

  • Maven will check dependencies, and download the required one.
  • Maven will rebuild the files : .classpath et .project
  • Eclipse will use maven libraries.

5) Compiling and Artifact creation with maven : Compilation is really easy with maven :

maven war -> Creation of a war file (Compilation, Unit tests…).

maven jar -> creation of a jar file.

maven java:compile-> Compilation of the project.

maven site -> Creation of a documentation website about the project.

maven clean -> Clean the project build files.

maven eclipse:eclipse -> Build eclipse files.

Maven is a build tool, which will manage project dependencies for you. In every task, Maven will check the dependencies, and will try to resolve them if they are not present (or in wrong version).

In order to do this, Maven uses differents repositories, the biggest one, is IBiblio (http://www.ibiblio.org/maven/). A lot of Maven Plugins and options already exist, I cannot clearly list them here. This article intend to be a quickstart with Maven.

Usefull links :

Community Area - You can add comments here




Bottom