maven pom简单入门

本文详细介绍了Maven的pom.xml文件,包括项目信息配置如groupId、artifactId和version,以及pom结构如继承、依赖和聚合关系。还讲解了构建配置、资源处理、插件管理和仓库设置。此外,列举了Spring框架和其他常用依赖,以及maven-surefire-plugin的用法。

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

本文也是综合了网上多人博文汇总而成,不算原创。文章很长,建议打开目录看下。

pom文件简介

pom 是指 Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。

下面详细介绍maven的pom.xml的各个属性:

pom结构

pom 基本配置

<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.codehaus.mojo</groupId>  
    <artifactId>my-project</artifactId>  
    <version>1.0</version>  
    <packaging>war</packaging> 
    <name>·····</name>
    <url>·····</url>
    ·····
    ·····

</project>

project 属性

  • web-app 是web.xml的根节点标签名称
  • version是版本的意思
  • xmlns 是web.xml文件用到的命名空间,类似包名,因为xml的标签可自定义
  • xmlns:xsi 是指web.xml遵守xml规范
  • xsi:schemaLocation 是指具体用到的schema资源地址,两部分组成,前面部分就是命名空间的名字,后面是xsd(xmlschema)的地址,如:
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
                      http://maven.apache.org/maven-v4_0_0.xsd"

项目信息配置

  • groupId :项目或者组织的唯一标志,并且配置时生成路径也是由此生成,如org.myproject.mojo生成的相对路径为:/org/myproject/mojo
  • artifactId : 项目的通用名称
  • version : 项目版本号
  • packaging : 项目打包机制,如 pom, jar, maven-plugin, ejb, war, rar, par 。在idea中,如果一个项目下有多个模块,一般父级项目packaging是pom
  • name : 用户描述项目的名称,无关紧要的东西,可选
  • url : 应该是只是写明开发团队的网站,无关紧要,可选

groupId、artifactId 和 version 是必须定义的,定义项目的基本信息,可唯一确定一个maven项目。

pom常量配置

properties:是为pom定义一些常量,如JDK版本号、字符编码等,在pom中的其它地方可以直接引用。

<project>
    <properties>
        <file.encoding>UTF-8</file_encoding>
        <junit.version>4.12</junit.version>
        <java.target.version>1.5</java_target_version>
    </properties>
    //引用方式:${junit.version}
    [···]
    <dependency> 
    [···]
      <version>${junit.version}</version>   
    </dependency>
    [···]
</project>

关系配置

pom关系,主要为:继承、依赖、聚合三种

继承关系

继承关系:继承其他pom.xml配置的机制。如:子pom可以继承父pom的依赖。

添加属性即可:

<project>
    [...]  
    <parent>  
        <groupId>com.devzuz.mvnbook.proficio</groupId>  
        <artifactId>proficio</artifactId>  
        <version>1.0-SNAPSHOT</version>  
        <relativePath>../my-parent</relativePath> 
    </parent>  
    [...]  
</project>

relativePath是可选的,maven会首先搜索这个地址,在搜索本地远程repositories之前.

依赖关系

依赖关系列表(dependency list)是POM的重要部分。pom详细介绍百度文库

image
- groupId : 依赖项的groupId
- artifactId : 依赖项的artifactId
- version : 依赖项的版本
- scope : 依赖项的适用范围:
image
- optional : 传递性依赖:
image
- exclusions : 排除依赖:
image

image

聚合关系

Maven 3支持Maven项目的多模块(multi-modules)结构。这样的Maven项目也被称为聚合项目,通常由一个父模块和若干个子模块构成。其中,==父模块必须以pom打包类型==,同时以给出所有的子模块。父模块的POM示例如下:

<project>
    <modules>  
        <module>my-module-1</module>  
        <module>my-module-2</module>  
        <module>my-module-3</module>  
    </modules>  
</project>

其中的每个module,都是另外一个Maven项目

build 构建配置

build设置构建项目需要的信息,其中中的主要标签:Resources和Plugins

基本配置

