配置Maven使用本地lib文件夹

The official "Maven Way" of dependency management is to use Maven Central and local repository specified in the settings.xml file (which usually points to $HOME/.m2/repository. While it works great for projects that rely on a large number of open source libraries and satisfies 95% of dependency management needs in those projects, there is that 5% of the time when a jar is not sourced from a Maven project. One example is a jar using JNI so is only available for certain OS platforms. How do we integrate this jar into Maven dependency management? If you search the web for hints, you may be led to believe that you either have to bend to the Maven Way or to use the systemPath. But the Maven Way will force you to maintain a local repository for a trival library. The systemPath on the other hand does not work naturally with packaging. Developers will most likely ask "Can I check in this library to my (your_favorite_VCS) with my project and still have Maven use it in a way just like any other dependency?" The answer is YES. Just follow the steps below:

1. Create a directory under your project, say "lib".

2. Use Maven to install your jar to the lib directory.

mvn install:install-file -DgroupId=com.baidu -DartifactId=bccs-api-lib -Dversion=2.0.1 -Dpackaging=jar -Dfile=bccs-api-lib-2.0.1.jar -DlocalRepositoryPath=D:\svn\go_backend\lib


3. Setup your POM like this.

    <repositories>
       <repository>
           <!-- DO NOT set id to "local" because it is reserved by Maven -->
           <id>lib</id>
           <url>file://${project.basedir}/lib</url>
       </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.baidu</groupId>
            <artifactId>bccs-api-lib</artifactId>
            <version>2.0.1</version>
        </dependency>

  ...


Now you can check in/out bccs-api-lib-2.0.1.jar just like any other file in your project and Maven will manage the dependency on mylib.jar just like any other dependency artifact. Perfect harmony. :-)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值