Maven Tutorial Part One

maven is a build tool for the java projects. It speed up the java development build process.maven is one of the java tool for continuous build integration. Maven has predefined phases or goals. The goals are similar to ant targets. developer will invoke goals to do their tasks.

Please make sure that you download maven from the apache software and install it.

what are the things to considere while making your java projects to implement maven as build tool
      project object model file
      java project folder structure
Hello World maven applicaiton:- I am going to createa HelloWorld java application.
mvn archetype:generate -DgroupId=com.techrocksz -DartifactId=HelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Output of this mvn command:-

E:\techrocksz>mvn archetype:generate -DgroupId=com.techrocksz -DartifactId=HelloWorld -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Interactive mode
Define value for version:  1.0-SNAPSHOT: : 1.0.0-SNAPSHOT
Confirm properties configuration:
groupId: com.techrocksz
artifactId: HelloWorld
version: 1.0.0-SNAPSHOT
package: com.techrocksz
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.techrocksz
[INFO] Parameter: packageName, Value: com.techrocksz
[INFO] Parameter: package, Value: com.techrocksz
[INFO] Parameter: artifactId, Value: HelloWorld
[INFO] Parameter: basedir, Value: E:\techrocksz
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: E:\techrocksz\HelloWorld
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30 seconds
[INFO] Finished at: Fri Dec 02 10:47:29 GMT+05:30 2011
[INFO] Final Memory: 8M/254M
[INFO] ------------------------------------------------------------------------

In the above example created,see the folder structure, In the root folder we have folder named 'src' and file pom.xml and also we have following sub directories

src\main\java :- This folder contains all your java classes src\test\java :-This folder contains all your java test related classes and also we have com\techrocksz\ and com\techrocksz\ files which is a HelloWorld applicaiton and it's test class

what is pom.xml:- pom.xml is a project object model configuration file which is used by maven.

Maven uses pom.xml which contains all details about your project as well as the configuration details.


In the above, groupId represents the package of the project where ExampleDemo-1.0.0.jar file is created artifactId is the project name as well as jar/war name of the module. packaging specify whether this module is webapplication i.e war or java module i.e jar. if packaging option is not specified, maven consider this as jar module. dependencies tag specifies the what are dependencies this project has. here as we have written App, by default junit dependency is added as part of sample project. Running Maven Project:-
  1. cd HelloWorld
  2. mvn clean install
maven execution process:-

whenever you executed maven install command, we configured repositories location in pom.xml and the dependencies, For the first time it tries to download different plugins and dependencies from the repository and copy those dependicies to folder (your user profile of your document and Settings folder(i.e C:\Documents and Settings\username\.m2\repository). This is time consuming for the first time as it tries to download all your plugins to your local time onward, it will download those already downloaded jars and executes quickly

. Proxy settings to be enabling for maven

settings.xml in folder C:\Documents and Settings\username\.m2\ have to be created for this.

Here is the output of the command.
E:\techrocksz\HelloWorld>mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorld
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory E:\techrocksz\HelloWorld\target
[INFO] [resources:resources]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\techrocksz\HelloWorld\src\main\resources
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to E:\techrocksz\HelloWorld\target\classes
[INFO] [resources:testResources]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\techrocksz\HelloWorld\src\test\resources
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to E:\techrocksz\HelloWorld\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: E:\techrocksz\HelloWorld\target\surefire-reports

 T E S T S
Running com.techrocksz.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec

Results :

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

[INFO] [jar:jar]
[INFO] Building jar: E:\techrocksz\HelloWorld\target\HelloWorld-1.0.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO] Installing E:\techrocksz\HelloWorld\target\HelloWorld-1.0.0-SNAPSHOT.jar to C:\Documents and Settings\kinturu\.m2\repository\com\techrocksz\HelloWorld\1.0.0-SNAPSHOT\HelloWorld-1.0.0-SNAPSHOT.j
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Fri Dec 02 11:58:46 GMT+05:30 2011
[INFO] Final Memory: 15M/254M
[INFO] ------------------------------------------------------------------------

Finally your module HelloWorld-1.0.0-SNAPSHOT.jar is created. Now Any other app is ready to use this module. Finally learning maven is started. Please leave a comment if you have any questions.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.