Maven artifact(依赖包)版本管理规范,同级引用以及打包

本文介绍了Maven artifact(依赖包)版本管理规范,包括Project结构、版本信息声明、同级引用等内容。还阐述了不同的打包方式,如打包不包含依赖包、包含依赖包,以及其他打包方式如依赖包分离、Spring Boot打包等,并给出了相应的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:Maven artifact(依赖包)版本管理规范

Project结构
maven-parent/pom.xml

<groupId>com.archon.example</groupId>
<artifactId>maven-parent</artifactId>
<packaging>pom</packaging>
<version>0.1</version>

<modules>
    <module>maven-core</module>
    <module>maven-plugin</module>
</modules>


maven-parent/maven-core/pom.xml

<parent>
    <artifactId>maven-parent</artifactId>
    <groupId>com.archon.example</groupId>
    <version>0.1</version>
</parent>
<artifactId>maven-core</artifactId>
<packaging>jar</packaging>


maven-parent/maven-plugin/pom.xml

<parent>
    <artifactId>maven-parent</artifactId>
    <groupId>com.archon.example</groupId>
    <version>0.1</version>
</parent>
<artifactId>maven-plugin</artifactId>
<packaging>jar</packaging>


artifact版本管理规范
一般artifact(依赖包)的版本信息全部在maven-parent中声明
PS: 被依赖的子模块也需要一并引入(一般全部子模块都引入即可maven-core maven-plugin)

maven-parent/pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <artifactId>maven-core</artifactId>
            <groupId>com.archon.example</groupId>
            <version>${maven.core.version}</version>
        </dependency>

        <dependency>
            <artifactId>maven-plugin</artifactId>
            <groupId>com.archon.example</groupId>
            <version>${maven.plugin.version}</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.11</version>
        </dependency>
    </dependencies>
</dependencyManagement>


同级引用
在maven-plugin中引入maven-core以及logback(用于测试打入依赖包)

maven-parent/maven-plugin/pom.xml

<dependencies>
    <dependency>
        <artifactId>maven-core</artifactId>
        <groupId>com.archon.example</groupId>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
    </dependency>
</dependencies>


引入之后可以本地run,但mvn compile需要maven本地仓库有

maven-parent的pom
maven-corepom和jar
此时需要在maven-parent中运行mvn install(并非在maven-core中运行,这样会导致缺少maven-parent的pom)

打包
maven默认插件
在<packaging>jar</packaging>的情况下,jdk版本默认为1.5(可能是IDEA的设置,没深究)


打包,不包含artifact(依赖包)
由于maven默认就引入了以上插件,所以可以直接打包

mvn package

为确保jdk版本,最好手动引入compile

maven-parent/pom.xml(直接覆盖整个项目)

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
打包,包含artifact(依赖包)
使用maven的assembly,默认不引入,需要手动引入并注意版本

maven-parent/maven-plugin/pom.xml

<properties>
    <!-- 3.x版本没有assembly方法,只有single -->
    <maven-assembly-plugin.version>2.6</maven-assembly-plugin.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>${maven-assembly-plugin.version}</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>plugin.PluginMain</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>


运行打包

mvn assembly:assembly

其他打包
打包,artifact(依赖包)分离
少用

maven-shade-plugin

Spring Boot
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值