Posts tagged: maven

Maven and missing dependencies

If you can’t immediately resolve a Maven dependency, use the following sites to help you find it – try these in order.

I normally jump to http://oss.sonatype.org/ and do a basic search. When I find the artifact I’m looking for, I select the entry and then look to the lower detail area. The detail area provides:

  • The repository that contains the artifact
  • The artifiact, the artifact pom and the maven dependency fragment

This then should lead directly to the correct POM dependency entry, (and POM repository entry if needed).

A few screen shots to help you along.

Intellij 9.0, maven and changed dependencies

I’ve sometimes experienced, that when I change. rebuild and reinstall a dependent JAR in my local maven repository, Intellij 9.0 doesn’t notice that the dependency has changed, and consequently are not included in a (rebuilt) WAR file.

The solution is to invoke the build ‘package’ target once using the Intellij Maven panel for that component. It seems then that Intellij recognizes the changed dependency.

Maven – Show all dependencies

To show all dependencies managed by maven specify:

mvn dependency:tree

 

Maven – Create a jar with all dependencies included

In Maven, to create a jar with all dependencies included.

With thanks to How can I create an executable jar with dependencies using Maven?

<build>
   <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-jar-plugin</artifactId>
       <configuration>
         <archive>
           <manifest>
             <addClasspath>true</addClasspath>
             <mainClass>fully.qualified.MainClass</mainClass>
           </manifest>
         </archive>
       </configuration>
     </plugin>
     <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <configuration>
         <descriptorRefs>
           <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
       </configuration>
     </plugin>
   </plugins>
</build>

And to run the target

mvn assembly:assembly

Maven profiles and OS-specific dependencies

Maven profiles and OS-specific dependencies

To see OS-specific values. In an existing maven-ized project run

mvn enforcer:display-info

Example output – for OS X 10.6.2 – Snow Leopard

 OS Info: Arch: x86_64 Family: mac Name: mac os x Version: 10.6.2

Create and test a simple Java project with Maven and Intellij

How to create and test a simple Java project with Maven and Intellij.

Step 1

Use maven to create the project skeleton.

Either

mvn archetype:generate

and pick Option 15 – maven-archetype-quickstart

Or

mvn org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha-7:create \
    -DgroupId=net.olioinfo \
    -DartifactId=java_simple \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeGroupId=org.apache.maven.archetypes

Step 2

  • Import into Intellij as a Maven project.
  • Make sure that the SDK is set to a JAVA SDK
  • On the Run menu, edit the Configurations to add a Junit test suite
    • Select ‘Test all in package’
    • Specify package
    • Pick the top-level project directory as the working directory
    • Ensure that ‘Make’ and ‘Build artifacts’ are checked
    • Save
  • The top two entries on the Run menu are now available
    • The top ‘Run’ menu entry will run the last configuration applied
    • The top ‘Debug’ menu entry will debug the last configuration applied
  • Maven introduces two more entries below the top two entries on the Run menu
    • ‘Run’ will run the configured maven goal (from the last configuration applied).
    • ‘Debug’ should run maven in debug mode. Doesn’t seem to work
  • All the standard maven targets work from command line. They will also function when included in an Intellij run-time configuration.

Intellij Idea 9 – Importing a Maven Groovy project

To import a Maven Groovy project into Intellij Idea 9:

  • File > New Project
  • Select ‘Import project from external model’.  Click [Next]
  • Select ‘Maven’. Click  [Next]
  • In the root directory window, navigate to the top-level directory of the Maven-Groovy project. Click [Next]
  • Accept default offered for ‘Select Maven projects to import’. Click [Next]
  • Accept default offered for project name. Click [Finish]
  • All done!

Double-click on the video player to open on YouTube.

Maven – Setting up a Groovy project

To set up a basic Groovy project using Maven (all on the same line):

mvn archetype:generate \
  -DarchetypeGroupId=org.codehaus.groovy.maven.archetypes \
  -DarchetypeArtifactId=gmaven-archetype-basic \
  -DarchetypeVersion=1.0-rc-3

Then fill in the information as for any Maven project.

An Example:

mvn archetype:generate \
  -DarchetypeGroupId=org.codehaus.groovy.maven.archetypes \
  -DarchetypeArtifactId=gmaven-archetype-basic \
  -DarchetypeVersion=1.0-rc-3

Define value for groupId: : net.olioinfo
Define value for artifactId: : configatron-groovy
Define value for version:  1.0-SNAPSHOT: : 0.1
Define value for package:  net.olioinfo: : net.olioinfo.configatron
Confirm properties configuration:
name: Example Project
groupId: net.olioinfo
artifactId: configatron-groovy
version: 0.1
package: net.olioinfo.configatron
 Y: : Y