<project>
···
    <build>  
        <defaultGoal>install</defaultGoal>  
        <directory>${basedir}/target</directory>  
        <finalName>${artifactId}-${version}</finalName>  
        <!--该元素设置了项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->
        <sourceDirectory />
        <!--该元素设置了项目脚本源码目录,该目录和源码目录不同:绝大多数情况下,该目录下的内容 会被拷贝到输出目录(因为脚本是被解释的,而不是被编译的)。 -->
        <scriptSourceDirectory />
        <!--该元素设置了项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->
        <testSourceDirectory />
        <!--被编译过的应用程序class文件存放的目录。 -->
        <outputDirectory />
        <!--被编译过的测试class文件存放的目录。 -->
        <testOutputDirectory />
        <!--使用来自该项目的一系 -->
        ...  
    </build> 
···
</project>

resources

资源往往不是代码,无需编译,而是一些properties或XML配置文件,构建过程中会往往会将资源文件从源路径复制到指定的目标路径。

<project>
    ···

    <build>  
    ...  
        <filters>  
            <filter>filters/filter1.properties</filter>  
        </filters>  
        <resources>  
            <resource>  
                <targetPath>META-INF/plexus</targetPath>  
                <filtering>false</filtering>  
                <directory>${basedir}/src/main/plexus</directory>  
                <includes>  
                    <include>**/*.properties</include>
                    <include>**/*.xml</include> 
                </includes>  
                <excludes>  
                    <exclude>**/*.properties</exclude>  
                </excludes>  
            </resource>  
        </resources>  
        <testResources>  
        ...  
        </testResources>  
    ...  
    </build>  
    ···
</project>
  • 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/目录下。这里的资源文件不会被构建到目标构件中

plugins

引入构建过程用使用到的插件

<project>
    ···

    <build>  
    ...  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jar-plugin</artifactId>  
                <version>2.6</version>  
                <extensions>false</extensions>  
                <inherited>true</inherited>  
                <configuration>  
                    <classifier>test</classifier> 
                    <skipTests>true</skipTests>
                    <includes>
                        <include>com.my.asdf.**</include>
                    </includes>
                    <excludes>
                        <exclude>selftest.**</exclude>
                    </excludes>
                </configuration>  
                <dependencies>
                    ...
                </dependencies>  
                <executions>
                    ...
                </executions>  
            </plugin>  
        </plugins>  
    ...  
    </build>  
    ···
</project>
  • extensions,是否加载该插件的扩展,默认false
  • inherited,该插件的configuration中的配置是否可以被(继承该POM的其他Maven项目)继承,默认true
  • configuration,该插件所需要的特殊配置,在父子项目之间可以覆盖或合并
    • ==shipTests,调过测试==
    • include
    • excludes
  • dependencies,该插件所特有的依赖类库
  • executions,该插件的某个goal(一个插件中可能包含多个goal)的执行方式。一个execution有如下设置:
    id,唯一标识
    • goals,要执行的插件的goal(可以有多个),如run
    • phase,插件的goal要嵌入到Maven的phase中执行,如verify
    • inherited,该execution是否可被子项目继承
    • configuration,该execution的其他配置参数

maven仓库

repositories:pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于,pom里的仓库是个性化的。比如一家大公司里的setting文件是公用 的,所有项目都用一个setting文件,但各个子项目却会引用不同的第三方库,所以就需要在pom里设置自己需要的仓库地址。

<repositories> 
    <repository> 
        <releases> 
            <enabled>false</enabled> 
            <updatePolicy>always</updatePolicy> 
            <checksumPolicy>warn</checksumPolicy> 
        </releases> 
        <snapshots> 
            <enabled>true</enabled> 
            <updatePolicy>never</updatePolicy> 
            <checksumPolicy>fail</checksumPolicy> 
        </snapshots> 
        <id>codehausSnapshots</id> 
        <name>Codehaus Snapshots</name> 
        <url>http://snapshots.maven.codehaus.org/maven2</url> 
        <layout>default</layout> 
    </repository> 
</repositories> 
  • id,库的ID
  • name,库的名称
  • url,库的URL
  • layout,在Maven 2/3中都是default,只有在Maven 1.x中才是legacy
  • releases,库中版本为releases的构件
  • snapshots,库中版本为snapshots的构件

常用依赖

Spring framework

详情请移步: spring官方文档
spring框架详细介绍文章

Spring framework 框架结构分析

image

  1. 核心core container
    1. spring-core, spring-beans,提供控制反转和依赖注入功能
    2. spring-context,类似jndi注册处,提供资源绑定加载,事件广播,还有EJB, JMX, 和basic remoting等javaEE特征
    3. spring-context-support,提供第三方库集成到spring应用,如缓存 caching (EhCache, Guava, JCache), 邮件mailing (JavaMail), 计划任务scheduling (CommonJ, Quartz) 和模板引擎template engines (FreeMarker, JasperReports, Velocity)
    4. spring-expression 类似EL语言,是对EL的一种扩展
  2. Aop和Instrumentation部分
    1. spring-aop,允许开发者定义方法拦截器和切点
    2. spring-aspects,用于aspectj面向切面编程的集成,
    3. spring-instrument,用于集成特定容器,
    4. spring-instrument-tomcat,提供tomcat的集成
  3. Messaging部分
    1. spring-messaging与一些基于消息的应用的集成,提供一些消息相关的注解
  4. The Data Access/Integration部分
    1. spring-jdbc,使你免于编写数据库连接代码
    2. spring-tx,提供事务管理,
    3. spring-orm,允许开发者使用O/R对象关系映射框架,和上面提到的事务管理,
    4. spring-oxm,允许将spring-orm抽象成XML映射,即以xml文件来描述映射,
    5. spring-jms,JAVA消息服务,生成和消费消息,与spring-messaging集成
  5. WEB层
    1. spring-web,包含http客户端,多文件上传,利用监听器进行ioc容器初始化,提供应用上下文等功能
    2. spring-webmvc,提供MVC和REST WEB服务实现
    3. spring-webmvc-portlet,允许spring-webmvc用于Portlet组件环境(插件式管理)
  6. 测试层
    1. spring-test,支持单元测试或集成测试,提供了ApplicationContexts加载和contexts缓存。

image

数据库依赖

处理jdbc连接方式,常用的还有hibernate以及mybatis

<dependencies>
    <!-- mysql数据库驱动 -->  
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.27</version>
    </dependency>

    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.8</version>
    </dependency>

      <!-- hibernate4 -->  
    <dependency>  
        <groupId>org.hibernate</groupId>  
        <artifactId>hibernate-core</artifactId>  
        <version>4.2.5.Final</version>  
    </dependency>       
    <dependency>  
        <groupId>org.hibernate</groupId>  
        <artifactId>hibernate-entitymanager</artifactId>  
        <version>4.2.5.Final</version>  
    </dependency>  
    <dependency>  
        <groupId>org.hibernate</groupId>  
        <artifactId>hibernate-ehcache</artifactId>  
        <version>4.2.5.Final</version>  
    </dependency>  
    <dependency>  
        <groupId>org.hibernate.javax.persistence</groupId>  
        <artifactId>hibernate-jpa-2.0-api</artifactId>  
        <version>1.0.1.Final</version>  
    </dependency> 
</dependencies>

日志

常用 log4j

<dependency>  
    <groupId>log4j</groupId>  
    <artifactId>log4j</artifactId>  
    <version>1.2.17</version>  
</dependency>  

Servelet、jsp、jstl依赖

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version> 
</dependency>

<dependency> 
   <groupId>javax.servlet.jsp</groupId> 
   <artifactId>jsp-api</artifactId> 
   <version>2.1</version>  
</dependency>
<!-- jstl标签库 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

测试

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

plugins

maven plugins

pulgins配置文章
1. maven-compiler-plugin
2. maven-resources-plugin
3. maven-war-plugin
4. maven-jar-plugin
5. maven-clean-plugin
6. maven-assembly-plugin
7. maven-source-plugin

这些配置放到pom文件中,基本上可以直接用,需要用到哪个就引入。

maven-surefire-plugin

maven-surefire-plugin 是maven里执行测试用例的插件,不显示配置就会用默认配置。这个插件的 surefire:test 命令会默认绑定maven执行的 test 阶段。 maven-surefire-plugin介绍文章

常用配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit4</artifactId>
            <version>2.19</version>
        </dependency>
    </dependencies>
    <configuration>
        <skipTests>false</skipTests>
    </configuration>
</plugin>

当进行了单元测试或者mvn test 命令,就会使用到这个插件,在build时控制台会打印这个:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.qyf404.learn.maven.AppTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in com.qyf404.learn.maven.AppTest

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

要在build时调过单元测试可以:
1. 在插件的configuration配置中声明跳过测试用例: true
2. 在propertis添加:

<properties>
    <maven.test.skip>true</maven.test.skip>
</properties><properties>
    <skipTests>true</skipTests>
</properties>

·
·
to be continue ~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值