Maven: No need to write build script and manage dependencies
----------
Project management and comprehension tool based on concept of [POM] --> Project Object Model.
Maven -Manages--> [Build,Dependecies,SCMs] Releases,distribution, Reporting and Documentation.
a) Maven provides developers a [complete build life cycle framework].
b) Maven uses [**standard directory layout and ** default build cycle].
c) Maven project structure and contents are mentioned in pom.xml[project object model].
d) When Maven project created, maven creates a standard project structure, deveopers are required only to place files in those folders.
e) ${basedir}/src/main/java/ -> Source code.
${basedir}/src/main/resources --> resources.
${basedir}/src/test --> Test code.
${basedir}/src/test/resources --> Test resources.
${basedir}/src/target --> distibutable jar.
${basedir}/src/target/classes --> compiled class files.
7) In order to build the project, maven provides developers options to mention [life-cycle goals] and [project dependencies](in pom.xml).
8) Export M2_HOME=export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.3 and add bin folder to PATH to run maven build from anywhere in sys.
9)POM.xml -> It's XML file
a) Always resides in [base directory] of project.
b) While executing maven task/goal, maven looks for pom.xml in current directory.
c) POM might contains below information:
a) project dependencies b) plugins(compiler plugin etc) c) goals(custom goals) d) build profiles (dev,prod,test) e) project version(under project element witch is root element) f) developers d) mailing list
We can modify, project folder structure
**d) Before creating POM, we need to come up with project group(groupid),project name(artifact) and version for [unique] identification of project in repository.
e) It's allowed only one POM file per project.
f) All POMs inherit from a parent POM known as super POM.
g) Maven provides different archtypes to ssupport diff project structures.
Build Life cycle:
-------------------
Build Life Cycle Contains Phases --> Phases contains Goals
1) Typical build cycle contains following phases:
a) prepare-resources (Resource copying can be customized in this phase.) b) compile (just compile source) c) package (build jar/war file) d) install (upload artifacts to local/remote repository)
2) Every phase has pre and post phases to register goals.
c) Maven has 3 standard life cycles: a) clean b) default or build d) site (Documentation)
d) Goal is a specific task which contributes to the build.
e) Order of invocation matters
clean phase -> dependency goal -> package phase
mvn clean dependency:copy-dependencies package
| | |
Phase -> Goal -> Phase
f) Default/Build life cycle : Has 23 phases
Build Profiles:
----------------
a) Build profiles can be used to distinguish build definations for diff env like prod,dev and UAT
b) Build profiles are three types:
Per Project -> Defined in project pom.xml
Per User -> Defined in Users/username/.m2/settings.xml
Global -> %M2_HOME%/conf/settings.xml
mvn phase -Pprod
c) Profile can be activated in pom.xml using activeProfiles tag.
Repository:
----------------
a) Repository is a place, all project jars,library jars,plugins resides.
b) 3 types: Local,Central and Remote
Local: i) Gets created in %USER_HOME% folder.
ii) Maven keeps all project dependencies in local repository when mvn build runs.
iii) Path can be overridden by mentioning path in settings.xml in %M2_HOME%/conf folder.
Central: Provoded by maven community; http://repo1.maven.org/maven/
Remote: Developers own custom library
Order of reference for dependencies: Local -> Central -> Remote
Plugins:
------------
1) Maven is actually plugin execution framework where every task is done by plugins
2) Ex: Creating jar file,create war file,compile source code,unit testing code,create documentation and project reports
3) Plugins usually provides multiple goals can be executed like this: [mvn plugin-name:goal-name]
For ex: Java project can be compiled by using mvn-compiler-plugin's mvn compiler:compile
4) PLugins are two types: Build plugins (under /build) and reporting plugins(under /reporting)
5) in POM.xml <plugins><plugin><executions><execution><goals><goal></goal><goals><configuration><tasks></tasks></configuration><executions><execution></plugin></plugins>
Creating Projects:
-------------------
1) Maven uses archtype plugin to create projects.
External Dependencies:
-----------------------
We can add lib folder with jars to maven to refer when those jars are not found in all 3 repos.
<dependency><groupid></groupid><artifactid></artifactid><version></version><scope>system</scope><systempath></systempath></dependency>
-U to force maven to download latest dependencies
****** if version contains snapshot, it means maven fetch copy from repo for every build. unlike normal version case
Build automation: Kick off build of dependent projects, once current build is done successfully.
This can be acieved by modifying pom.xml or else using continuous integration(CI) hudson
Whenever SVN check in happens, it will trigger build and then triggers dependent builds.
Transitive Dependencies Discovery:
----------------------------------------------
It is pretty often a case, when a library say A depends upon other library say B. In case another project C want to use A then that project requires to use library B too.
Maven helps to avoid such requirement to discover all the libraries required. Maven does so by reading project files(pom.xml) of dependencies, figure out their dependencies and so on.
We only need to define [direct dependency] in each project pom. Maven handles the rest automatically.
Common dependencies can be placed at single place using concept of parent pom.Dependencies
Scope Description
compile This scope indicates that dependency is available in classpath of project. It is default scope.
provided This scope indicates that dependency is to be provided by JDK or web-Server/Container at runtime
runtime This scope indicates that dependency is not required for compilation, but is required during execution.
test This scope indicates that the dependency is only available for the test compilation and execution phases.
system This scope indicates that you have to provide the system path.
import This scope is only used when dependency is of type pom. This scopes indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section.
mvn dependency:tree
----------
Project management and comprehension tool based on concept of [POM] --> Project Object Model.
Maven -Manages--> [Build,Dependecies,SCMs] Releases,distribution, Reporting and Documentation.
a) Maven provides developers a [complete build life cycle framework].
b) Maven uses [**standard directory layout and ** default build cycle].
c) Maven project structure and contents are mentioned in pom.xml[project object model].
d) When Maven project created, maven creates a standard project structure, deveopers are required only to place files in those folders.
e) ${basedir}/src/main/java/ -> Source code.
${basedir}/src/main/resources --> resources.
${basedir}/src/test --> Test code.
${basedir}/src/test/resources --> Test resources.
${basedir}/src/target --> distibutable jar.
${basedir}/src/target/classes --> compiled class files.
7) In order to build the project, maven provides developers options to mention [life-cycle goals] and [project dependencies](in pom.xml).
8) Export M2_HOME=export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.3 and add bin folder to PATH to run maven build from anywhere in sys.
9)POM.xml -> It's XML file
a) Always resides in [base directory] of project.
b) While executing maven task/goal, maven looks for pom.xml in current directory.
c) POM might contains below information:
a) project dependencies b) plugins(compiler plugin etc) c) goals(custom goals) d) build profiles (dev,prod,test) e) project version(under project element witch is root element) f) developers d) mailing list
We can modify, project folder structure
**d) Before creating POM, we need to come up with project group(groupid),project name(artifact) and version for [unique] identification of project in repository.
e) It's allowed only one POM file per project.
f) All POMs inherit from a parent POM known as super POM.
g) Maven provides different archtypes to ssupport diff project structures.
Build Life cycle:
-------------------
Build Life Cycle Contains Phases --> Phases contains Goals
1) Typical build cycle contains following phases:
a) prepare-resources (Resource copying can be customized in this phase.) b) compile (just compile source) c) package (build jar/war file) d) install (upload artifacts to local/remote repository)
2) Every phase has pre and post phases to register goals.
c) Maven has 3 standard life cycles: a) clean b) default or build d) site (Documentation)
d) Goal is a specific task which contributes to the build.
e) Order of invocation matters
clean phase -> dependency goal -> package phase
mvn clean dependency:copy-dependencies package
| | |
Phase -> Goal -> Phase
f) Default/Build life cycle : Has 23 phases
Build Profiles:
----------------
a) Build profiles can be used to distinguish build definations for diff env like prod,dev and UAT
b) Build profiles are three types:
Per Project -> Defined in project pom.xml
Per User -> Defined in Users/username/.m2/settings.xml
Global -> %M2_HOME%/conf/settings.xml
mvn phase -Pprod
c) Profile can be activated in pom.xml using activeProfiles tag.
Repository:
----------------
a) Repository is a place, all project jars,library jars,plugins resides.
b) 3 types: Local,Central and Remote
Local: i) Gets created in %USER_HOME% folder.
ii) Maven keeps all project dependencies in local repository when mvn build runs.
iii) Path can be overridden by mentioning path in settings.xml in %M2_HOME%/conf folder.
Central: Provoded by maven community; http://repo1.maven.org/maven/
Remote: Developers own custom library
Order of reference for dependencies: Local -> Central -> Remote
Plugins:
------------
1) Maven is actually plugin execution framework where every task is done by plugins
2) Ex: Creating jar file,create war file,compile source code,unit testing code,create documentation and project reports
3) Plugins usually provides multiple goals can be executed like this: [mvn plugin-name:goal-name]
For ex: Java project can be compiled by using mvn-compiler-plugin's mvn compiler:compile
4) PLugins are two types: Build plugins (under /build) and reporting plugins(under /reporting)
5) in POM.xml <plugins><plugin><executions><execution><goals><goal></goal><goals><configuration><tasks></tasks></configuration><executions><execution></plugin></plugins>
Creating Projects:
-------------------
1) Maven uses archtype plugin to create projects.
External Dependencies:
-----------------------
We can add lib folder with jars to maven to refer when those jars are not found in all 3 repos.
<dependency><groupid></groupid><artifactid></artifactid><version></version><scope>system</scope><systempath></systempath></dependency>
-U to force maven to download latest dependencies
****** if version contains snapshot, it means maven fetch copy from repo for every build. unlike normal version case
Build automation: Kick off build of dependent projects, once current build is done successfully.
This can be acieved by modifying pom.xml or else using continuous integration(CI) hudson
Whenever SVN check in happens, it will trigger build and then triggers dependent builds.
Transitive Dependencies Discovery:
----------------------------------------------
It is pretty often a case, when a library say A depends upon other library say B. In case another project C want to use A then that project requires to use library B too.
Maven helps to avoid such requirement to discover all the libraries required. Maven does so by reading project files(pom.xml) of dependencies, figure out their dependencies and so on.
We only need to define [direct dependency] in each project pom. Maven handles the rest automatically.
Common dependencies can be placed at single place using concept of parent pom.Dependencies
Scope Description
compile This scope indicates that dependency is available in classpath of project. It is default scope.
provided This scope indicates that dependency is to be provided by JDK or web-Server/Container at runtime
runtime This scope indicates that dependency is not required for compilation, but is required during execution.
test This scope indicates that the dependency is only available for the test compilation and execution phases.
system This scope indicates that you have to provide the system path.
import This scope is only used when dependency is of type pom. This scopes indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section.
mvn dependency:tree
No comments:
Post a Comment