【Maven】添加本地库到Maven项目中只有这三种方法

64 篇文章 0 订阅
17 篇文章 0 订阅

引语

你是否遇到跟我一样需要添加自己的开发库到maven项目中,这些库要么没有被Maven中央库所管理,或本身不想被公开的内部库. 那么你只有三种方法,如果这三种方法不灵,就没辙了,你也不用再花时间去别的的去找了:

1- 手工安装你的本地库到Maven的本地存储仓库。

The first solution is to add manually the JAR into your local Maven repository by using the Maven goal install:install-file. The use of the plugin is very simple as below:

1

mvn install:install-file -Dfile=<path-to-file>

Note that we didn’t specify groupId, artifactId, version and packaging of the JAR to install. Indeed, since the version 2.5 of Maven-install-plugin, these information can be taken from an optionally specified pomFile.

These information can also be given in command line:

1

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

Where:

  • <path-to-file>: Path to the JAR to install
  • <group-id>: Group id of the JAR to install
  • <artifact-id>: Artifact id of the JAR to install
  • <version>:  Version of the JAR

For example:

1

mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0

You can then add the dependency to your Maven project by adding those lines to your pom.xml file:

1

2

3

4

5

<dependency>

<groupId>com.roufid.tutorials</groupId>

<artifactId>example-app</artifactId>

<version>1.0</version>

</dependency>

This solution can be very expensive. Why ? You have to consider that the day you change your local Maven repository you have to re-install the JAR. Or again, if there are many persons working on the project, each must install the JAR in his local repository. The portability of the project must be taken into account.

Another solution is to use the maven-install-plugin in your pom.xml which will install the jar during the Maven “initialize” phase. To do this, you must specify the location of the jar you want to install. The best way is to put the JAR in a folder created at the root of the project (in the same directory as the pom.xml file).

Let’s consider that the jar is located under <PROJECT_ROOT_FOLDER>/lib/app.jar. Below the configuration of maven-install-plugin:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<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} represents the directory containing pom.xml.

You may encounter an error while adding the previous lines, add the following plugin to your project to allow the lifecycle mapping:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

<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- 直接添加依赖,作为系统库来引用

Another solution – dirty solution – is by adding the dependency as system scope and refer to it by its full path. Consider that the JAR is located in <PROJECT_ROOT_FOLDER>/lib. Then add the dependency in your pom.xml file as following:

1

2

3

4

5

6

7

<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} represents the directory containing pom.xml.

 

3- 创建一种不同的本地库

The third solution is quite similar to the first one, the difference lies in the fact that the JARs will be installed in a different local Maven repository.

Let’s consider the new local Maven repository is named “maven-repository” and is located in ${basedir} (the directory containing pom.xml). First you have to do is deploying the local JARs in the new local maven repository as below:

1

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

Normally, the Maven deploy:deploy-file installs the artifact in a remote repository but in our case the repository is located in the local machine.

After installing the JARs your need to add the repository in your pom.xml file:

1

2

3

4

5

6

<repositories>

    <repository>

        <id>maven-repository</id>

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

    </repository>

</repositories>

Then you can add the dependency into your pom.xml

1

2

3

4

5

<dependency>

<groupId>com.roufid.tutorials</groupId>

<artifactId>example-app</artifactId>

<version>1.0</version>

</dependency>

4- 使用Nexus仓库管理工具

The best solution is to use a Nexus Repository Manager which will contain all your JARs and you will use it as repository to download the dependency.

This book from the official Nexus site will show you how to install and use Nexus repository manager.

 

Refrences

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值