3種方式使用本地jar包

在實際開發過程遇到以下問題:

當A依賴B,B依賴C,如果A對B、B對C的依賴pom scope設置爲compile,則A對C的依賴與B對C的依賴相同。

這樣會產生一個問題:當B依賴的C(C可能是B端的本地包,不在公共倉庫上)在A端沒有,則使用maven build的時候會報錯。

 

依賴傳遞:

由scope定義

  • compile - this is the default scope, used if none is specified. Compile dependencies are available in all classpaths. Furthermore, those dependencies are propagated to dependent projects.

  • provided - this is much like compile, but indicates you expect the JDK or a container to provide it at runtime. It is only available on the compilation and test classpath, and is not transitive.

  • runtime - this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

  • test - this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. It is not transitive.

  • system - this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

一.手動安裝

使用maven提供的插件:mvn install:install-file

mvn install:install-file
 -Dfile=path of jar file to be installed
 -DgroupId=groupId of the jar file to be installed
 -DartifactId=artifactId
 -Dversion=version of jar
 -Dpackaging=type(jar/)

e.g

set ov_version=4.5R2
set libs_path=..\ovlibs
call mvn install:install-file -Dfile=%libs_path%\cache-%ov_version%.jar -DgroupId=com.alu.ov.ngnms -DartifactId=cache -Dversion=%ov_version% -Dpackaging=jar

也可以在pom.xml中直接引用maven-install-plugin插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <id>cache</id>
            <phase>initialize/install/compile/...</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <file>${basedir}/ovlibs/cache-${ov.version}.jar</file>
                <groupId>com.alu.ov.ngnms</groupId>
                <artifactId>cache</artifactId>
                <version>${ov.version}</version>
                <packaging>jar</packaging>
            </configuration>
        </execution>
    </executions>
</plugin>

 

二.scope system

可以使用

<scope>system</scope>

直接引用本地Jar包,不需要安裝到本地maven倉庫:

<dependency>
	<groupId>com.alu.ov.ngnms</groupId>
	<artifactId>cache</artifactId>
	<version>${ov.version}</version>
    <scope>system</scope>
	<systemPath>${basedir}/*.jar</systemPath>
</dependency>

這種方式可擴展性相對低

三.搭建公共maven倉庫

與第一種方式類似,只是第一種方式是安裝到本地倉庫,這種方式是安裝到公共倉庫。

將需要依賴的B包de到公共倉庫:

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

在pom.xml中引用公共倉庫:

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>

再依賴B:

<dependency>
	<groupId>com.alu.ov.nmgss</groupId>
	<artifactId>B</artifactId>
	<version>1.0</version>
</dependency>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章