Monday, May 23, 2016

OREILLY Learning Apache Maven

Maven has 3 lifecycles: Clean, default and site.
in each lifecycles we have alot of phases, executing a phase means executes all the previous phases.

Maven is convention over configuration, which means you dont tell maven in some configuration where your java files are, by convention they must be in src/main/java

You can change these convention but it is not recommended, you may need that if you are working on a legacy application

you can have a Terminal inside eclipse, use TCF Terminal plugin

Inheritance in Maven
in the general pom all the directories and conventional stuff are defined


Maven profile
you can use Maven profile to build the project based on your environment, for example a profile for the test environment, and another for DEV another for production.

now when you run maven use -P
mvn -Pproduction package


if you dont want to use -P you can do something else
you can define an Environment Variable in windows and then use it in pom.xml


now Maven will check PACKAGE_ENV environment variable and determine which profile to use


Maven Dependency

Maven can handle transitive dependency which means if you depend on X.jar and X.jar depends on Y.jar, Maven will fetch Y.jar

You can define Remote repositories in Maven.

You can define Scope for your dependencies, which means you dont need junit when you compile you need it when you test

Maven can handle conflicts, for example you depend on X.jar and Y.jar, X.jar depends on Z.jar version1 and Y.jar depends on Z.jar version2 . Maven can handle this conflict.
it will fetch the latest version by default, however you can control this behaviour using the <exclusion> tag.

Maven Lifecycles 
there are 3 different life cycles in Maven, default, clean and site
cycles have phases
phases are connected to plugins, for each plugin you have goals that must pass in order for the phase to pass

default is the most used cycle
in default you have these phases:
Compile: which means compile everything in src/main/java
test-compiles: compile everything in src/main/test
test: run unit test
package: create the jar or ear or war
install: take the generated package and put it in local repository so other project can use it as dependeny
deploy: take the package and put it in remote repository, so other teams in the company can use it