Maven

Articles
Videos

Dependency hell

http://maven.apache.org/index.html
https://maven.apache.org/guides/mini/guide-configuring-maven.html

Maven features:

1. Dependency management
2. Archtypes
3. Goals

Common Commands or Goals:

mvn --version
mvn clean // delete target directory
mvn compile
mvn test-compile // compile test files
mvn test // run unit tests
mvn package // creates the jar file
mvn install // install or publish your created artifact (jar) file to local maven repository
  // This does not deploy your application to your local container (Tomcat, WebLogic, etc)
mvn deploy // install or publish your artifact / project build to the remote Maven repository.  
  // This does not deploy to your local container.
mvn archetype:generate
mvn pluginName:goalName

mvn site // generate documentation for your project (Java Doc)
mvn site:run // starts an embedded Jetty for your project documentation (Java Doc).
  // We can browse documentation on port 8080

Build Life Cycle (Phases):

Validate // Validate dependencies
Compile
Test
Package
Integration Test
Verify
Install
Deploy

Scope:

compile: only needed for the compile phase
test: only needed for the test phase
provided: it is provided by the container
runtime: this is needed at runtime

What is Maven?

Maven is a software management and comprehension tool based on the concept of Project Object Model (POM) which can manage project build, reporting, and documentation from a central piece of information. It is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity. See http://maven.apache.org

It is an automated build tool. It focus on simplicity:

  1. Creation - generates intelligent "starters / project skeletons"
  2. Management - assumes intelligence defaults. Manage dependencies on external projects, JAR files, libraries, etc.

It covers build-oriented phases in ALM:

  1. Build management
  2. Testing
  3. Release versioning
  4. Deployment

What are the roles of a build system?

  1. Compile source code
  2. Copy resource
  3. Compile and run tests
  4. Package project
  5. Deploy project
  6. Clean

What do we want from maven?

  1. a standard way to build the projects
  2. a clear definition of what the project consist of
  3. an easy way to publish project information and way to share JARs across several projects

What are the tenets of Maven?

  • Project oriented
  • Convention over Configuration
  • Dependency management
  • Extensible through plug-ins
  • Reuse through centralized repositories

Why should we use Maven instead of Ant?

Not sure yet.

  • Build tool
  • Dependency management
  • Project structure

Is Ant simply a build tool without dependency management and without archetype?

What is the definition of "goal"?

A goal contains multiple tasks. An example of a goal is "clean", such as:

mvn clean

In the above command, "clean" is the goal.

What are the available goals ?

  1. clean
  2. compile
  3. test-compile
  4. test
  5. install

What is the build lifecycle / phases in Maven?

  1. validate
  2. compile
  3. test
  4. package
  5. integration-test
  6. verify
  7. install
  8. deploy

How can we get started with Maven?

  1. Go to http://maven.apache.org/download.html and choose the binary zip format
  2. Extract Maven to appropriate location
  3. Set the Maven Home Variable (M2_HOME):
    • export M2_HOME=/usr/local/maven
    • set M2_HOME="C:\maven"
  4. Append maven to your path:
    • export PATH=$M2_HOME/bin:$PATH
    • set PATH=%PATH%;%M2_HOME%/bin
  5. See "How can we create our first project with mvn", "How can we compile your project using mvn", "How can we clean our project" questions below.

How can we test to see if Maven is installed appropriately?

From the command line, run:

mvn -v

How can we use Maven with Eclipse?

Depending on the version of Eclipse that you installed, the m2eclipse plugin (Maven) may already have been installed.

How can we create a Maven project using Eclipse?

  1. Click on File -> New Project
  2. In the "type filter text" box, type Maven
  3. Click on "Maven Project"
  4. Click Next

What should we use for the groupId field?

The general practice is to use com.companyName

What should we use for the artifactId field?

This is the name of the project / product that you are working on.

What should we use for the version field?

1.0-SNAPSHOT indicates that this is a development version.

What should we use for the package field?

This is usually the same as the groupId (com.companyName) + ‘.’ + artifactId

How can we add a dependency using Eclipse?

Double click on the pom.xml file. This launch the graphical editor so that we can edit this pom.xml file graphically. To add dependency, we can click on the "Dependencies" tab (look at the middle of the screen and toward the bottom of the screen). We can add dependency graphically, or we can click on the "pom.xml" tab. This will render the raw pom.xml file so that we can manually edit it. The content of the pom.xml should look like:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.khai</groupId>
  <artifactId>testproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
      </dependency>
  </dependencies>
</project>

when we finish. In the above code, we added junit as a dependency. Make sure that we save the file by click on the Save icon. It seems that as soon as we save the file, Maven automatically download the jar file for the dependency and add it to the project / classpath and resolves errors.

How can we create our first project with mvn?

Projects are created using the archtype plugin. The archetype has prototypes for many Java related projects. Use the archetype plugin creates project structure and necessary starter files base on project type. Before creating a project, you should define:

  1. Which group is responsible for the project? [groupId]. The groupID can be anything.
  2. What is the project going to be called? [artifactId]. This define the library name.
  3. What version are you working on? [version]
  4. What type of project are you working on? [archetypeArtifactId]
mvn archetype:create -DgroupId=… -DartifactId=… -Dversion=… -DpackageName=… -DarchetypeArtifactId=…

Or if we do not know all this information, but just want to get something going, we can use:

mvn archetype:create -DgroupId=… -DartifactId=… 
mvn archetype:create -DgroupId=com.di.maven -DartifactId=hello-world

and we can go back and edit and provide other details later. If we use the simple syntax above, Maven will assume that you want to create a JAR file, and it will assume that the JAR file will contains a standalone application (a class with a main method).

How can we compile your project using mvn?

mvn compile

How can we clean our project?

mvn clean

