Tuesday, 4 September 2018

Maven Tutorial




Maven Learning Steps


Maven Commands for project creation and other steps

We can download Maven and set the class path in system/user environment variables. I downloaded maven and placed that in C:\ drive

To check the version:

 mvn --version



  
To create a project, we can execute below command
mvn archetype:generate -DgroupId=com.gaurav
-DartifactId=demoProjectByMaven
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  •       groupId : This refers to packaging of the project and helps to identify them.
  •       artifactId : Name of project/jar, This will not include version.
  •       version : We can specify version number as per our desire.
  •       archetypeArtifactId : This is the Template type.We have several templates from which we need to select one.
  •       interactiveMode : If    it is set to false then maven will create project in batch mode without asking any confirmation, if it is set to true then maven will ask confirmation on each step of project during generation.


Here archetype:generate is a goal. Generated pom file will look alike below
<modelVersion>4.0.0</modelVersion>
<groupId>com.gaurav</groupId>

<artifactId>demoProjectByMaven</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demoProjectByMaven</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Standard Maven project structure

src/main/java             Application/Library sources src/main/resources                            Application/Library resources src/test/java        Test         sources src/test/resources Test resources

For Web application

mvn archetype:generate -DgroupId=com.gaurav
-DartifactId=demoWebAppProjectByMaven
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false



 Generated pom file for web application

<modelVersion>4.0.0</modelVersion>
<groupId>com.gaurav</groupId>
<artifactId>demoWebAppProjectByMaven</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>demoWebAppProjectByMaven Maven Webapp</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>demoWebAppProjectByMaven</finalName>
</build>
</project>

Generated web.xml file

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app> Generated index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>

</html>


Default Directory Structure for web application

-src
--main
---resources
---webapp
----WEB-INF
-----web.xml
----index.jsp
-pom.xml

Standard Maven Web application project structure

src/main/java             Application/Library sources src/main/resources                                   Application/Library resources src/main/webapp Web application sources src/test/java   Test sources src/test/resources        Test resources

WecanDeployandRuntheMavenWebProjectbyplacingthegeneratedwarfileinto server

We can use mvn clean package to create the war file.

http://<host-name>:<portnumber>/projectname

For example: http://localhost:8666/demoWebAppProjectByMaven OUTPUT





To clean the Maven project, we can use below command, this will remove the target folder, if there is already available. This cleans up artifacts created by prior builds. This is a Maven phase of it's build life cycle.

mvn clean command



Package :- This command will help to package the compiled code in distributable format.Like jar, war e.t.c

mvn package




Here package is simply a phase. A phase is a step of build life cycle.

If we want to test the generated jar using above command by executing that then we can check like below:-

java -cp target/demoProjectByMaven-1.0-SNAPSHOT.jar com.gaurav.App


 Maven Phases


These life cycle phases should be executed sequentially to complete the default life cycle.

  •       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: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.


There are two other Maven life cycles of note beyond the default list above. They are

  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project



mvn validate



mvn compile



  mvn clean package



The above command will execute below phases: clean, validate, compile, test, package.

mvn clean dependency:copy-dependencies package


The above command will execute below phases:

clean, validate, compile, test, copy dependencies, package.

copy-dependencies: - Goal that copies the project dependencies from the repository to a defined location. Suppose we have runtime dependency and we want to copy them into target/lib.

<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>

<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${targetdirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Converting generated Maven project to Eclipse compatible project, we can specify below command

mvn eclipse:eclipse





We can notice that Maven will create .project, .classpath file in the root of the project after the execution of above command to make this project compatible to eclipse.


For a development environment below is the usual command, Following call can be used to build and install artifacts into the local repository.

mvn install


This command executes each default life cycle phase in order (validate, compile, test, package, etc.), before executing install.

In a build environment, use the following call to cleanly build and deploy artifacts into the shared repository.

mvn clean deploy


The same command can be used in a multi-module scenario (i.e. a project with one or more subprojects). Maven traverses into every subproject and executes clean, then executes deploy (including all of the prior build phase steps).


Note:- Some Phases we should not call from the Command Line

In the case of invoking integration-test, the environment may be left in a hanging state.

Packaging

To set the packaging for our project, we can use POM element <packaging>. Some of the valid packaging values are jar, war, ear and pom. If no packaging value has been specified, it will default to jar.

Each packaging contains a list of goals to bind to a particular phase. For example, the jar packaging will bind the following goals to build phases of the default lifecycle.

process-resources
resources:resources
compile
compiler:compile
process-test-resources
resources:testResources
test-compile
compiler:testCompile
test
surefire:test
package
jar:jar
install
install:install
deploy
deploy:deploy


No comments:

Post a Comment