What is Maven? - learn maven - In 20 minutes
What is maven?
- Maven, a Yiddish word meaning accumulator of knowledge, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project. There were several projects each with their own Ant build files that were all slightly different and JARs were checked into CVS.
- We will create a new trivial Hibernate Application that reads rows in a database table (shown below) and outputs them to the console.
- First, we will need a class to represent a greeting:
Third, let's create the directories for our project:
Learn maven Tutorial - maven create directory - maven Example
Here is the project structure for your reference
Learn maven Tutorial - maven project structure - maven Example
Visit the Hibernate Website, eventually find their download page…
Learn maven Tutorial - maven hybernate website - maven Example
Download gigantic ZIP file
Learn maven Tutorial - maven gignatic wesite - maven Example
Unpack the ZIP file
Learn maven Tutorial - maven unpack zipfile - maven Example
- Copy the jar files to your lib directory
- Option 1: Copy Everything (Lazy)
- Option 2: Copy only what you need (Tedious)
- Create your build.xml file
Create the build.xml - Start with an empty file
Create some variables
Tell ant about all of your libraries
Tell ant about compiling
Tell ant how to run the app
- With maven, you execute goals in plugins over the different phases of the build lifecycle, to generate artifacts. Examples of artifacts are jars, wars, and ears.
- These artifacts have an artifactId, a groupId, and a version.
- Together, these are called the artifact’s “coordinates.” The artifacts stored in repositories.
- Artifacts are deployed to remote repositories and installed into local repositories. A POM (Project Object Model) describes a project.
Create a Project Directory - Maven has a command for starting a project:
Learn maven Tutorial - maven archetype create - maven Example
Learn maven Tutorial - maven archetype create goal - maven Example
Learn maven Tutorial - maven demo - maven Example
Set up the dependencies - Open pom.xml. We need to tell maven that we have a dependency on Hibernate:
Set up the dependencies
- We don’t need to tell Maven about any of the jars on which Hibernate depends; Maven takes care of all of the transitive dependencies for us!
Set up the compiler
- Maven assumes a default source version of 1.3. We need to tell it if we want 1.5. Here’s a preview of plugin configuration:
Set up Hibernate Configuration
- Create a resources directory beneath the main (and, optionally, test) directory, and put the Hibernate configuration file there.
- Files in the resources directory get copied to the root of the classpath when packaging occurs as part of the resource:resource goal (more on that later)
- The resources directories are automatically created for many of the archetypes, but not for the quickstart archetype that we used.
- Next, package everything up before we run it.
- To do this, invoke maven thusly:
- mvn package
- This is an alternate way to invoke maven. Instead of specifying a plugin and goal, you specify a phase (in this case, package is the phase). A phase is a sequenced set of goals. The package phase compiles the java classes and copies the resources
- Next, use the exec plugin to run our application:
- mvn exec:exec \
- Settings Files (settings.xml)
- In ~/.m2 (per-user settings) and in Maven’s install directory, under conf (per-system settings)
- Alternate location for repository
- Proxy Configuration
- Per-server authentication settings
- Download policies, for plugins and repositories; snapshots and releases.
- Project Object Model (pom.xml)
- Inherited - individual projects inherit POM attributes from parent projects, and ultimately inherit from the “Super POM”
- The Super POM is in Maven’s installation directory, embedded in the uber jar.
- The Super POM defines, among lots of other things, the default locations for the plugin and jar repositories, which is http://repo1.maven.org/maven2
- Local - in ~/.m2/repository
- Remote - e.g., http://repo1.maven.org/maven2 or another internal company repository (any directory reachable by sftp will do).
- Contains dependencies and plugins
- Can be managed by a “Repository Manager” like Nexus
- Describes the project, declaratively
- General Information - Project Coordinates (groupId, artifactId, Version)
- Build Settings - Configuration of the plugins
- Build Environment - We can configure different profiles that can be activated programatically
- POM Relationships - Dependencies on other projects