Pages

Monday, June 7, 2010

Maven Lifecycle

Share it Please
In this article we will create a sample example and see how maven life cycle works

Start creating the project by executing the command

mvn archetype:generate -DgroupId=com.SampleExample.Example -DartifactId=Example -Dpackage=com.SampleExample.Example -Dversion=1.0-SNAPSHOT

While creating the project, it asks for the number .Enter “16” which is a archetype value 16 for a maven Quick start project. Some times after this it may ask for version, select your version. After this if asks for confirmation, enter “Y”.

So now the project Example is created which we can see in the place we created. Now the complete project structure is created.

Now go to Example directory and execute “mvn install”. Once the life cycle phase is executed , we can see the following output

[INFO] task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to C:\Example\target\classes
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to C:\Example\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\Example\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.SampleExample.Example.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] [jar:jar]
[INFO] Building jar: C:\Example\target\Example-1.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO]Installing C:\Example\target\Example-1.0-SNAPSHOT.jar to C:\xprk477\MAVEN\repository\com\SampleExample\Example\Example\1.0-SNAPSHO
T\Example-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
We can see all the default life cycles phases gets executed

[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to C:\Example\target\classes
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to C:\Example\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\Example\target\surefire-reports

As we discussed before once we execute a life cycle phase , the phases before them also gets executed.

A new Directory called “target” is created for the class files and output files based on the Maven default directory structure. At last the jar file with name in POM file also gets created.

Now we will delete the taget directory and execute a life cycle phase “mvn compile”.once compile phase is completed, we can see the target directory one more time, but will only classes directory in it with the compiled classes.

”mvn Package” allows you to create the jar file [set in POM file].

When we created the sample project Example , we can see a java file called “App.java” in the com.SampleExample.Example. now we will see how we can compile the java file and execute them.

Execute the “mvn compile” and then we will run the App.java class.In order to execute the java classes we will use a new plugin “exec:java” . Exec:java is a maven plugin which executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.It has one more goal called exec, which allows external programs to get executed.so how can we use this plugin

mvn exec:java -Dexec.mainClass=com.SampleExample.Example.App

Once executed , we can see the following info

C:\Example>mvn exec:java -Dexec.mainClass=com.SampleExample.Example.App
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] artifact org.codehaus.mojo:exec-maven-plugin: checking for updates from central
[INFO] ------------------------------------------------------------------------
[INFO] Building Example
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[INFO] [exec:java]
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL

We can see the output “Hello World” in the console.

Now lets change the App class a little bit so that we can send arguments to the program
I just added a println statement to print the arguments that we pass.we can send the arguments to a program using

mvn exec:java -Dexec.mainClass=com.SampleExample.Example.App -Dexec.args="kill"

we can see the info in console as

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Example
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[INFO] [exec:java]
kill
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL

We can see the “kill” argument is displayed in the output.

If we need to find help for a plugin we can use the help plugin , if we need more info about exec plugin we execute

mvn help:describe -Dplugin=exec –Dfull

This will display all the information about exec plugin.

Exploring the Dependencies – there are many times when we need to see what jars are included In our class path.we can explore the dependencies by running
“mvn dependency:resolve”

Once we execute this we can see the dependencies in the console as



[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Example
[INFO] task-segment: [dependency:resolve]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:resolve]
[INFO]
[INFO] The following files have been resolved:
[INFO] junit:junit:jar:3.8.1:test
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL


We can see the junit library dependency in our project.if we need to see the entire dependency tree of our project we can execute
“mvn dependency:tree”

This shows all the dependencies in a tree structure

Unit Test
We have a sample junit example AppTest.java available in the test folder. we can run the tests by executing the “mvn test”.when we see the POM file we will see that junit library is set to test scope which says that the dependency that is available on the classpath only during test compilation and test execution.

If we need to skip the junits completely we can use “mvn install -Dmaven.test.skip=true”

So more articles coming , So Happy Coding.......


No comments :

Post a Comment