将自己开发的依赖包加入到Maven本地仓库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将自己开发的依赖包加入到Maven本地仓库相关的知识,希望对你有一定的参考价值。

Maven 在 pom 文件中加入自己开发的依赖包,这些包肯定是不在Maven仓库(http://repo.maven.apache.org/maven2/)的。那我们怎么将那些不在Maven仓库中的包加入到本地的Maven库中呢?很简单。这里以IKAnalyzer.jar包为例进行讲解。 

第一步 :将IKAnalyzer.jar包存放在一个文件夹中,比如test文件夹 
第二步 :建一个IKAnalyzer.jar包相关的 pom .xml文件,需要在pom.xml中定义其maven坐标及其相应的依赖代码即可,同样将pom文件存放在上述jar文件同一文件夹下,IKAnalyzer.jar坐标及依赖代码如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.wltea.ik-analyzer</groupId>
  <artifactId>ik-analyzer</artifactId>
  <version>3.2.8</version>
  <name>IK Analyzer 3</name>
  <description>A dictionary and grammar-based Chinese segmenter.</description>
  <dependencies>
    <dependency>
      <groupId>org.apache.lucene</groupId>
      <artifactId>lucene-core</artifactId>
      <version>3.0.3</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.apache.solr</groupId>
      <artifactId>solr-core</artifactId>
      <version>1.4.1</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.lucene</groupId>
      <artifactId>lucene-analyzers</artifactId>
      <version>3.0.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.lucene</groupId>
      <artifactId>lucene-smartcn</artifactId>
      <version>3.0.3</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

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

 

E:\test>mvn install:install-file -Dfile=文件名.jar -DgroupId=组织名 -DartifactId=项目名 
-Dversion=版本号 -Dpackaging=jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom
---
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-uti
ls/3.0.5/plexus-utils-3.0.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-util
s/3.0.5/plexus-utils-3.0.5.pom (3 KB at 0.4 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-
api/2.0.6/maven-plugin-api-2.0.6.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project
/2.0.6/maven-project-2.0.6.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-setting
s/2.0.6/maven-settings-2.0.6.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile
/2.0.6/maven-profile-2.0.6.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-
registry/2.0.6/maven-plugin-registry-2.0.6.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-r
egistry/2.0.6/maven-plugin-registry-2.0.6.jar (29 KB at 15.4 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/classworlds/classworlds/1.1-al
pha-2/classworlds-1.1-alpha-2.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/
2.0.6/maven-profile-2.0.6.jar (35 KB at 10.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2
.0.6/maven-model-2.0.6.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-settings
/2.0.6/maven-settings-2.0.6.jar (48 KB at 11.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifac
t-manager/2.0.6/maven-artifact-manager-2.0.6.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-a
pi/2.0.6/maven-plugin-api-2.0.6.jar (13 KB at 2.8 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-reposit
ory-metadata/2.0.6/maven-repository-metadata-2.0.6.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-reposito
ry-metadata/2.0.6/maven-repository-metadata-2.0.6.jar (24 KB at 3.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifac
t/2.0.6/maven-artifact-2.0.6.jar
Downloaded: https://repo.maven.apache.org/maven2/classworlds/classworlds/1.1-alp
ha-2/classworlds-1.1-alpha-2.jar (37 KB at 5.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-uti
ls/3.0.5/plexus-utils-3.0.5.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact
-manager/2.0.6/maven-artifact-manager-2.0.6.jar (56 KB at 6.4 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.
0.6/maven-model-2.0.6.jar (85 KB at 6.9 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact
/2.0.6/maven-artifact-2.0.6.jar (86 KB at 5.7 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/
2.0.6/maven-project-2.0.6.jar (114 KB at 5.8 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-util
s/3.0.5/plexus-utils-3.0.5.jar (226 KB at 10.7 KB/sec)
[INFO] Installing E:\test\文件名.jar to C:\Users\Administrator\.m2\repository\组织名项目名\版本号\文件名.jar
[INFO] Installing C:\Users\ADMINI~1\AppData\Local\Temp\mvninstall199361744543933
8455.pom to C:\Users\Administrator\.m2\repository\组织名\项目名
\版本号\文件名.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.436 s
[INFO] Finished at: 2016-05-21T22:16:10+08:00
[INFO] Final Memory: 9M/183M
[INFO] ------------------------------------------------------------------------

 

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

 

<dependency>
	<groupId>org.wltea</groupId>
	<artifactId>IKAnalyzer</artifactId>
	<version>3.2.8</version>
</dependency>

 

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

<dependency>
	<groupId>org.wltea</groupId>
	<artifactId>IKAnalyzer</artifactId>
	<version>3.2.8</version>
	<scope>system</scope>
    <systemPath>E:\test\IKAnalyzer3.2.8.jar</systemPath>
</dependency>

 

以上是关于将自己开发的依赖包加入到Maven本地仓库的主要内容,如果未能解决你的问题,请参考以下文章

Maven依赖的是本地工程还是仓库jar包

Maven添加外部jar包到本地仓库

用命令将本地jar包导入到本地maven仓库

如何添加jcenter仓库 gradle依赖

maven项目 模块化开发 引用的类修改了代码,jar包的引用问题

用命令将本地jar包导入到本地maven仓库