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坐标及依赖代码如下:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 4 5 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 7 <modelVersion>4.0.0</modelVersion> 8 <groupId>org.wltea.ik-analyzer</groupId> 9 <artifactId>ik-analyzer</artifactId> 10 <version>3.2.8</version> 11 <name>IK Analyzer 3</name> 12 <description>A dictionary and grammar-based Chinese segmenter</description> 13 <dependencies> 14 <dependency> 15 <groupId>org.apache.lucene</groupId> 16 <artifactId>lucene-core</artifactId> 17 <version>3.0.3</version> 18 <optional>true</optional> 19 </dependency> 20 <dependency> 21 <groupId>org.apache.solr</groupId> 22 <artifactId>solr-core</artifactId> 23 <version>1.4.1</version> 24 <optional>true</optional> 25 </dependency> 26 <dependency> 27 <groupId>junit</groupId> 28 <artifactId>junit</artifactId> 29 <version>3.8.2</version> 30 <scope>test</scope> 31 </dependency> 32 <dependency> 33 <groupId>org.apache.lucene</groupId> 34 <artifactId>lucene-analyzers</artifactId> 35 <version>3.0.3</version> 36 <scope>test</scope> 37 </dependency> 38 <dependency> 39 <groupId>org.apache.lucene</groupId> 40 <artifactId>lucene-smartcn</artifactId> 41 <version>3.0.3</version> 42 <scope>test</scope> 43 </dependency> 44 </dependencies> 45 </project>
第三步:打开CMD,进入到mylib文件夹,运行下面命令
1 mvn 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库中,而是通过下面配置引入,效果和上面的差不多。这种操作起来毕竟方便 但是如果是团队项目的话,得约定jar文件的指定路径才好
1 <dependency> 2 <groupId>org.wltea</groupId> 3 <artifactId>IKAnalyzer</artifactId> 4 <version>3.2.8</version><scope>system</scope> 5 <systemPath>C:\Users\yangping\Desktop\a\IKAnalyzer3.2.8.jar</systemPath> 6 </dependency>

浙公网安备 33010602011771号