Monday, May 29, 2017

Eclipse project from maven project

Create eclipse project from maven


Run this command
C:\Users\anil.thakur\maven work\ MyProject > mvn eclipse:eclipse

MAVEN Web Project

Create a java Web Application using maven


Go  to the folder where you want to create the java application
Example
C:\Users\anil.thakur\maven work

Run this command


C:\Users\anil.thakur\maven work >  mvn archetype:generate -DgroupId=com.atwisdom -DartifactId=MyWebApp  -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

import this project to eclipse

MAVEN Project from java project using eclipse


MAVEN project using eclipse


MAVEN Project from console

Create a java Application using maven

Go  to the folder where you want to create the java application
Example
C:\Users\anil.thakur\maven work

Run this command
C:\Users\anil.thakur\maven work >  mvn archetype:generate -DgroupId=com.atwisdon -DartifactId=MyProject  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


MAVEN commands

The goal you indicate in the command line is linked to the lifecycle of Maven. 
For example, the build lifecycle (you also have the clean and site lifecycles which are different) is composed of the following phases:


  • validate: validate the project is correct and all necessary information is available.
  • compile: compile the source code of the project.
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run.
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally.
  • deploy: deploy in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

MAVEN Folder Structure


MAVEN Project Documents


MAVEN External Dependency


MAVEN Manage Dependencies


MAVEN Plugins


MAVEN Repository

Maven repository are of 3 types

  • Local repository
  • Central repository
  • Remote repository

Local repository

Local repository is the location in your machine where all your dependencies (jars, plugins etc..)will be downloaded when ever we first run the maven command.
It is created automatically.

maven-> config-> setting.xml

<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>D:\local_repo</localRepository>
</settings>

You have to mention the path in localRepository.

If this is not done then it will create the .m folder at default location.

Central repository

Maven central repository is provided by the maven community, it consist of the all dependencies. when ever dependencies not exit in the local it search in central repository. it does not required any configuration only internet access is required.

Remote repository

Remote Repository  is developer's own custom repository containing required libraries or other project jars.

MAVEN Build Profile

Build profile

A Build profile is a set of configuration values which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.

Profiles modify the POM at build time, and are used to give parameters different target environments (for example, the path of the database server in the development, testing, and production environments).

What are the different types of profile?
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 (${maven.home}/conf/settings.xml).


.
File Name                  Description
env.properties default configuration used if no profile is mentioned.
env.test.properties test configuration when test profile is used.
env.prod.properties production configuration when prod profile is used.

MAVEN Build Lifecycle

Maven defines 3 lifecycles

  • default Lifecycle
  • clean Lifecycle
  • site Lifecycle

Clean Lifecycle


  • pre-clean     execute processes needed prior to the actual project cleaning
  • clean     remove all files generated by the previous build
  • post-clean     execute processes needed to finalize the project cleaning

Default Lifecycle


  • validate validate the project is correct and all necessary information is available.
  • initialize initialize build state, e.g. set properties or create directories.
  • generate-sources generate any source code for inclusion in compilation.
  • process-sources process the source code, for example to filter any values.
  • generate-resources generate resources for inclusion in the package.
  • process-resources copy and process the resources into the destination directory, ready for packaging.
  • compile compile the source code of the project.
  • process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
  • generate-test-sources generate any test source code for inclusion in compilation.
  • process-test-sources process the test source code, for example to filter any values.
  • generate-test-resources create resources for testing.
  • process-test-resources copy and process the resources into the test destination directory.
  • test-compile compile the test source code into the test destination directory
  • process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
  • test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  • prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
  • package take the compiled code and package it in its distributable format, such as a JAR.
  • pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
  • integration-test process and deploy the package if necessary into an environment where integration tests can be run.
  • post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
  • verify run any checks to verify the package is valid and meets quality criteria.
  • install install the package into the local repository, for use as a dependency in other projects locally.
  • deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Site Lifecycle

pre-site execute processes needed prior to the actual project site generation
site generate the project's site documentation
post-site execute processes needed to finalize the site generation, and to prepare for site deployment
site-deploy deploy the generated site documentation to the specified web server

MAVEN installation on WINDOW


  1. Download zip file of  maven from https://maven.apache.org/download.cgi
  2. Unzip maven folder
  3. Copy maven folder to C:\Program Files\
  4. Go to path C:\Program Files\apache-maven-3.3.9, copy that
  5. Open my computer-> system properties ->Advanced system properties ->environment variable
  6. Set java home if not set
  7. Set MAVEN_HOME=C:\Program Files\apache-maven-3.3.9


  8. Add maven home to system path:  %MAVEN_HOME%\bin;
  9. Apply and check with console if maven is install.: mvn -version

MAVAN VS ANT

Comparison with ANT


  • One level above ANT
  • Higher level of reusability between builds
  • Faster turn around time to set up a powerful build
  • Project website generation
  • Less maintenance
  • Greater momentum
  • Repository management
  •  Automatic downloads