miércoles, 20 de noviembre de 2013

Maven

Installing local dependencies in Maven

Maven is a build automation tool used primarily for Java projects. It uses a Project Object Model (POM) –which is an XML file– to describe the project being build, its dependencies on other modules and external components. The benefit of using maven to build external dependencies is that you do not have to care about downloading all the libraries and versions that are required.


Sometimes there are dependencies that are not available in any Maven public repository, or maybe you want to create a dependency from an own project. This is a suggested practice given that you divide the project in smaller modules, making the architecture and the coding much cleaner and easier.

Maven provides an easy way to achieve this: the install plugin. It creates an artifact to the local repository. Below is detailed the command for installing the artifact:

mvn install:install-file -Dfile=c:\path-to-dependency.jar -DgroupId=your.group.id
-DartifactId=yourArtifactIdentifier -Dversion=version -Dpackaging=jar


mvn install:install-file specifies maven to install a dependency from a file to the local repository.

-Dfile specifies the path to the (jar) file that will be installed to the local repository.

-DgroupId identifies the group your project belongs to. A good practice is to follow the package name rules: it has to be at least as a domain name you control and then you can create subgroups, for example: maven.i2cat.net, maven.i2cat.net.projectA, maven.i2cat.net.projectA.business, etc.

-artifactId is the name of the jar without version.

-Dversion is the version of the dependency (for example 1.0.0).

The command prompt will show some information about the installing process. Once this is done, you can add your dependency in your project:

<dependency>
     <groupId>your.group.id</groupId>
     <artifactId>yourArtifactIdentifier</artifactId>
     <version>1.0.0</version>
</dependency>


To sum up, Maven lets us add external dependencies into our projects. In addition, it provides a plugin for installing new dependencies to our local repository, which is a good practice because the architecture and coding is much cleaner and easier given that the project will be divided into multiple smaller modules.