Creating Simple Project Using Maven Command Line
Creating Simple Project Using Maven Command Line
Step 1) From command prompt go into directory of project pom.xml has been created.
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.
mvn help:effective-pom
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.
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:
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
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.
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.
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)
Step2) configure the Maven Compiler plugin to target Java 5. To do this, add the build element to the
initial POM.
Step3)