Basic Maven lifecycle

Maven’s lifecycle:

Lifecycle Reference

The following lists all build phases of the default, clean and site lifecycle, which are executed in the order given up to the point of the one specified.

Clean Lifecycle

pre-clean executes processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean executes processes needed to finalize the project cleaning

Default Lifecycle

validate validate the project is correct and all necessary information is available.
initialize initialize build state, e.g. set properties or create directories.
generate-sources generate any source code for inclusion in compilation.
process-sources process the source code, for example to filter any values.
generate-resources generate resources for inclusion in the package.
process-resources copy and process the resources into the destination directory, ready for packaging.
compile compile the source code of the project.
process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sources generate any test source code for inclusion in compilation.
process-test-sources process the test source code, for example to filter any values.
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code into the test destination directory
process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify run any checks to verify the package is valid and meets quality criteria.
install install the package into the local repository, for use as a dependency in other projects locally.
deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

In this post, I will run ‘clean’, ‘compile’, ‘test’, ‘install’ and ‘deploy’.

I am using code from https://github.com/velniukas/helloworld to do the experience.

1. mvn compile

C:\Users\luhuang>D:

D:\>cd Source\helloworld-master

D:\Source\helloworld-master>mvn compile
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building HelloWorld 1.0
[INFO] ————————————————————————
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-
compiler-plugin/2.3.2/maven-compiler-plugin-2.3.2.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-c
ompiler-plugin/2.3.2/maven-compiler-plugin-2.3.2.pom (8 KB at 1.6 KB/sec)

[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 1 source file to D:\Source\helloworld-master\target\classes
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 30.922 s
[INFO] Finished at: 2014-06-28T23:01:51+08:00
[INFO] Final Memory: 10M/121M
[INFO] ————————————————————————

Result:

D:\Source\helloworld-master\target\classes>dir
Volume in drive D is Data
Volume Serial Number is AE56-409D

Directory of D:\Source\helloworld-master\target\classes

06/28/2014 11:01 PM <DIR> .
06/28/2014 11:01 PM <DIR> ..
06/28/2014 11:01 PM 1,154 HelloWorld.class
1 File(s) 1,154 bytes
2 Dir(s) 36,699,480,064 bytes free

D:\Source\helloworld-master\target\classes>

 

2. mvn test

D:\Source\helloworld-master>mvn test
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building HelloWorld 1.0
[INFO] ————————————————————————
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-
surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom

——————————————————-
T E S T S
——————————————————-
Running HelloWorldTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec

Results :

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

[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 46.004 s
[INFO] Finished at: 2014-06-28T23:05:14+08:00
[INFO] Final Memory: 11M/126M
[INFO] ————————————————————————
D:\Source\helloworld-master>

Check its repository,

C:\Users\luhuang\.m2\repository\junit\junit\4.11>dir
Volume in drive C is System
Volume Serial Number is 4499-5BD0

Directory of C:\Users\luhuang\.m2\repository\junit\junit\4.11

06/28/2014 06:29 PM <DIR> .
06/28/2014 06:29 PM <DIR> ..
06/28/2014 06:29 PM 245,039 junit-4.11.jar
06/28/2014 06:29 PM 40 junit-4.11.jar.sha1
06/28/2014 06:24 PM 2,344 junit-4.11.pom
06/28/2014 06:24 PM 40 junit-4.11.pom.sha1
06/28/2014 06:29 PM 185 _remote.repositories
5 File(s) 247,648 bytes
2 Dir(s) 52,974,571,520 bytes free

C:\Users\luhuang\.m2\repository\junit\junit\4.11>

It is because of setting in pom.xml


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

 

3. mvn install

D:\Source\helloworld-master>mvn install
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building HelloWorld 1.0
[INFO] ————————————————————————
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-
jar-plugin/2.4/maven-jar-plugin-2.4.pom

[INFO] — maven-compiler-plugin:2.3.2:compile (default-compile) @ helloworld —

[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ he
lloworld —
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Source\helloworld-master\src\test\
resources
[INFO]
[INFO] — maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello
world —
[INFO] Nothing to compile – all classes are up to date

——————————————————-
T E S T S
——————————————————-
Running HelloWorldTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec

Results :

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

[INFO] Installing D:\Source\helloworld-master\pom.xml to C:\Users\luhuang\.m2\re
pository\HelloWorld\helloworld\1.0\helloworld-1.0.pom
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 36.489 s
[INFO] Finished at: 2014-06-28T23:20:08+08:00
[INFO] Final Memory: 11M/124M
[INFO] ————————————————————————

In Maven, build phases (plus the other build phases not shown here) are executed sequentially to complete the default lifecycle. Given the build phases above, this means that when the default lifecycle is used, Maven will first validate the project, then will try to compile the sources, run those against the tests, package the binaries (e.g. jar), run integration tests against that package, verify the package, install the verifed package to the local repository, then deploy the installed package in a specified environment. Here it skipped the build of code as it has no update, and then ran test following by an install target. If developers want to run compile in any case, then a clean target could be run firstly.

C:\Users\luhuang\.m2\repository\junit\junit\4.11>cd C:\Users\luhuang\.m2\reposit
ory\HelloWorld\helloworld\1.0

C:\Users\luhuang\.m2\repository\HelloWorld\helloworld\1.0>dir
Volume in drive C is System
Volume Serial Number is 4499-5BD0

Directory of C:\Users\luhuang\.m2\repository\HelloWorld\helloworld\1.0

06/28/2014 11:20 PM <DIR> .
06/28/2014 11:20 PM <DIR> ..
06/28/2014 11:19 PM 2,228 helloworld-1.0.jar
06/28/2014 10:57 PM 804 helloworld-1.0.pom
06/28/2014 11:20 PM 179 _remote.repositories
3 File(s) 3,211 bytes
2 Dir(s) 52,961,185,792 bytes free

C:\Users\luhuang\.m2\repository\HelloWorld\helloworld\1.0>

<modelVersion>4.0.0</modelVersion>
<groupId>HelloWorld</groupId>
<artifactId>helloworld</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>HelloWorld</name>

So here, the .jar file name is helloworld. Version is 1.0. Package type is jar, hence its full name is helloworld-1.0.jar, its group id is HelloWorld.

 

Summary

In this post, mvn build source codes, run junit testings, and install it into repository, for use as a dependency in other projects locally.