Maven如何手动添加依赖的jar文件到本地Maven仓库

来源:互联网 发布:湖北11选5遗漏数据 编辑:程序博客网 时间:2024/05/09 16:57

 Apache Maven,是一个软件(特别是Java软件)项目管理及自动构建工具,由Apache软件基金会所提供。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。曾是Jakarta项目的子项目,现为独立Apache项目。
  大家肯定遇到过想在pom文件中加入自己开发的依赖包,这些包肯定是不是在Maven仓库(http://repo1.maven.org/maven2/)的。那我们怎么将那些不存在Maven仓库中的包加入到本地的Maven库中呢?很简单。这里以IKAnalyzer.jar包为例进行讲解。
  第一步:将IKAnalyzer.jar包存放在一个文件夹中,比如mylib文件夹
  第二步:建一个IKAnalyzer.jar包相关的pom.xml文件,需要在pom.xml中定义其maven坐标及其相应的依赖代码即可,同样将pom文件存放在上述jar文件同一文件夹下,IKAnalyzer.jar坐标及依赖代码如下:

01<project xmlns="http://maven.apache.org/POM/4.0.0"
02    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
04    http://maven.apache.org/xsd/maven-4.0.0.xsd">
05    <modelVersion>4.0.0</modelVersion>
06    <groupId>org.wltea.ik-analyzer</groupId>
07    <artifactId>ik-analyzer</artifactId>
08    <version>3.2.8</version>
09    <name>IK Analyzer 3</name>
10    <description>A dictionary and grammar-based Chinese segmenter</description>
11    <dependencies>
12        <dependency>
13            <groupId>org.apache.lucene</groupId>
14            <artifactId>lucene-core</artifactId>
15            <version>3.0.3</version>
16            <optional>true</optional>
17        </dependency>
18        <dependency>
19            <groupId>org.apache.solr</groupId>
20            <artifactId>solr-core</artifactId>
21            <version>1.4.1</version>
22            <optional>true</optional>
23        </dependency>
24        <dependency>
25            <groupId>junit</groupId>
26            <artifactId>junit</artifactId>
27            <version>3.8.2</version>
28            <scope>test</scope>
29        </dependency>
30        <dependency>
31            <groupId>org.apache.lucene</groupId>
32            <artifactId>lucene-analyzers</artifactId>
33            <version>3.0.3</version>
34            <scope>test</scope>
35        </dependency>
36        <dependency>
37            <groupId>org.apache.lucene</groupId>
38            <artifactId>lucene-smartcn</artifactId>
39            <version>3.0.3</version>
40            <scope>test</scope>
41        </dependency>
42    </dependencies>
43</project>

  第三步:打开CMD,进入到mylib文件夹,运行下面命令

1mvn install:install-file               \
2    -Dfile=IKAnalyzer3.2.8.jar         \
3    -DgroupId=org.wltea.ik-analyzer    \
4    -DartifactId=ik-analyzer           \
5    -Dversion=3.2.8                   \
6    -Dpackaging=jar

这样你就可以将IKAnalyzer3.2.8.jar安装到您Maven本地的库文件夹相应目录中。你可以根据你需要安装包的实际情况修改上面的几个参数的设定值即可。之后你可以在pom.xml文件中通过以下依赖在项目中引入上述的包,如下:

1<dependency>
2     <groupId>org.wltea.ik-analyzer</groupId>
3     <artifactId>ik-analyzer</artifactId>
4     <version>3.2.8</version>
5 </dependency>

当然你也可以不将IKAnalyzer3.2.8.jar发布到您本地的Maven库中,而是通过下面配置引入,效果和上面的差不多:

查看源代码
打印帮助
1<dependency>
2    <groupId>org.wltea</groupId>
3    <artifactId>IKAnalyzer</artifactId>
4    <version>3.2.8</version>
5    <systemPath>C:\Users\yangping\Desktop\a\IKAnalyzer3.2.8.jar</systemPath>
6</dependency>转自:http://www.iteblog.com/archives/646
0 0
原创粉丝点击