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

Some weeks ago I had to get an 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 that wasn’t available on the repository: it was a custom generated jar to include some fonts that 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 was created to add dependencies that once where external libraries but now are 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 is to create a maven repo that 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 are 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 will be stored on a directory called “my-repo” and it will be located in the project root directory.

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

On maven 2.2:

mvn org.apache.maven.plugins:maven-install-plugin:2.3.:install-file \

-Dfile=<path-to-file> -DgroupId=<myGroup> \

-DartifactId=<myArtifactId> -Dversion=<myVersion> \

-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>

On maven 2.3 and onwards:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \

-DartifactId=<myArtifactId> -Dversion=<myVersion> \

-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>

There are several options to be 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 will be 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 is to store the jar into a project directory and install it into the m2repo directory (where all mvn dependencies are downloaded to) on build time. To get this working the dependency should be added to the repository1 as usual, and also add the plugin to the pom configuration:

<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>

This is the solution I decided to use on the project I was 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 you use).

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

Adding a custom jar as a maven dependency的更多相关文章

  1. lib目录和maven dependency目录的jar包冲突

    用eclipse时新建项目时,会在lib目录下自动生成一些jar包,然后又在pom.xml文件中添加了依赖,导致lib下的jar包和maven dependency目录下的jar包产生了冲突.刚开始r ...

  2. Maven错误Failed to read artifact descriptor for xxx:jar 和 missing artifact maven dependency

    可参考:http://stackoverflow.com/questions/6111408/maven2-missing-artifact-but-jars-are-in-place http:// ...

  3. Maven Dependency Scope用法

    原帖地址:http://uule.iteye.com/blog/2087485 官方API描述 Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个 ...

  4. Maven Dependency Scope

     官方API描述 Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态. 自从2.0.9后,新增了1种,现在有了 ...

  5. jar导入本地maven库

    最近在了解视频监控相关sdk,海康威视官方sdk要求自己手工将fas-data-sdk-1.0-SNAPSHOT.jar导入本地maven库,maven配置文件pom.xml配置如下 <?xml ...

  6. Maven mvn install 本地jar添加到maven仓库中

    最近在使用支付宝.财付通这样的第三方支付,在使用支付宝过程中需要引入官方SDK方便开发,使用以下命令来将本地的jar装载到maven仓库中. 这里有几点需要注意点,我使用Windows10时,使用po ...

  7. 将非Maven管理的jar打包到Maven本地资源库

    这里有2个案例,需要手动发出Maven命令包括一个 jar 到 Maven 的本地资源库. 要使用的 jar 不存在于 Maven 的中心储存库中. 您创建了一个自定义的 jar ,而另一个 Mave ...

  8. IDEA 热部署 + 下载jar包放到maven中

    IDEA 热部署: 1 :  POM中加入devtools的依赖,就可以实现热部署 <dependency> <groupId>org.springframework.boot ...

  9. 找不到或无法加载主类 ide 正常执行,但是打包jar后报错 maven 引入本地包

    错误: 找不到或无法加载主类 com.myali.TTSmy 问题原因: ide中编译能找到相关包,但是,打包成jar时,本地的jar引入失败 maven将系统用到的包从线上maven仓库下载到本地的 ...

随机推荐

  1. WPF数据爬取小工具-某宝推广位批量生成,及订单爬取 记:接单最痛一次的感悟

    项目由来:上月闲来无事接到接到一个单子,自动登录 X宝平台,然后重定向到指定页面批量生成推广位信息:与此同时自动定时同步订单数据到需求提供方的Java服务. 当然期间遇到一个小小的问题就是界面样式的问 ...

  2. 【AtCoder】AGC013

    AGC013 A - Sorted Arrays 直接分就行 #include <bits/stdc++.h> #define fi first #define se second #de ...

  3. spark学习之路1--用IDEA编写第一个基于java的程序打包,放standalone集群,client和cluster模式上运行

    1,首先确保hadoop和spark已经运行.(如果是基于yarn,hdfs的需要启动hadoop,否则hadoop不需要启动). 2.打开idea,创建maven工程.编辑pom.xml文件.增加d ...

  4. MySQL高级01

    MySQL支持大型数据库,支持5000万条记录的数据仓库,32位系统表文件最大可支持4GB,64位系统支持最大的表文件为8TB. 官网下载地址:http://dev.mysql.com/downloa ...

  5. POJ 1904 King's Quest (强连通分量+完美匹配)

    <题目链接> 题目大意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王 ...

  6. linux 硬盘分区与格式化挂载

    1. 硬件设备与文件名的对应关系(详见linux系统管理P297)1) 掌握在Linux系统中,每个设备都被当初一个文件来对待.2) 掌握各种设备在Linux中的文件名 2. 硬盘的结构及硬盘分区(详 ...

  7. Summary

    PDF 暑假开始准备转移博客,试了几个都不怎么满意(我还去试了下LineBlog 不知道那时候在想什么..),屯着一堆文章,,到时候一起发了 现在暂时转移至WordPress,不过还在完善中,预计.. ...

  8. emitted value instead of an instance of error the scope attribute for scoped slots webpack babel polyfill

    api20180803.vue emitted value instead of an instance of error the scope attribute for scoped slots h ...

  9. Some Conclusions.

    目录 DP 四边形不等式 数论 & 数学 数据结构 树链剖分 左偏树的性质及\(O(n)\)的构造 图论 树 二分图 竞赛图 平面图 双连通分量 字符串 后缀自动机 复杂度分析 没什么好写的. ...

  10. django rest_framework 序列化组件详解

    为什么要用序列化组件 当我们做前后端分离的项目,我们前后端交互一般都选择JSON数据格式,JSON是一个轻量级的数据交互格式. 那么我们给前端数据的时候都要转成json格式,那就需要对我们从数据库拿到 ...