How can we get help from command line?

mvn -h

What is the purpose of the "repository" folder inside the installation directory?

When we first install maven, the "repository" folder is not there. When we use maven for the first time, it will create that directory, and over time, as we use maven, this directory will contain JAR files. This is our local repo.

What are the key Maven commands:

There are two different ways to invoke commands. There is a syntax that allow us to invoke a Maven lifecycle, which is a closest to a command, and then there is a way for us to invoke a plugin's goal which is roughly equivalent to an Ant task. Commands are a way of invoking the Maven lifecycle. Other commands are defined through plug-ins.

  1. mvn compile
  2. mvm package: create the jar file or war file in the target folder, and run junit test cases.

How can we invoke a plug-ins goal?

Plug-ins are invoked using syntax:

mvn plugin_name:goal

How can we run jetty?

mvn jetty:run

What happens when we run "mvn archetype:generate"?

This command will ask us to select appropriate archtype, and then it will generate appropriate pom.xml file

What are the key phases of a lifecycle?

Maven uses concept of a lifecycle for builds. The key phases in a project's life cycle:

  1. Creation: create a project
  2. Compile: compile the project (and its test cases)
  3. Test: perform automated testing
  4. Package: bundle project into JAR, WAR, etc
  5. install: install or publish your artifact / project build to the local Maven repository. This does not deploy your application to your local container (Tomcat, WebLogic, etc)
  6. deploy: install or publish your artifact / project build to the remote Maven repository. This does not deploy to your local container.
  7. clean: clean

Phases are built on each other (they depends on each other just like Ant). Phase name can be used like maven command

If you run "mvn" just by itself, what does it do?

It attempts to build the project.

What does the "mvn site" command do?

It generate site documentation (Java Doc), project documentation. It can automatically upload your project document to remote web server.

What does "mvn site:run" do?

It starts and embedded Jetty instance and deploy your site (project documentation) to Jetty. You can now browse your project documentation via http://localhost:8080/

Can you modify standard life cycles or create your own?

Yes.

What is a phase?

Maven consider the process of software development as a life-cycle, and during this life-cycle, there are several phases: clean, compile, package, deploy. A phase is like a goal, which consist of multiple sub-tasks.

What is the purpose of the package phase?

Packages build into artifact (.jar, .war, etc)

How is the pom.xml file generated?

The pom.xml is generated by the "mvn archetype:create …" from above.

When we start a new project, what should we use as the version number?

1.0.1. Need further explanation on why we should use 1.0.1 as the version number when we start a new project.

What does ALM abbreviate for?

Application Lifecycle Management

Do we have to completely abandon Ant?

We do not have to completely abandon Ant. To ease the transition from Ant to Maven, there is a Maven plugin name Ant that allow us to use existing Ant build.xml file.

What is the purpose of deploy?

Deploy does not deploy to application server or web server. What does deploy really do?

Are plugins required for all phases?

No. Not all phases have bound plugins. Typically project packaging defines which plugins are bound to which phase.

What is the relationship between project types and plugin goals?

Different project types requires different plugin goals.

How can we configure maven to use a different compiler?

If we configure the compiler for the compile phase, we have to manually configure it for other phases as well.

What is the purpose of the process-resources phase?

  • Processes resources and copies them to output directory (target)
  • Typically handled by the resources:resources goal
  • Support variable substitution during processing through filters - turned off by default

How can we execute a Maven goal using Eclipse?

  1. Right click on the project
  2. Select "Run As -> Maven build …" (notice the 3 dots)
  3. Provide the appropriate goal in the Goal field
  4. Click Run

How can we create a JAR file for our project using Eclipse?

The general steps are:

  1. Compile App.java -> App.class
  2. Compile AppTest.java -> AppTest.class
  3. Run tests
  4. Create jar

To execute the above general steps:

  1. Execute the "clean" goal
  2. Execute the "compile" goal
  3. Execute the "test-compile" goal
  4. Execute the "test" goal
  5. Execute the "install" goal

Because the "install" goal should automatically run all the goals that it depends on, we should only have to execute the "clean" goal and the "install" goal.

From the command line:

mvn clean
mvn compile
mvn test-compile
mvn test
mvn install

The JAR file is generated inside the target folder.

What happens when we run the clean goal?

All the compiled classes and artifacts are removed from the target folder.

How can we see the transitive dependencies of a dependency?

Double click on the pom.xml file to open it. Click on Hie or click on the pom.xml tab. Hold down the CTRL key and hover on top of a dependency, and click on it.

How can we exclude a particular dependency?

See the last part of https://www.youtube.com/watch?v=zPg5aPjh-sA starting approximately at 1hr mark.

How can we limit the scope of a decency?

See the last part of https://www.youtube.com/watch?v=zPg5aPjh-sA starting approximately at 1hr mark.

How can we import a Maven project into Eclipse?

Let assume that we have a Maven project exist somewhere with the pom.xml file. Perhaps this was an export or checkout from your source code repository or you download it from the Internet. To import this Maven project into Eclipse:

  1. Launch Eclipse
  2. Create a new workspace if desired
  3. Go to File -> Import -> Maven -> Existing Maven Projects
  4. Click Next
  5. Specify the folder that contains the Maven project in the "Select Root Directory" field
  6. If your Maven project contains multiple modules, then you would the list of modules and you should be able to check appropriate check boxes for the module that you want to import.
  7. Click Finish

How can we install the Maven Integration for Eclipse WTP?

  1. Launch Eclipse
  2. Click on Help -> Install New Software
  3. Give it the name m2e-wtp
  4. Specify the URL http://download.eclipse.org/m2e-wtp/releases/neon/

How can we specify the -e -X switch using m2eclipse?

Just add these flags to the Goal:

jetty:run -e -X
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License