Feeds:
Posts
Comments

Archive for the ‘OS’ Category

A Tour to Maven

What is Maven?

Maven is a software project management and build automation tool. Maven incorporates the concept of convention over configuration by providing sensible default behaviors for projects i.e. source code is assumed to be in {basedir}src/main/java and resources are assumed to be in {basedir}src/main/resources. Tests are assumed to be in {basedir}/src/test.

The goals of Maven as:
• Make the build process easy
• Provide a uniform build system
• Provide quality project information
• Provide guidelines for best practices

Everything in Maven is controlled via the pom.xml (Project Object Model) file, which contains both information and configuration details on the project.

Lifecycles, Phases and Goals of Maven
Maven is designed around the concept of project lifecycles. While you can define your own, there are three built-in lifecycles: default, clean and site.
The default lifecycle builds and deploys your project.
The clean lifecycle cleans (deletes) compiled objects or anything else that needs to be removed or reset to get the project to a pristine pre-build state.
Finally, the site lifecycle generates the project documentation.
Within each lifecycle there are a number of phases that define various points in the development process.  The most commonly used phases in the default lifecycle are:
compile – compiles the main source code of the project
test – tests the main code using a suitable unit testing framework. These tests should not require that the code is packaged or deployed. This phase implicitly calls the  testCompile  goal to compile the test case source code.
package – packages the compiled code into its distributable format, such as a JAR. The POM controls how a project is packaged through the <packaging/> element
install – installs the package into the local repository for use as a dependency in other projects locally
deploy – used in an integration or release environment. Copies the final package to the remote repository for sharing with other developers and projects.

Maven is typically run from the command line  by executing command “mvn <phase>”, where <phase> is one of the phases listed above. Since phases are defined in order, all phases up to the one you specify will be run. For example, if you want to package your code, simply run “mvn package” and the compile and test phases will automatically be run. You can also execute specific goals for the various plugins that Maven uses. Execution of a specific goal is
done with the command “mvn <plugin>:<goal>”. For instance, the compile phase actually calls the compiler:compile goal by default.. You can also specify multiple phases/goals in one command line, and Maven will execute them in order. This is useful, for instance, if you want to do a clean build of your project. Simply run “mvn clean jetty:run” and the clean lifecycle will run, followed by the jetty:run goal (and all of the prerequisites for jetty:run, such as compile).

Repositories
Repositories are one of the key features of Maven. A repository is a location that contains plugins and packages for your project to use. There are two types of repository: local and remote.
Local repositories are local to the machine and represent a cache of artifacts downloaded from remote repositories as well as packages that you’ve installed from your own projects. The default locations of your local repo will be:
You can override the local repository location by setting the M2_REPO environment variable, or by editing the <home>/.m2/settings.xml file.
Remote repositories are repositories that are reachable via protocols like http and ftp and are generally where you will find the dependencies needed for your projects. Repositories are defined in the POM. Maven has an internal default set of repositories so usually you don’t need to define too many extra repos.
Defining a repository:
<repositories>
<repository>
<id>Repository ID</id>
<name> Repository Name</name>
<url> URL of repository</url>
</repository>
</repositories>

Plugins
Plugins add functionality to the Maven build system.
A plugin provides a set of goals that can be executed using the following syntax:

mvn [plugin-name]:[goal-name]

Configuring the Plugin:
<plugins>
<plugin>
<groupId>Plugin group ID</groupId>
<artifactId>Plugin artifact ID</artifactId>
<version>Plugin Version</version>
</plugin>
</plugins>

Dependencies
Dependency management is one of the more useful features of Maven.
The details of the specification are straightforward:
• The groupId and artifactId specify the artifact. A given group may have many artifacts under it.
• The version is specified either directly or with a range.
• The scope of the dependency is optional, and controls exactly where the dependency is used
Adding a Dependency:
<dependency>
<groupId> </groupId>
<artifactId></artifactId>
<version></version>
<scope> </scope>
</dependency>

I hope this will give you basic understanding of maven.

Read Full Post »