Maven has a default repository where many open source libraries are stored. When we declare one of these libraries as one of our project's dependencies, the library along with it's dependencies are automatically downloaded. Sometimes, either our project or our project's dependencies depend on libraries that are not freely distributable, therefore they cannot be placed in Maven's default repository. When working with Maven, a local repository is created in the developer's workstation (in a directory called .m2 under the developer's home directory). The solution to using libraries that are not freely distributable is to install them in our local repository.
To illustrate this situation, let's add Hibernate as a dependency to our project, since it depends on the Java Transaction API, which is not freely distributable.
Adding dependencies to a project involves adding a simple change
to the project's pom.xml. The changes to the file involve adding a <dependency>
element to the <dependencies>
element. For example,
to add Hibernate as a dependency, the project's pom.xml would look like
this:
<modelVersion>4.0.0</modelVersion>
<groupId>maven-test</groupId>
<artifactId>maven-test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>maven-test</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
When attempting to compile the project again (by executing the mvn
compile
goal), Maven will download Hibernate along with all of it's
freely distributable dependencies, but fail when it encounters the non
freely distributable dependency:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
Missing:
----------
1) javax.transaction:jta:jar:1.0.1B
Try downloading the file manually from:
http://java.sun.com/products/jta
Then, install it using the command:
mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta \
-Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file
The solution, of course, is to download the Java Transaction API and install it as indicated in the error message.
After installing the Java Transaction API and attempting to recompile, we will run into a similar issue for another library, the Java Authorization Contract for Containers (JACC). It can be solved the same way.
After installing the above two libraries, we are now able to compile the application.
[heffel@vader maven-test]$ mvn compile [INFO] Scanning for projects... [INFO] ---------------------------------------------------------------------------- [INFO] Building maven-test [INFO] task-segment: [compile] [INFO] ---------------------------------------------------------------------------- [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Nothing to compile - all classes are up to date [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2 seconds [INFO] Finished at: Thu Dec 14 21:03:54 EST 2006 [INFO] Final Memory: 5M/14M [INFO] ------------------------------------------------------------------------
Maven is a great tool that standardizes the build process across Java projects. In this article, we saw how to quickly create, compile, and execute a standalone Java application project using Maven.
In addition to quickly creating a project for a standalone
application, Maven can quickly create a Web Application project
(deployed as a WAR file), as well as EJB's and other types of projects.
This can be accomplished by passing the -DarchetypeArtifactId=foo
argument to the archetype:create
goal. See resources for
valid values for this parameter.
archetype:create
-DarchetypeArtifactId
parameter.
Better Builds
With Maven
(free e-book, registration required)