Menu

Tuesday, June 21, 2011

Learn Maven


What is Maven?
How to install Maven in Windows?
Maven project directory structure
How to create a simple Project using maven?
How to package a Project using Maven?
How to add dependency jar file?
How to include resources in the jar file?

                Maven is a project management tool which is used to build and package the project.

How to install Maven in Windows?
  
1.       Download Maven from the below site.
http://maven.apache.org/download.html - The latest version that I downloaded was apache-maven-3.0.3-bin.zip

2.       Extract the downloaded file. Consider that I have extracted it into the below directory
                  C:\Softwares\Apache Products\Maven\apache-maven-3.0.3

3.       Set the necessary environment variables.
M2_HOME=C:\Softwares\Apache Products\Maven\apache-maven-3.0.3 – Home directory
M2= %M2_HOME%\bin                                                 – Bin directory of the Maven
MAVEN_OPTS= -Xms256m -Xmx512m                        – This is optional
PATH=%M2%                                                                 – Maven bin directory in the path variable

4.       Check the status of the Maven in the command line.
> mvn --version
The output will be like this.
             

                Now the Maven has been installed successfully in the windows system.


Maven project directory structure

src                      - Source code will be here.
target                  - When you do package, maven package the project and will keep  it here.
pom.xml              - Maven Project configuration file.

src/main/java                                - Source code
src/main/resources                        - Resources folder (properties files location)
src/main/filters                              - Resource filter files
src/main/assembly                         - Assembly descriptors
src/main/config                             - Configuration files location
src/main/webapp                          - Web application resource folder
src/test/java                                  - Test Source code
src/test/resources                          - Test Resources folder (properties files location)
src/test/filters                                 - Test filters folder
src/site                                          - Site

All the information to build the project should be entered into the pom.xml file which is a project configuration file in Maven.

Structure of the pom.xml file



How to create a simple Project using maven?

In Maven each task will be considered as a goal. The below goal (archetype:generate) is to create a project called maven-app
> mvn archetype:generate -DgroupId=com.mycompany.app  
-DartifactId=maven-app
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

How to package a Project using Maven?
> mvn package

How to add dependency jar file?
Consider you have used classes from the log4j and commons-lang in your application, and then those dependency jar files should be entered in the pom.xml file as shown. If it is not included, it will throw error when you package the application.
<project . . . >
. . .

<dependencies>
      <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>compile</scope>
      </dependency>    
      <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
            <scope>compile</scope>
      </dependency>
</dependencies>

<build>
<plugins>
<plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                  <execution>
                        <id>${project.artifactId}-fetch-deps</id>
                        <phase>generate-sources</phase>
                        <goals>
                              <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                              <outputDirectory>
${project.build.outputDirectory}
</outputDirectory>
                              <stripVersion>true</stripVersion>
                              <excludeTransitive>true</excludeTransitive>
                              <excludeArtifactIds>junit</excludeArtifactIds>
                        </configuration>
                  </execution>
            </executions>
      </plugin>
</plugins>
</build>
. . .
</project>

How to include resources in the jar file?
Consider all the properties file are in the src/main/resources folder
<project …>
      . . .
<build>
            <resources>
                  <resource>
                        <directory>src/main/resources</directory>
                  </resource>
            </resources>
</build>
. . .
</project>