0% found this document useful (0 votes)
119 views

Creating Simple Project Using Maven Command Line

The document provides steps to create a simple Maven project from the command line, build the project, and overview some core Maven concepts. It describes how Maven uses plugins and goals to execute tasks, manages dependencies and repositories, and allows customizing projects. Key points covered include the Maven lifecycle, common goals like compile and test, dependency scopes, and using the site plugin to generate project documentation.

Uploaded by

Abhijeet Duraphe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Creating Simple Project Using Maven Command Line

The document provides steps to create a simple Maven project from the command line, build the project, and overview some core Maven concepts. It describes how Maven uses plugins and goals to execute tasks, manages dependencies and repositories, and allows customizing projects. Key points covered include the Maven lifecycle, common goals like compile and test, dependency scopes, and using the site plugin to generate project documentation.

Uploaded by

Abhijeet Duraphe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Creating simple project using maven command line.

Step 1)Type maven command

mvn archetype:generate -DgroupId=com.oma.core -DartifactId=sample1 -Dpackage=org.oma


-Dversion=1.0-ABHISHOT

Step 2)Select archetype

Step 3)Select archetype version.(select latest using higher number version.)

Step 4) Confirm selected settings

Step 5) Verify created POM and other files/folder.


Building created maven project.

Step 1) From command prompt go into directory of project pom.xml has been created.

sStep 2) From this directory fire lifecycle maven “install” command.

Overview POM of sample1 project.


There is a fifth, seldom-used coordinate named  classifier

Though we have very small POM for this project, Actual effective POM is quite large.

Effective POM is generated using this projects POM + all parents project POM + super POM defined in
maven + user defined settings + active profile.

Maven executes all commands against this effective POM.

We can see effective POM using following command.

mvn help:effective-pom

Maven Core Concepts

When we download new maven set-up, we get only core maven. This core maven knows only how to
parse the command line, manage a classpath, parse a POM file, and download Maven plugins as needed.

Plugins and Goals

Plugin

A Maven Plugin is a collection of one or more goals. Examples - Jar plugin, which contains goals for
creating JAR files, Compiler plugin, which contains goals for compiling source code and unit tests, or the
Surefire plugin, which contains goals for executing unit tests and generating reports,  Hibernate3 plugin
for integration with the popular persistence library Hibernate, the JRuby plugin which allows you to
execute ruby as part of a Maven build or to write Maven plugins in Ruby. Maven also provides for the
ability to define custom plugins. A custom plugin can be written in Java, or a plugin can be written in any
number of languages including Ant, Groovy, beanshell, and, as previously mentioned, Ruby.

Goal
A goal is a specific task that may be executed as a standalone goal or along with other goals as part of a
larger build. Examples- compile goal in the Compiler plugin, which compiles all of the source code for a
project.

Goals need to have parameters passed, so it can execute things properly. Example- we passed
parameters -DartifactId=sample1 -Dpackage=org.oma -Dversion=1.0-ABHISHOT in example sample1
project.

If we don’t pass parameters then goals have some default values for them OR goal can prompt to enter
particular parameter. Example generate goal prompt for value of archetype.

[Goals are configured via configuration properties that can be used to customize behavior. ]

Maven Lifecycle.

Maven moves through the phases in a lifecycle. Plugin goals can be attached to a lifecycle phase.

As Maven moves through the phases in a lifecycle, it will execute the goals attached to each particular
phase.
Instead of executing a Maven lifecycle goal you could achieve the same results by specifying a sequence
of plugin goals as follows:

mvn resources:resources compiler:compile resources:testResources


compiler:testCompile surefire:test jar:jar install:install

Important goals description.

resources:resources copies all of the resources from src/main/resources and any other configured


resource directories to the output directory.
compiler:compile compiles all of the source code from src/main/java or any other configured
source directories to the output directory.
resources:testResource copies all of the resources from src/test/resources and any other configured test
s resource directories to a test output directory.
compiler:testCompile compiles test cases from src/test/java and any other configured test source
directories to a test output directory.
surefire:test executes all of the tests and creates output files that capture detailed results. By
default, this goal will terminate a build if there is a test failure.
jar:jar packages the output directory into a JAR file
Maven Repositories

A repository is a collection of project artifacts (jars) stored in a directory structure that closely matches a
project’s Maven coordinates. 

Maven downloads artifacts and plugins from a remote repository to your local machine and stores these
artifacts in your local Maven repository. Your local repository will be location ~/.m2/repository

[~ indicates users home directory]

When we create and build maven project on local system, package of newly created project get installed
in local repository and gets available for other local projects.

Maven dependency management.

Support for transitive dependencies is one of Maven’s most powerful features. Let’s say your project
depends on a library that, in turn, depends on 5 or 10 other libraries (Spring or Hibernate, for example).
Instead of having to track down all of these dependencies and list them in yourpom.xml explicitly, you
can simply depend on the library you are interested in and Maven will add the dependencies of this
library to your project’s dependencies implicitly.

dependencies of dependencies are called transitive dependencies, and they are made possible by the
fact that the Maven repository stores more than just bytecode; it stores metadata about artifacts[POM].

 Dependency scopes

When a dependency has a scope of test, it will not be available to the compilegoal of the Compiler
plugin. It will be added to the classpath for only the compiler:testCompile and surefire:test goals.

When you create a JAR for a project, dependencies are not bundled with the generated artifact [jar];
they are used only for compilation. When you use Maven to create a WAR or an EAR file, you can
configure Maven to bundle dependencies with the generated artifact, and you can also configure it to
exclude certain dependencies from the WAR file using the provided scope. The provided scope tells
Maven that a dependency is needed for compilation, but should not be bundled with the output of a
build. This scope comes in handy when you are developing a web application. You’ll need to compile
your code against the Servlet specification, but you don’t want to include the Servlet API JAR in your
web application’s WEB-INF/lib directory.

Site generation and reporting.

Executing below command on project directory, creates web-pages containing information about
project.[Dependencies in project, project members, mailing list, issue-tracking etc. ]

$ mvn site
Customizing a Maven Project.

Step 1)

mvn archetype:generate -DgroupId=com.oma.core -DartifactId=simple-weather -Dpackage=org.oma


-Dversion=1.0

Step2)  configure the Maven Compiler plugin to target Java 5. To do this, add the build element to the
initial POM.

Step3)

Add licenses, organization and developers elements before dependencies  element.


Step 4).Add new dependencies.

You might also like