Overview of buildscript maven:-
Maven takes convention over configuration means developers are not required to create the build process themselves.
when a maven project is created its create the default project directory structure under which the developers has to put the files accordingly and need not has to define any configuration in the pom.xml file.
SET OF MAVEN ENVIRONMENT
We need to add M2_HOME,M2,MAVEN_OPTS
About pom.xml Pom stands for Project Object Model it contains the information about the project and various configuration details below are the some of the configuration which are specified in pom.xml:-
* Plugins
* Goals
* build profiles
* Project versions
* developers
* mailing list
Before creating the pom.xml we should appropriately define these project values (groupID,artifactID and its version to indentify the project in repository
Below are the basic for pom files:
* All pom files required the project element and three mandatory fields
groupID,artifactID,version
*Project notation in repository is groupID:artifatcID:version
*Root element of pom.xml is project and it has three major sub-nodes
MAVEN PROJECT TEAMPLATE
Archetype is the maven plugin which creates the project as per the given template below is the example for creating a webapp application using maven template:-
In this pom.xml we have used the tomcat deploy plugn org.apache.tomcat.maven to deploy the generated war file to the configured tomcat.
GOALS/PHASES:
Profiles in maven
Sometime artifact contents needs to be changed as per the different environments so for this kind of requirement we have build profile where particular profile is taken under consideration while building the project as per the profile input:
command to be executed:
Maven repository are of three types
Maven local repository keeps your project's all dependencies (library jars, plugin jars etc). When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository.It helps to avoid references to dependencies stored on remote machine every time a project is build.
Maven local repository by default get created by Maven in %USER_HOME% directory. To override the default location, mention another path in Maven settings.xml file available at %M2_HOME%\conf directory.
When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/
Key concepts of Central repository
For example, using below mentioned POM.xml,Maven will download dependency (not available in central repository) from Remote Repositories mentioned in the same pom.xml.
Maven takes convention over configuration means developers are not required to create the build process themselves.
when a maven project is created its create the default project directory structure under which the developers has to put the files accordingly and need not has to define any configuration in the pom.xml file.
SET OF MAVEN ENVIRONMENT
We need to add M2_HOME,M2,MAVEN_OPTS
About pom.xml Pom stands for Project Object Model it contains the information about the project and various configuration details below are the some of the configuration which are specified in pom.xml:-
* Plugins
* Goals
* build profiles
* Project versions
* developers
* mailing list
Before creating the pom.xml we should appropriately define these project values (groupID,artifactID and its version to indentify the project in repository
Below are the basic for pom files:
* All pom files required the project element and three mandatory fields
groupID,artifactID,version
*Project notation in repository is groupID:artifatcID:version
*Root element of pom.xml is project and it has three major sub-nodes
| Node | Description |
| groupId | This is an Id of project's group. This is generally unique amongst an organization or a project. For example, a banking group com.company.bank has all bank related projects. |
| artifactId | This is an Id of the project. This is generally name of the project. For example, consumer-banking. Along with the groupId, the artifactId defines the artifact's location within the repository. |
| version |
This
is the version of the project.Along with the groupId, It is used
within an artifact's repository to separate versions from each
other. For example:
com.company.bank:consumer-banking:1.0
com.company.bank:consumer-banking:1.1.
|
MAVEN PROJECT TEAMPLATE
Archetype is the maven plugin which creates the project as per the given template below is the example for creating a webapp application using maven template:-
mvn archetype:generate
-DgroupId=com.mkyong
-DartifactId=CounterWebApp
-DarchetypeArtifactId=maven-archetype-webapp
-DinteractiveMode=false
SAMPLE POM.XML file with tomcat deploy plugin
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong</groupId>
<artifactId>CounterWebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>CounterWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>CounterWebApp</finalName>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager</url>
<server>TomcatServer</server>
<path>/CounterWebApp</path>
</configuration>
</plugin></plugins></pluginManagement>
</build>
</project>
In this pom.xml we have used the tomcat deploy plugn org.apache.tomcat.maven to deploy the generated war file to the configured tomcat.
GOALS/PHASES:
Goals are executed in phases which help determine the order goals get
executed in. The best understanding of this is to look at the default Maven lifecycle bindings
which shows which goals get run in which phases by default. The
compile phase goals will always be executed before the test phase goals
which will always be executed before the package phase goals and so on.
Part of the confusion is exacerbated by the fact that when you execute maven you can specify a goal or a phase. If you specify a phase then maven will run all phases up to the phase you specified in order (e.g. if you specify package it will first run through the compile phase and then the test phase and finally the package phase) and for each phase it will run all goals attached to that phase.
When you create a plugin execution in your Maven build file and you only specify the goal then it will bind that goal to a given default phase. For example, the jaxb:xjc goal binds by default to the generate-resources phase. However, when you specify the execution you can also explicitly specify the phase for that goal as well.
If you specify a goal when you execute Maven then it will still run all phases up to the phase for that goal. In other words, if you specify the jar goal it will run all phases up to the package phase (and all goals in those phases), and then it will run the jar goal.
Part of the confusion is exacerbated by the fact that when you execute maven you can specify a goal or a phase. If you specify a phase then maven will run all phases up to the phase you specified in order (e.g. if you specify package it will first run through the compile phase and then the test phase and finally the package phase) and for each phase it will run all goals attached to that phase.
When you create a plugin execution in your Maven build file and you only specify the goal then it will bind that goal to a given default phase. For example, the jaxb:xjc goal binds by default to the generate-resources phase. However, when you specify the execution you can also explicitly specify the phase for that goal as well.
If you specify a goal when you execute Maven then it will still run all phases up to the phase for that goal. In other words, if you specify the jar goal it will run all phases up to the package phase (and all goals in those phases), and then it will run the jar goal.
Profiles in maven
Sometime artifact contents needs to be changed as per the different environments so for this kind of requirement we have build profile where particular profile is taken under consideration while building the project as per the profile input:
What are the different types of profile? Where is each defined?
- Per Project - Defined in the POM itself (pom.xml).
- Per User - Defined in the Maven-settings (%USER_HOME%/.m2/settings.xml).
- Global - Defined in the global Maven-settings (%M2_HOME%/conf/settings.xml).
- Profile descriptor - a descriptor located in project basedir (profiles.xml) (unsupported in Maven 3.0: see Maven 3 compatibility notes)
Listing 6. A deployment profile
<profiles>
<profile>
<id>deploywar</id>
<build>
<plugins>
<plugin>
<groupId>net.fpic</groupId>
<artifactId>tomcat-deployer-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>pos</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<host>${deploymentManagerRestHost}</host>
<port>${deploymentManagerRestPort}</port>
<username>${deploymentManagerRestUsername}</username>
<password>${deploymentManagerRestPassword}</password>
<artifactSource>
address/target/addressservice.war
</artifactSource>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
SETTINGS.XML
<!-- Defines the development deployment information -->
<profile>
<id>dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<deploymentManagerRestHost>10.50.50.52</deploymentManagerRestHost>
<deploymentManagerRestPort>58090</deploymentManagerRestPort>
<deploymentManagerRestUsername>myusername</deploymentManagerRestUsername>
<deploymentManagerRestPassword>mypassword</deploymentManagerRestPassword>
</properties>
</profile>
<!-- Defines the QA deployment information -->
<profile>
<id>qa</id>
<activation>
<property>
<name>env</name>
<value>qa</value>
</property>
</activation>
<properties>
<deploymentManagerRestHost>10.50.50.50</deploymentManagerRestHost>
<deploymentManagerRestPort>58090</deploymentManagerRestPort>
<deploymentManagerRestUsername>
myotherusername
</deploymentManagerRestUsername>
<deploymentManagerRestPassword>
myotherpassword
</deploymentManagerRestPassword>
</properties>
</profile>
command to be executed:
mvn -Pdeploywar -Denv=dev clean install
What is a Maven Repository?
In Maven terminology, a repository is a place i.e. directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily.Maven repository are of three types
- local
- central
- remote
Local Repository
Maven local repository is a folder location on your machine. It gets created when you run any maven command for the first time.Maven local repository keeps your project's all dependencies (library jars, plugin jars etc). When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository.It helps to avoid references to dependencies stored on remote machine every time a project is build.
Maven local repository by default get created by Maven in %USER_HOME% directory. To override the default location, mention another path in Maven settings.xml file available at %M2_HOME%\conf directory.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>C:/MyLocalRepository</localRepository> </settings>When you run Maven command, Maven will download dependencies to your custom path.
Central Repository
Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/
Key concepts of Central repository
- This repository is managed by Maven community.
- It is not required to be configured.
- It requires internet access to be searched.
Remote Repository
Sometime, Maven does not find a mentioned dependency in central repository as well then it stopped build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository which is developer's own custom repository containing required libraries or other project jars.For example, using below mentioned POM.xml,Maven will download dependency (not available in central repository) from Remote Repositories mentioned in the same pom.xml.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>com.companyname.common-lib</groupId> <artifactId>common-lib</artifactId> <version>1.0.0</version> </dependency> <dependencies> <repositories> <repository> <id>companyname.lib1</id> <url>http://download.companyname.org/maven2/lib1</url> </repository> <repository> <id>companyname.lib2</id> <url>http://download.companyname.org/maven2/lib2</url> </repository> </repositories> </project>
Maven Dependency Search Sequence
When we execute Maven build commands, Maven starts looking for dependency libraries in the following sequence:- Step 1 - Search dependency in local repository, if not found, move to step 2 else if found then do the further processing.
- Step 2 - Search dependency in central repository, if not found and remote repository/repositories is/are mentioned then move to step 4 else if found, then it is downloaded to local repository for future reference.
- Step 3 - If a remote repository has not been mentioned, Maven simply stops the processing and throws error (Unable to find dependency).
- Step 4 - Search dependency in remote repository or repositories, if found then it is downloaded to local repository for future reference otherwise Maven as expected stop processing and throws error (Unable to find dependency).
No comments:
Post a Comment