Thursday, March 1, 2018

Things know about maven



Purpose 

1) Adding set of Jars in each project

2) Creating the right project structure: 

3) Building and Deploying the project: 


Components

1) A Maven repository is a location where all the project jars, library jars, plugins or any other particular project-related artifacts are stored and can be easily used by Maven.There types are local, central and remote

2) Usually an artifact is a JAR file which gets arrayed to a Maven repository. One or more artifacts a maven build produces such as compiled JAR and a sources JAR.

Each artifact includes a group ID, an artifact ID and a version string.

3)There are two types of Maven Plugins:

  • Build plugins – These plugins are executed during the build and are configured in the <build/> element of pom.xml
  • Reporting plugins – These plugins are executed during the stage generation and are configured in the <reporting/> element of the pom.xml.

4) Dependencies refer to the Java libraries that are needed for the project. Repositories refer to the directories of packaged JAR files.

5)The maven’s order of inheritance is

  1. Parent Pom
  2. Project Pom
  3. Settings
  4. CLI parameters








mvn −version  


mvn package 

  1. validate
  2. compile
  3. test
  4. package
  5. integration-test
  6. verify
  7. install
  8. deploy



18) For POM what are the minimum required elements?

The minimum required elements for POM are project root, modelVersion, groupID, artifactID and version.

19) Explain how you can produce execution debug output or error messages?

To produce execution debug output you could call Maven with X parameter or e parameter.

20) Explain how to run test classes in Maven?

To run test classes in Maven, you need surefire plugin, check and configure your settings in setting.xml and pom.xml for a property named "test."


Apache Maven helps to manage

  • Builds
  • Documentation
  • Reporing
  • SCMs
  • Releases
  • Distribution

Essential elements of MAVEN
  1. project: Root Element
  2. modelVersion: subelement 
  3. groupId: id for the project group
  4. artifact Id: name of the project 
  5. version: version of project 
  6. packaging : war/jar/ear
  7. dependencies /dependency : third party or witin 
  8. url : url of project
  9. scope :[compile | provided | runtime |test | system]
  10. name : name of project





Setting up MAVEN



Maven is a project management and comprehension tool 

that provides developers a complete build lifecycle framework. 

Default project structure is provided by  Maven

ItemDefault
source code${basedir}/src/main/java
Resources${basedir}/src/main/resources
Tests${basedir}/src/test
Complied byte code${basedir}/target
distributable JAR${basedir}/target/classes

In order to build the project, Maven provides developers with options to mention 

  • life-cycle goals and
  • project dependencies 


Setting

 1.Add environment variable


OSOutput
Windows
Set the environment variables using system properties.
M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.3.1 M2=%M2_HOME%\bin MAVEN_OPTS=-Xms256m -Xmx512m
Linux
Open command terminal and set environment variables.
export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.1 export M2=$M2_HOME/bin
export MAVEN_OPTS=-Xms256m -Xmx512m
Mac
Open command terminal and set environment variables.
export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.1
export M2=$M2_HOME/bin
export MAVEN_OPTS=-Xms256m -Xmx512m

Add Path

WindowsAppend the string ;%M2% to the end of the system variable, Path.
Linuxexport PATH=$M2:$PATH
Macexport PATH=$M2:$PATH


Check Version

Now open console and execute the following mvn command.

OSTaskCommand
WindowsOpen Command Consolec:\> mvn --version
LinuxOpen Command Terminal$ mvn --version
MacOpen Terminalmachine:~ joseph$ mvn --version

POM also contains the goals and plugins. While executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, and then executes the goal. Some of the configuration that can be specified in the POM are following −
  • project dependencies
  • plugins
  • goals
  • build profiles
  • project version
  • developers
  • mailing list

Attributes that uniquely identify 

All POM files require the project element and three mandatory fields: groupId, artifactId, version.



Goals : There are 7 goals

PhaseHandlesDescription
prepare-resourcesresource copyingResource copying can be customized in this phase.
validateValidating the informationValidates if the project is correct and if all necessary information is available.
compilecompilationSource code compilation is done in this phase.
TestTestingTests the compiled source code suitable for testing framework.
packagepackagingThis phase creates the JAR/WAR package as mentioned in the packaging in POM.xml.
installinstallationThis phase installs the package in local/remote maven repository.
DeployDeployingCopies the final package to the remote repository.

 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">
   4.0.0
com.companyname.project-group project 1.0 C:\MVN\project\src\main\java src/main/scripts C:\MVN\project\src\test\java C:\MVN\project\target\classes C:\MVN\project\target\test-classes resource-0 C:\MVN\project\src\main\resources resource-1 C:\MVN\project\src\test\resources C:\MVN\project\target project-1.0
 Plugins Types
Build plugins
Repositary plugin
Maven is actually a plugin execution framework where every task is actually done by plugins. Maven Plugins are generally used to −
  • create jar file
  • create war file
  • compile code files
  • unit testing of code
  • create project documentation
  • create project reports
A plugin generally provides a set of goals, which can be executed using the following syntax −
 print data on console maven-antrun-plugin
  1.3 maven-assembly-plugin< /artifactId> 2.2-beta-2 maven-clean-plugin< /artifactId> 2.2 
Compiles Java source files. maven-compiler-plugin 
2.0.2 maven-dependency-plugin 2.0 maven-deploy-plugin 2.4 
Builds a EAR file from the current project. maven-ear-plugin 
2.3.1 maven-ejb-plugin 2.1 maven-install-plugin 
2.2 Builds a JAR file from the current project. maven-jar-plugin 2.2 Generates Javadoc for the project. maven-javadoc-plugin 
2.5 maven-plugin-plugin 
2.4.3 Builds a RAR file from the current project. maven-rar-plugin 
2.2 maven-release-plugin 2.0-beta-8 maven-resources-plugin 2.3 maven-site-plugin 2.0-beta-7 maven-source-plugin 
2.0.4 Runs the JUnit unit tests. Creates test reports. maven-surefire-plugin 2.4.3 Builds a WAR file from the current project. maven-war-plugin 2.1-alpha-2 maven-help-plugin 2.1.1
  Repositary plugin 
Repositories
Location where built WAR/EAR/JAR or any other artifact will be stored after code build is successful.
false
central Maven Repository Switchboard http://repo1.maven.org/maven2 never false central Maven Plugin Repository http://repo1.maven.org/maven2 C:\MVN\project\target/site


What is Archetype?

Archetype is a Maven plugin whose task is to create a project structure as per its template. We are going to use quickstart archetype plugin to create a simple java application here.

C:\MVN>mvn archetype:generate 


   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">
   4.0.0
DEPLOYED COPY bus_core_api bus_core_api 1.0-SNAPSHOT CURRENT DEVELOPMENT COPY jar


Deployment Process

In project development, normally a deployment process consists of the following steps −
  • Check-in the code from all project in progress into the SVN (version control system) or source code repository and tag it.
  • Download the complete source code from SVN.
  • Build the application.
  • Store the build output either WAR or EAR file to a common network location.
  • Get the file from network and deploy the file to the production site.
  • Updated the documentation with date and updated version number of the application.

CREATE WEB APPLICATION 

C:\MVN>mvn archetype:generate

-DgroupId = com.companyname.automobile -DartifactId = trucks -DarchetypeArtifactId = maven-archetype-webapp -DinteractiveMode = false

Aspects t

Maven handles the following activities of a developer

  • Build
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution
  • Mailing list















No comments:

Post a Comment