[TOC]app
在构建 Maven 项目时,有时候须要导入本地的jar包,本文介绍了三种添加本地jar包的方法。eclipse
第一种解决方法是经过 Maven goal install:install-file
命令将jar包安装到本地仓库。该插件的使用方法很简单,以下所示:maven
mvn install:install-file -Dfile=<path-to-file>
工具
注意:咱们没有定义依赖包的 groupId、articleId、version、packaging 这些属性。其实,从 Maven-install-plugin
插件2.5版本开始,这些信息能够从特定的pom文件中获取。ui
这些属性信息同时也能够添加到命令行中:url
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>
插件
其中:命令行
<path-to-file>
为 jar 包的文件路径翻译
<group-id>
为 jar 包的 GroupId
<artifact-id>
为 jar 包的 ArtifactId
<version>
为 jar 包的版本号
示例:
mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0
同过添加 pom
依赖包的方式,能够将上面的 jar 包引入到 maven 项目中:
<dependency> <groupId>com.roufid.tutorials</groupId> <artifactId>example-app</artifactId> <version>1.0</version> </dependency>
这种添加本地依赖包的方式代价很是高。由于它将依赖文件直接添加到了本地 maven 仓库中,若是有一天你切换了本地仓库,那么你必须从新安装一遍 jar 包。同时,也不利于项目的移植和协同开发。
另外一种方案是在 pom
文件中使用 maven-install-plugin
插件的方式,这种方式会在 maven initialize
阶段添加相应的 jar 包。为了实现上述效果,你必须定义好须要安装 jar 包的路径。最好的方式是将所须要安装的 jar 包放置在项目根路径的某个文件夹内(和 pom.xml 同级目录中)。
咱们假设 jar 包存放位置为 <PROJECT_ROOT_FOLDER>/lib/app.jar
。如下是 maven-install-plugin
的配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.5</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>install-file</goal> </goals> <configuration> <groupId>com.roufid.tutorials</groupId> <artifactId>example-app</artifactId> <version>1.0</version> <packaging>jar</packaging> <file>${basedir}/lib/app.jar</file> </configuration> </execution> </executions> </plugin>
${basedir}
指的是 pom.xml 所在的根路径。
在添加上述代码的时候,你可能会遇到一个错误(没遇到>_<),那么添加下面的配置来容许 maven 生命周期的映射关系:
<pluginManagement> <plugins> <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. --> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <versionRange>[1.0,)</versionRange> <goals> <goal>test-compile</goal> <goal>compile</goal> </goals> </pluginExecutionFilter> <action> <execute /> </action> </pluginExecution> <pluginExecution> <pluginExecutionFilter> <groupId> org.apache.maven.plugins </groupId> <artifactId> maven-install-plugin </artifactId> <versionRange> [2.5,) </versionRange> <goals> <goal>install-file</goal> </goals> </pluginExecutionFilter> <action> <execute> <runOnIncremental>false</runOnIncremental> </execute> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement>
这种方案(不是特别好方案)经过添加 system scope 并指向 jar 包的全路径。假如 jar 包的路径为 <PROJECT_ROOT_FOLDER>/lib
, 那么它的配置以下所示:
<dependency> <groupId>com.roufid.tutorials</groupId> <artifactId>example-app</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/yourJar.jar</systemPath> </dependency>
${basedir}
指的是 pom.xml 所在的根路径。
第三种方案和第一种方案类似,不一样在于 jar 包安装的仓库位置。假如咱们有一个本地仓库,名称为 maven-repository
,位置在 ${basedir}
(pom.xml所在的根路径)。第一步要作的就是将 jar 包部署到本地仓库,以下所示:
mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true
一般 Maven 命令 deploy:deploy-file
会将 artificial
安装在远端仓库中,可是在本例子中仓库地址在本地。 添加完 jar 包后须要在对应的 pom 文件添加对应的仓库:
<repositories> <repository> <id>maven-repository</id> <url>file:///${project.basedir}/maven-repository</url> </repository> </repositories>
而后添加对应的依赖:
<dependency> <groupId>com.roufid.tutorials</groupId> <artifactId>example-app</artifactId> <version>1.0</version> </dependency>
最好的方式固然是创建本身的私有云仓库了,怎么安装?戳这里