Adding a custom jar as a maven dependency

Source: Internet
Author: User

Using maven in a Java project is great. It manages builds (as customized as you may need), executions, dependencies ... In fact, dependencies are, in my opinion, the key feature of Maven. Whenever a dependency is added to a project, MAVEN would search for it at repositories, download it and store it, tagging V Ersions.

Some weeks ago I had to get a old project without Maven and make it work with it. Everything went right at FIRST:I had only to search for the correct dependency on the Repository and config my project to Get it. But suddenly I found a jar, wasn ' t available on the repository:it is a custom generated jar to include some fonts th At were being used by Jasper on a PDF generation.

I had to get it working without adding the jar to the repository. I got some working solutions:

Adding a system dependency
<Dependencies>  <Dependency>    <groupId>GroupId</groupId>    <Artifactid>Artifactid</Artifactid>    <version>1.0</version>    <Scope>System</Scope>    <Systempath>${basedir}/lib/xx.jar</Systempath>  </Dependency></Dependencies>

I wouldn ' t do it. System scope is created to add dependencies this once where external libraries but now is packed into the JDK. Also, the Assembly plugin ignores this kind of dependencies and it doesn ' t pack them with the others.

Creating a maven repo inside the project

The idea was to create a MAVEN repo this runs on our own computer instead or a remote server. Every file needed would be on the version control system so every developer can build the project once it is downloaded.

Three steps is needed to accomplish this:

1. Add The local repository to the project Pom:

<repositories>  <Repository>    <ID>My-local-repo</ID>    <URL>File://${basedir}/my-repo</URL>  </Repository></repositories>

In this case the repository is stored on a directory called "My-repo" and it'll be located in the project root dire Ctory.

2. Install jar in the repository through Install-plugin.

On MAVEN 2.2:

MVN org.apache.maven.plugins:maven-Install-plugin:2.3. 1:install-file-dfile=<path-to-file>-dgroupid=<mygroup >-dartifactid=<myartifactid>-dversion=<myversion>-dpackaging=<mypackaging >-dlocalrepositorypath=<path>

On Maven 2.3 and onwards:

Install:install-file -dfile=<path-to-file>-dgroupid=<mygroup>  -dartifactid=<myartifactid>-dversion=<myversion>-dpackaging=<mypackaging>- Dlocalrepositorypath=<path>

There is several options to being filled on the command:

    • path-to-file: The path to the file of the artifact to install.
    • myGroup, myArtifactId, myVersion: The group, artifact name and version of the artifact to install.
    • myPackaging: The packaging of the artifact (Ie:jar).
    • path: The path to the local repo.

3. Add the dependency to the project as usual.

After running the command of the second step, several files would be is created on the local repository (like Pom and checksum files). This is the reason I don ' t like this solution, I find it ugly with those files added to the VCS.

Installing the jar by using Install-plugin

The idea was to store the jar into a project directory and install it into the M2repo directory (where all MVN dependencies is downloaded to) on build time. To get this working the dependency should is added to the repository1 as usual, and also add the plugin to the Pom Configu Ration:

<Project>    <groupId>Org.koshik.javabrains</groupId>    <Artifactid>Jarname</Artifactid>(A fldernamed Jarname was created)<version>1.0-snapshot</version>    <Packaging>Jar</Packaging>    <name>Jarname</name>    <URL>http://maven.apache.org</URL>    <Properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </Properties>    <Build>        <Plugins>        <plugin>            <groupId>Org.apache.maven.plugins</groupId>            <Artifactid>Maven-install-plugin</Artifactid>            <version>2.4</version>            <executions>            <Execution>                <Phase>Initialize</Phase>                <Goals>                <goal>Install-file</goal>                </Goals>                <Configuration>                <groupId>Mygroupid</groupId>                <Artifactid>Myartifactid</Artifactid>                <version>Myversion</version>                <Packaging>Jar</Packaging>                <file>${basedir}/lib/xxx.jar</file>                </Configuration>            </Execution>            </executions>        </plugin>        </Plugins>    </Build>    <Dependencies>        <Dependency>            <groupId>Junit</groupId>            <Artifactid>Junit</Artifactid>            <version>3.8.1</version>            <Scope>Test</Scope>        </Dependency>    </Dependencies></Project>

The the solution I decided to the project I am working on. It's clean and it ' ll work from the first moment a new developer downloads the project from the VCS (I mean the source code REPOSITORY:SVN, GIT or whatever your use).

from:http://blog.valdaris.com/post/custom-jar/

Adding a custom jar as a maven dependency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.