自定义jar包引入依赖传递

Overview

源于笔者想让自己打的jar包能引入jar包本身的其他依赖,而有此文。

生成jar包

目标结构

|-- [class files]
|-- META-INF
   -- maven
     -- <groupId>
       -- <artifactId>
         -- pom.propertis
         -- pom.xml //项目pom.xml<pre>

pom.properties内容

version=<version>
groupId=<groupId>
artifactId=<artifactId> 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>{project.groupId}</groupId>
    <artifactId>{project.artifactId}</artifactId>
    <version>{project.version}</version>

    <dependencies>
        <!-- 若干dependency -->
    </dependencies>
</project>

使用maven安装jar包

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

P.S. 以上命令,仅限于maven默认 maven-install-plugin 版本为3.0.0-M1以上(含,官方有一处提及2.5.2也支持,笔者测试不支持,当时没怀疑被坑惨了),笔者撰写此文时,最新maven版本为 3.6.3,默认的 maven-install-plugin 版本为 2.4 ,因此,需要用如下命令:

mvn org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file -Dfile=<path-to-file>

maven-install-plugin版本过低,将无法识别META-INF内的pom.xml,需要自己指定:

mvn install:install-file -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dfile=<path-to-file> -DpomFile=<path-to-pom>

P.S. 当指定-DpomFile时,jar包内无需META-INF(可删)

那么,该如何配置maven自带插件的默认版本呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值