Maven POM文件元素

本文介绍了Maven项目中build、dependencyManagement及pluginManagement的作用与配置方法。通过实例展示了如何指定编译器版本、统一管理依赖项及插件,避免版本冲突。

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

  1. build
    build下的子元素pluinins用来声明插件。因为maven 2.1默认用jdk 1.3来编译,maven 3 貌似是用jdk 1.5,所以有时候在maven编译的过程中会出现jdk版本低的提示,就可以在此处声明jdk的版本。

     <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <vesion>2.1</vesion>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
  2. dependencyManagenment
    。dependencyManagenment声明的依赖既不会实际引入到父模块,也不会引入到子模块中。那它有什么用?答案就是对依赖项进行管理,Maven继承中子模块会继承父模块的依赖项,这就容易导致依赖项冲突,因此比较统一的管理依赖项会大大改善这种情况。如何使用呢?比如在父模块的POM文件中有一个junit依赖
<dependencyManagement>
    <dependencies>
    ....
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>1.0</version>
            <scope>test</scope>
        </dependency>
    ....
    </dependencies>
</dependencyManagement>

然后子模块恰好需要juit依赖:

<dependencies>
    <groupId>junit<groupId>
    <artifactId>junit</artifactId>
</dependencies>

这样就足够了,version和scope会从父模块的pom中继承。
3. pluginManagement
pluginManagement对插件的管理与dependencyManagement对依赖类似,pluginManagement下声明的插件不会被父模块和子模块实际调用。比如在父模块pluginManagement下声明了maven-resources-plugin插件

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugubs</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</bulid>

在子模块中如需要调用词插件则可以使用以下声明:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugubs</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这样就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值