Double-click on the video player to open on YouTube.

Maven – The basic build phases

These notes are summarized from the main Maven site, at ‘Introduction to the Build Lifecycle‘.

These notes assume a standard Maven installation.

The basic build phases (think Ant ‘targets’) for Maven builds are the following:

  • validate – validate the project is correct and all necessary information is available
  • compile – compile the source code of the project
  • test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package – take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test – process and deploy the package if necessary into an environment where integration tests can be run
  • verify – run any checks to verify the package is valid and meets quality criteria
  • install – install the package into the local repository, for use as a dependency in other projects locally
  • deploy – done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

For a complete list of the build phases, refer to the Lifecycle Reference.

To run any phase, specify:

mvn <phase>

For example to compile a project:

mvn compile

Maven 2 Primer

Introduction

This is a  Maven 2 Primer (Maven Home). It’s a result of trying to figure out what Maven is, and how to use it, and not finding any online materials that put everything together in a succinct form.

What is Maven?

Maven is a tool to simplify the management of the build, documentation and reporting phases of Java-based projects using  a common, centralized set of information.

The essence of Maven

A simple description of a project is created. If the project is organized in a standard fashion, this is frequently an automated process.

The description at its simplest consists of declarations about the project and its dependencies.

The use of a standard organization  for a project then allows Maven to infer many things about the project and provide a number of standard features. For example:

  • Compile all classes in a project
  • Create a jar file containing all the compiled classes for the project
  • Run all the tests associated with a project

What’s different about Maven from xxx?

Convention over configuration.  Most of what Maven can do for a project is based on conventions about where to find things – .e.g. uncompiled Java classes – and standard things that can be done with these things – e.g. compile and combine into a jar file.

Since it always gets asked, it’s not Maven or Ant, it’s Maven and Ant. Maven can exploit any Ant build facilities available in a project, and the Ant build facilities can in turn use Maven. Everyone plays nice.

Obtaining and installing Maven

If you’re using the latest generation of any of the popular IDEs – Eclipse, Netbeans, Intellij Idea – then Maven either comes pre-installed or is available as a plugin.

The manual installation process is simple.

  • Requirements – Java 5 (1.5.x) or Java 6 (1.6.x)
  • Download a Maven binary distribution
  • Unpack it into some sensible location – e.g. /usr/local/maven-2.2.1
  • Create an environment variable M2_HOME and point it to the installed copy of Maven – i.e. export M2_HOME=/usr/local/maven-2.2.1
  • Add $M2_HOME/bin to the PATH setting – e.g. export PATH=${PATH}:${M2_HOME}/bin

Follow many sets of good instructions, starting at the Maven Home Site FAQ.

Creating a Maven project from scratch

(All on one line)

mvn archetype:create -DgroupId=net.olioinfo.experiments -DartifactId=maven-experiment1
-DpackageName=net.olioinfo.experiments

Maven-izing an existing project

This depends on the project type, the environment you’re in and whether someone has been kind enough to do the work already.

For instance, if there’s an existing Grails project, and the Maven – Grails plugin is installed, the command

grails:create-pom

will generate a Maven POM from the existing structure.

There may be third-party tools and IDE plugins that will generate a Maven POM for a given situation.

A simple Maven configuration file

This Maven configuration file was generated by the command given above to create a new project.

mavenpomsample1

Explanation of the basics of a Maven POM file

POM = Project Object Model

Element Description
groupId Identifies the organization that owns the project, and possibly a grouping of projects within that organization. By convention starts with the reversed domain of the organization – just like Java package naming.
artifactId A unique identifier for the project. Think of this as being within the scope of the groupId.
version The particular release or version of a project. By convention, if ‘SNAPSHOT’ is included in the version, this indicates a development version.
packaging The packaging format this project is delivered in. Typically jar or war

The combination of these four elements in the order groupId, artifactId, version, packaging together should and must uniquely identify that particular version of that project. This combination is called the ‘Maven coordinates’ of a project. That simply means the unique identifier for the project from Maven’s point of view. Among other things, this ensures that no conflicts arise when components are hosted in a Maven repository.

The other elements of the Maven POM file are fairly obvious. The most significant element is the <dependencies> element. It list all the components that this project depends on. For a further discussion the elements allowed within a POM see any standard reference.

Running Maven

Here’s the delight:

mvn install

will compile, run all the tests, package and install the project in the default local repository (~/.m2/repository ).

The Maven home site has several pages about the steps or ‘lifecycle stages’ of a Maven project.

WordPress Themes