Maven 項目添加本地jar包的三種方式

Maven 項目添加本地jar包的三種方式

翻譯自3 ways to add local jar to maven projectapache

[TOC]app

簡介

在構建 Maven 項目時,有時候須要導入本地的jar包,本文介紹了三種添加本地jar包的方法。eclipse

  1. 手動添加jar包到本地倉庫
  2. 添加爲 system scope 依賴
  3. 建立一個不一樣的本地倉庫
  4. 使用 Nexus 倉庫管理器

1. 手動添加jar包到本地倉庫

第一種解決方法是經過 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>

2. 直接將依賴包添加爲 system 範圍

這種方案(不是特別好方案)經過添加 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 所在的根路徑。

3. 建立一個不一樣的本地倉庫

第三種方案和第一種方案類似,不一樣在於 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>

4. 使用Nexus倉庫管理工具

最好的方式固然是創建本身的私有云倉庫了,怎麼安裝?戳這裏

相關文章
相關標籤/搜索