[Maven] Placing Third Party Libraries in your own Repository

Although the public repositories for Maven contains a vast collection of third party libraries. Despite this, there are some libraries that you would be able to find in them. Fortunately, you can place the library into your local repository or set up your own internal repository. If you are setting up a large internal repository, you might consider using a repository manager, such as Archiva. However, if you do not want to install one, you can set your own internal repository or place it directly in your private repository.

Installing to your personal local repository

Use the install plugin to install the files directly into the personal local repository (usually, ~/.m2/repository or c:\Documents and Settings\<username>\.m2\repository). Use the install-file goal to place specific files from third party libraries. The structure of the command:

mvn install:install-file \
    -Dfile=<the file to install> \
    -DartifactId=<id for the file> \
    -DgroupId=<group id> \
    -Dpackaging=<package type (e.g. jar, war, ear)> \
    -Dversion=<version of file>

The above command assumes that your personal local repository is in the default path (as mentioned before, either ~/.m2/repository or c:\Documents and Settings\<username>\.m2\repository). If it is in a different location, you need to provide it in the localRepositoryPath parameter. This can also be used to place the artifact in an internal repository.

If the artifact has any dependencies that may not be directly referenced by the your project, you will also have to add them into the repository. You should also edit the generated pom to include the dependency.

Deploying the artifact to the repository

The deploy can also be used to add artifacts to a repository. Unlike the install plugin, it does not assume a default location for the respository. Instead, you must specify the repository location, using the url parameter.

mvn deploy:deploy-file \
    -Dfile=<file to deploy> \
    -DartifactId=<id for the artifact> \
    -DgroupId=<group id> \
    -Dpackaging=<the type of packaging (e.g. jar, war, ear)> \
    -Dversion=<version of artifact> \
    -Durl=<url of repository>

It is important to note that url parameter is expecting a full url path, including the protocol. If the repository is a location within the file system, you should specify it with file://<file path>. The localRepositoryPath parameter for install already expects a file system path and does not require the protocol to be specified. If the artifact has any dependencies on any other artifacts, you will need to edit the pom generated for the artifact.

Manually adding the artifact to the repository

When you add artifacts using either the install or deploy plugins, these steps are performed. To add the artifact manually, you must first set up the appropriate directory structure in the repository. The directory structure is related to the group id of the artifact and is set up in a way that is similar to how directory structure to represent Java packages. For example, if the group id was org.example.project, the directory structure would look like this:

repository root
|
+- org
 |
 +- example
  |
  +- project

For group ids that do not use the dot notation, you would only need a single directory to represent the group id. At end of the directory structure (e.g. the project directory in the above example), you will need to create a directory for each of the artifacts. The name of the directory will correspond to the artifact id. Inside the artifact directory, you also need to create a directory per version of the artifact. For example, if you were adding version 1.0.1 of an artifact with the id sample in the above example, your directory structure would now be:

repository root
|
+- org
 |
 +- example
  |
  +- project
   |
   +- sample
    |
    +- 1.0.1

The artifact is placed in the directory representing the version. However, its file name needs to be in the following format:

<artifact id>-<version>.<file extension>

Following on from the example, if the artifact was a jar file, the file name would be sample-1.0.1.jar. Finally, set up a pom file for the artifact. The file name for the pom should follow the same format (e.g. sample-1.0.1.pom).

Once the pom is set up, you should be able to start pulling the artifact from the repository. As mentioned before, the install and deploy plugins will do all this for you. However, they also generate the hashes for the poms and the artifacts, a few other files. If you wish you could also manually create these files, but is not really necessary to start using the artifact from the repository.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: