Maven Build Phases:
In any Application development process, once we are done with the coding, we need to build and deploy the code. We can use Maven for this purpose. Using maven we can build the application with just couple of commands (Ex : mvn compile, mvn package).These simple commands can take care of most of the things. If we have to compare it with Ant, we would have to specify everything that Ant has to do. Ex, if there were ten classes , we would instruct Ant to compile and package all the ten classes, of course using the wild cards to specify the class names. But every step has to be explicitly mentioned. It is not the same with Maven. It has a standard directory structure and it knows what needs to be done. For example, Maven assumes code inside /src/main to be the actual source code and the code inside /src/test to be the test cases. Depending on this, it compiles and packages the code inside /src/main and does not package the /src/test but uses the /src/test to run the test cases before packaging.
Build Life Cycle :
- Every build follows a life cycle – write the code, compile it, test it, package it and install it to an online repository so that it can be used other projects. This is an ideal build life cycle.
- It is split into different phases. Compile is a build phase, test is a build phase, etc. All these phases together comprise of build life cycle in Maven.
- If we do not explicitly configure build life cycle, Maven chooses the default life cycle (will be detailed later). For example, as explained above, compile assumes /main to be the default source code directory and /test to be the test cases directory. This is the default behavior of the compile phase has in Maven. Similar default behavior exists for other phases as well. Take for example 3 phases – compile, test and package. In order to do a package, we would ideally first need to do a compile and a test (test need not be necessary, if we want our code to be tested before packaging, we include test phase).
- The other default behavior Maven has is, once you specify a phase, it automatically executes the previous phases. If we specify package, Maven automatically runs the compile phase and the test phase.
Some build phases :
- validate : checks if everything is in order – configuration, pom.xml, the code. This is usually not called explicitly. Due to the default behavior of automatically calling the previous phases, it gets executed as a previous phase.
- compile : compiles all the java files into .class files. When we run the compile phase directly, it automatically runs the ‘validate’ phase.
- test : the phase where we run our test cases specified for the code. Say for example we have MyApp.java as our application class and MyAppTest.java as our test casefor MyApp java class, ‘test’ automatically knows that there is a test case for the code we are trying to compile and runs those test cases after the ‘compile’ phase. So again, if we directly run the ‘test’ phase, Maven automatically runs ‘validate’ and ‘compile’ before the ‘test’.
- package : packages all the .class files. Again executes all the other phases and packages. Suppose, there is a bug and the ‘test’ phase fails, there is no point in packaging the code. So ‘package’ executes only after ‘validate’, ‘compile’ and ‘test’ are successfully executed. It ends up in a .jar or .war or. ear file depending on what is specified in pom.xml.
- install : installs the packaged artifact into the local Maven repository. Note that it doesn’t install into the application server. install here is maven-specific.
- deploy : the final phase often confused due to the name it has. Like ‘install’, it doesn’t deploy the artifact into the application server, we run this phase when we want to publish our artifact to a remote repository where other users can download and consume it for their applications. This is not something we really when we develop applications unless the code we write can be shared on a repository.
In order to run any of these phases, we need to run the command – mvn <phaseName> in command prompt from the directory where pom.xml is present.
Ex : To do a compile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
C:\MyApp\MavenTestApp>mvn compile [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenTestApp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenTestApp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:\MyApp\MavenTestApp\target\classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.182 s [INFO] Finished at: 2015-07-23T23:13:46+05:30 [INFO] Final Memory: 10M/121M [INFO] ------------------------------------------------------------------------ C:\MyApp\MavenTestApp> |
‘compile’ checks for the projects, it finds that there is a pom.xml in the same directory. Then finds the .java files in the /src directory and compiles. There is only one source file in the example, that is compiled and saved as .class file in /target/classes.
To run test :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
C:\MyApp\MavenTestApp>mvn test [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenTestApp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenTestApp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenTestApp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenTestApp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenTestApp --- [INFO] Surefire report directory: C:\MyApp\MavenTestApp\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.org.j2eereference.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.312 s [INFO] Finished at: 2015-07-23T23:28:45+05:30 [INFO] Final Memory: 10M/219M [INFO] ------------------------------------------------------------------------ C:\MyApp\MavenTestApp> |
Maven pulls up all the test case available in the /test directory and runs them. From the logs we can find that, before executing the ‘test’, it tries to compile the code.
1 2 |
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp --- [INFO] Nothing to compile - all classes are up to date |
Since, the code was already compiled, it says there is nothing to compile and everything is up to date.
Similarly, we can package the code using the command- mvn package . This command again, tries to compile and test. On successful compilation and tests, it packages the artifact and places it in /target folder.
To install the artifact in the local repository – mvn install . This command runs from ‘validate’ phase all over again and installs the artifact to local repository on the machine. When we run this command, we can notice the logs and see something similar to below statements that confirms the artifact install into the local repository.
1 |
[INFO] Installing C:\MyApp\MavenTestApp\pom.xml to C:\Users\Admin\.m2\repository\com\org\j2eereference\MavenTestApp\1.0-SNAPSHOT\MavenTestApp-1.0-SNAPSHOT.pom |
We can notice that the default location of the local repository, i.e., /m2/repository.
Leave a Reply