A Simple Maven Project

Today we are discussing about how to create a simple Maven Project.
But, before we start discussing how to create a project, we need to know how to set up our eclipse with Maven. And, for that, visit this link - Install Maven into Eclipse

What is Maven and why we use it?
Well, Maven is a simple build tool. But what makes it more popular is that, if we need any jar for our project,we just have to mention all of them in the pom.xml of the project and it downloads all the jar dependencies automatically. We don't really need to include any jar in the project. And, so, the project size become small & easily transferable.











How Maven manages to download jars?
We mention the jar details in the pom.xml file as below:

   org.quartz-scheduler
   quartz-jobs
   2.2.1
  

We mention the jar name, jar version in the file. What maven does is, it searches it's own library (Maven has it's own repository online , and it is called Central Repository- here is the URL for Maven Central Repository ), it is available there , then downloads it and stores it in local repository. While, building the project next time, it will search for the jars in the local repository first, and, if it is not available there, then it will again try to download it from the Central repository. However, for very few case, maven does not have the required jar file in its library. In that case you need to manually download the jar from internet and put in the maven local repository folder, which is in most cases: C:\Users\{username}\.m2\repository which is a default path for repository for Maven.
The only disadvantage of Maven so far(in my opinion) is : Maven needs internet connection while compiling the project for downloading the jars.

Creating a Sample Maven Project

Step 1: Click File -> New -> Other -> Maven Project.

Click on Next.

Step 2: Use default Workspace location. Click on Next

Step 3: Select maven-srchetype-quickstart in Select Archetype section.


Click on Next.

Step 4: Type your desired package in Group Id & Project name in the Artifact Id.


Click on Finish.

Now, if you open your current project structure , you will find this :


pom.xml will look like this. In this file, we will mention the jar names we need to download. By default, dependencies for junit is already added in it.

  4.0.0

  com.example.src
  MyFirstMavenProject
  0.0.1-SNAPSHOT
  jar

  MyFirstMavenProject
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
  


How to compile a Maven Project ?
First Right click on the Project. Click on Run As -> Maven Build. The following dialogue will be opened.

Type "package" in Goals. Click Apply & then Click on Run. You will find this in the console.









SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building MyFirstMavenProject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ MyFirstMavenProject ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WORKSPACE\MyFirstMavenProject\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MyFirstMavenProject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ MyFirstMavenProject ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WORKSPACE\MyFirstMavenProject\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ MyFirstMavenProject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ MyFirstMavenProject ---
[INFO] Surefire report directory: D:\WORKSPACE\MyFirstMavenProject\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.src.MyFirstMavenProject.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ MyFirstMavenProject ---
[INFO] Building jar: D:\WORKSPACE\MyFirstMavenProject\target\MyFirstMavenProject-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.353s
[INFO] Finished at: Mon Jan 13 11:10:17 ICT 2014
[INFO] Final Memory: 7M/152M
[INFO] ------------------------------------------------------------------------

Now, to run the application, simple run the App.java file.
Right click on App.java -> Run As -> Java Application.

You will get the following output -> Hello World!
Download the sample project here


Happy Learning!!

No comments :

Post a Comment