YMLiang

Maven-pom.xml-中元素解析

modules

模块
用处:项目规模比较大,模块较为复杂,目的是为了聚合,一次性构建全部模块
1
2
3
4
5
6
代码:
<modules>
    <!-- 模块都写在此处 -->
<module>admin-register</module>
<module>admin-login</module>
</modules>

dependencyManagement

依赖管理
用处:管理maven依赖
1
2
3
4
5
6
7
8
代码:
<dependencies>
<dependency>
<groupId>com.yixue.sms</groupId>
<artifactId>sms-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

parent

继承
用处:类似与java中的继承,如果每个子模块都用了相同的依赖包,则配置父模块,子模块继承父模块则代表继承了父模块的依赖包
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
代码:
父:

<modules>
    <!-- 模块都写在此处 -->
<module>admin-register</module>
<module>admin-login</module>
</modules>

<dependencies> <!-- 配置共有依赖 -->
<!-- spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
子:
<parent>
<groupId>com.admin.user</groupId>
<artifactId>admin-login</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
<!-- 与不配置一样,默认就是寻找上级目录下得pom.xml -->
</parent>
<!-- 配置自己独有依赖 -->
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
假设将来需要添加一个新的子模块admin-util,该模块只是提供一些简单的帮助工具,不需要依赖spring那么我们可以用dependencyManagement既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性

在dependencyManagement元素下得依赖声明不会引入实际的依赖,不过它能够约束dependencies下的依赖使用

父POM使用dependencyManagement能够统一项目范围中依赖的版本

当依赖版本在父POM中声明后,子模块在使用依赖的时候就无须声明版本,也就不会发生多个子模块使用版本不一致的情况,帮助降低依赖冲突的几率

properties

自定义一个或者多个Maven属性,然后再POM的其他地方使用${属性名}的方式引用该属性
作用:消除重复,统一管理
1
2
3
4
5
6
用法
<properties>
<!-- 定义 spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<junit.version>4.7</junit.version>
</properties>
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
这里我记录了一些pom的属性:

用户可以使用该类属性引用POM文件中对应元素的值。如:

${project.artifactId}就对应了<project> <artifactId>元素的值,常用的POM属性包括:

    ${project.build.sourceDirectory}:项目的主源码目录,默认为src/main/java/

    ${project.build.testSourceDirectory}:项目的测试源码目录,默认为src/test/java/

${project.build.sourceEncoding}表示主源码的编码格式;

    ${project.build.directory} : 项目构建输出目录,默认为target/

    ${project.outputDirectory} : 项目主代码编译输出目录,默认为target/classes/

    ${project.testOutputDirectory}:项目测试主代码输出目录,默认为target/testclasses/

    ${project.groupId}:项目的groupId

    ${project.artifactId}:项目的artifactId

    ${project.version}:项目的version,与${version} 等价

    ${project.build.finalName}:项目打包输出文件的名称,默认为${project.artifactId}-${project.version}

Exclusions

排除依赖
用法:来排除一些不需要同时下载的依赖jar
举例:B项目中需要导入A项目的Maven依赖,通过依赖传递,会将A中的Jar包传递进来,如果B中不需要A中的某个jar包就可以使用exclusions标签
1
2
3
4
5
6
7
8
9
10
11
用法:
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>

环境变量属性

所有环境变量属性都可以使用以env. 开头的Maven属性引用,如${env.JAVA_HOME}指代了JAVA_HOME环境变量的的值

beta:软件的验收测试

如果配置了<env>beta</env>

${env} = beta

内置属性

(Maven预定义,用户可以直接使用)

${basedir}表示项目根目录,即包含pom.xml文件的目录;

${version}表示项目版本;

${project.basedir}同${basedir};

${project.baseUri}表示项目文件地址;

${maven.build.timestamp}表示项目构件开始时间;

${maven.build.timestamp.format}表示属性${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm,可自定义其格式,其类型可参考java.text.SimpleDateFormat。用法如下:
1
2
3
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>

build

在Maven的pom.xml文件中,存在如下两种<build>:

(1)全局配置(project build)

        针对整个项目的所有情况都有效

(2)配置(profile build)

        针对不同的profile配置

共用的基本build元素:

defaultGoal,执行构建时默认的goal或phase,如jar:jar或者package等
directory,构建的结果所在的路径,默认为${basedir}/target目录
finalName,构建的最终结果的名字,该名字可能在其他plugin中被改变


<build>
    <filters>
        <filter>../config/filters-${env}.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/resources/</directory>
            <excludes>
                <exclude>test/*</exclude>
                <exclude>beta/*</exclude>
                <exclude>online/*</exclude>
            </excludes>
            <!-- 是否使用过滤器 -->
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources/${profiles.active}</directory>
        </resource>
    </resources>
</build>


resources,build过程中涉及的资源文件
targetPath,资源文件的目标路径
filtering,构建过程中是否对资源进行过滤,默认false
directory,资源文件的路径,默认位于${basedir}/src/main/resources/目录下
includes,一组文件名的匹配模式,被匹配的资源文件将被构建过程处理
excludes,一组文件名的匹配模式,被匹配的资源文件将被构建过程忽略。同时被includes和excludes匹配的资源文件,将被忽略。

filters,给出对资源文件进行过滤的属性文件的路径,默认位于${basedir}/src/main/filters/目录下。属性文件中定义若干键值对。在构建过程中,对于资源文件中出现的变量(键),将使用属性文件中该键对应的值替换。
testResources,test过程中涉及的资源文件,默认位于${basedir}/src/test/resources/目录下。这里的资源文件不会被构建到目标构件中

 评论


博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议

本站使用 Material X 作为主题 , 总访问量为 次 。
Copyright 2018-2019 YMLiang'BLOG   |   京ICP备 - 19039949  |  载入天数...载入时分秒...