《Maven官方文档》POM文件(一)
POM(project object model)包含了工程信息和工程的配置细节,Maven使用POM文件来构建工程。POM文件包含了工程中的大部分默认值。举个例子,target是默认的构建目录,src/main/java是默认的源码目录,src/test/java是默认的测试源码目录,等等。
Maven2中的pom.xml就是Maven1中的project.xml。相比于在maven.xml中包含可执行的goal,现在goals和plugins都可以在pom.xml中配置。当执行一个task或者goal时,Maven会在当前目录下寻找并读取pom.xml来获取配置信息,然后执行goal。
能在pom.xml中声明的配置包括工程依赖(project dependencies),插件(plugins),可执行的目标(goals),构建配置(build profiles)等等。其他信息,比如工程版本,描述,开发者,邮件列表等等也可以在pox.xml中声明。
Super POMSuper POM是Maven的默认POM文件,除非你显示的声明继承关系,否则所有的POM文件都是在Super POM的基础上的扩展,也就是说,Super POM中的的配置会被你的工程中创建的pom.xml继承。Maven 2.0.x中的Super POM代码如下:
project modelVersion 4.0.0 /modelVersion name Maven Default Project /name repositories repository id central /id name Maven Repository Switchboard /name layout default /layout url http://repo1.maven.org/maven2 /url snapshots enabled false /enabled /snapshots /repository /repositories pluginRepositories pluginRepository id central /id name Maven Plugin Repository /name url http://repo1.maven.org/maven2 /url layout default /layout snapshots enabled false /enabled /snapshots releases updatePolicy never /updatePolicy /releases /pluginRepository /pluginRepositories build directory target /directory outputDirectory target/classes /outputDirectory finalName ${artifactId}-${version} /finalName testOutputDirectory target/test-classes /testOutputDirectory sourceDirectory src/main/java /sourceDirectory scriptSourceDirectory src/main/scripts /scriptSourceDirectory testSourceDirectory src/test/java /testSourceDirectory resources resource directory src/main/resources /directory /resource /resources testResources testResource directory src/test/resources /directory /testResource /testResources /build reporting outputDirectory target/site /outputDirectory /reporting profiles profile id release-profile /id activation property name performRelease /name /property /activation build plugins plugin inherited true /inherited groupId org.apache.maven.plugins /groupId artifactId maven-source-plugin /artifactId executions execution id attach-sources /id goals goal jar /goal /goals /execution /executions /plugin plugin inherited true /inherited groupId org.apache.maven.plugins /groupId artifactId maven-javadoc-plugin /artifactId executions execution id attach-javadocs /id goals goal jar /goal /goals /execution /executions /plugin plugin inherited true /inherited groupId org.apache.maven.plugins /groupId artifactId maven-deploy-plugin /artifactId configuration updateReleaseInfo true /updateReleaseInfo /configuration /plugin /plugins /build /profile /profiles /project
下面是Maven 2.1.x的Super POM:
project modelVersion 4.0.0 /modelVersion name Maven Default Project /name repositories repository id central /id name Maven Repository Switchboard /name layout default /layout url http://repo1.maven.org/maven2 /url snapshots enabled false /enabled /snapshots /repository /repositories pluginRepositories pluginRepository id central /id name Maven Plugin Repository /name url http://repo1.maven.org/maven2 /url layout default /layout snapshots enabled false /enabled /snapshots releases updatePolicy never /updatePolicy /releases /pluginRepository /pluginRepositories build directory ${project.basedir}/target /directory outputDirectory ${project.build.directory}/classes /outputDirectory finalName ${project.artifactId}-${project.version} /finalName testOutputDirectory ${project.build.directory}/test-classes /testOutputDirectory sourceDirectory ${project.basedir}/src/main/java /sourceDirectory !-- TODO: MNG-3731 maven-plugin-tools-api 2.4.4 expect this to be relative... -- scriptSourceDirectory src/main/scripts /scriptSourceDirectory testSourceDirectory ${project.basedir}/src/test/java /testSourceDirectory resources resource directory ${project.basedir}/src/main/resources /directory /resource /resources testResources testResource directory ${project.basedir}/src/test/resources /directory /testResource /testResources pluginManagement plugins plugin artifactId maven-antrun-plugin /artifactId version 1.3 /version /plugin plugin artifactId maven-assembly-plugin /artifactId version 2.2-beta-2 /version /plugin plugin artifactId maven-clean-plugin /artifactId version 2.2 /version /plugin plugin artifactId maven-compiler-plugin /artifactId version 2.0.2 /version /plugin plugin artifactId maven-dependency-plugin /artifactId version 2.0 /version /plugin plugin artifactId maven-deploy-plugin /artifactId version 2.4 /version /plugin plugin artifactId maven-ear-plugin /artifactId version 2.3.1 /version /plugin plugin artifactId maven-ejb-plugin /artifactId version 2.1 /version /plugin plugin artifactId maven-install-plugin /artifactId version 2.2 /version /plugin plugin artifactId maven-jar-plugin /artifactId version 2.2 /version /plugin plugin artifactId maven-javadoc-plugin /artifactId version 2.5 /version /plugin plugin artifactId maven-plugin-plugin /artifactId version 2.4.3 /version /plugin plugin artifactId maven-rar-plugin /artifactId version 2.2 /version /plugin plugin artifactId maven-release-plugin /artifactId version 2.0-beta-8 /version /plugin plugin artifactId maven-resources-plugin /artifactId version 2.3 /version /plugin plugin artifactId maven-site-plugin /artifactId version 2.0-beta-7 /version /plugin plugin artifactId maven-source-plugin /artifactId version 2.0.4 /version /plugin plugin artifactId maven-surefire-plugin /artifactId version 2.4.3 /version /plugin plugin artifactId maven-war-plugin /artifactId version 2.1-alpha-2 /version /plugin /plugins /pluginManagement /build reporting outputDirectory ${project.build.directory}/site /outputDirectory /reporting profiles profile id release-profile /id activation property name performRelease /name value true /value /property /activation build plugins plugin inherited true /inherited groupId org.apache.maven.plugins /groupId artifactId maven-source-plugin /artifactId executions execution id attach-sources /id goals goal jar /goal /goals /execution /executions /plugin plugin inherited true /inherited groupId org.apache.maven.plugins /groupId artifactId maven-javadoc-plugin /artifactId executions execution id attach-javadocs /id goals goal jar /goal /goals /execution /executions /plugin plugin inherited true /inherited groupId org.apache.maven.plugins /groupId artifactId maven-deploy-plugin /artifactId configuration updateReleaseInfo true /updateReleaseInfo /configuration /plugin /plugins /build /profile /profiles /project最小化POM
一个最小化的POM文件的要求如下:
project 标签作为顶层标签 modelVersion – 应该设为4.0.0 groupId – 工程开发组的唯一id artifactId – 工件(artifact)或工程(projrct)的唯一id version – 版本号这里是一个最小化POM的例子:
project modelVersion 4.0.0 /modelVersion groupId com.mycompany.app /groupId artifactId my-app /artifactId version 1 /version /project
在POM文件中需要声明 groupId , artifactId 和 version 。这三个值以 groupId : artifactId : version 的形式声明,它们组成了工程的完整名称。比如上面的例子,完整名称为”com.mycompany.app:my-app1″。
如第一节所说,如果配置细节没有显示的设置,Maven将会使用继承自Super POM默认配置。其中一个默认值就是包的类型(packaging type),每个Maven工程都有一个包类型,如果没有在POM中设置,则默认为”jar”。
前面的Minimal POM中,其中 repositories 这个值没有设置,如果你使用minimal POM来构建你的工程,它将使用继承Super POM中的 repositories 值(http://repo.maven.apache.org/maven2),当Maven在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 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion 4.0.0 /model
Maven pom 中配置依赖机制 依赖管理是 Maven 的一个核心特性。管理单个项目的依赖关系非常简单。管理由数百个模块组成的多模块项目和应用程序的依赖关系是可能的。Maven 使用定义良好的类路径和库版本在定义、创建和维护可重复的构建方面帮助很大。
相关文章
- 快速创建SpringBoot2.x应用之工具类自动创建web应用、SpringBoot2.x的依赖默认Maven版本
- 【Maven异常】Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
- 使用Nexus管理maven仓库,setting文件理解
- 【Maven】解决maven打jar包报错 source 1.3 中不支持注释 (请使用 -sour
- maven打包资源文件(转)
- eclipse创建Maven项目,无法添加src/main/java source folder 问题
- 执行 Maven 编译的 jar,找不到相关的 依赖的类--使用 maven-assembly-plugin 解决
- IntellJ idea maven打包——项目带有第三方jar包
- jenkins pipeline、用户权限管理、插件下载地址更改、凭证管理、自由风格项目构建、maven项目构建、常用的构建触发器、邮件发送、SonarQube代码审查
- lunbuntu install maven
- idea maven配置默认选项(不用每次都重新设置)
- Maven 3.8.1新特性:默认屏蔽外部 HTTP 资源库(会导致repo或mirror地址为http时报错)
- maven pom resources标签的使用示例
- JavaFX maven工程pom文件完整内容
- aven依赖分析,jar包冲突解决利器intellij idea插件Maven Helper强烈建议安装
- Maven plugin - maven-dependency-plugin
- 【异常】IDEA中提示Maven报错,Blocked mirror for repositories:[nexus (http://xxxxx, default , releases+snapshot
- 【异常】IDEA中Maven插件提示异常Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin
- How to import a Maven project with Intellij Command line tool with command "idea pom.xml"?
- 使用Maven编译项目遇到——“maven编码gbk的不可映射字符”解决办法
- java的maven项目(一)
- Maven deploy 报错: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:deploy (default-deploy) on project Failed to retrieve remote metadata /maven-metadata.xml
- Maven的生命周期是为了对所有的构建过程进行了抽象了,便于统一。
- 部署基于Maven的war文件到Tomcat
- apache-maven-3.8.4下载配置【解决JDK15与Maven-3.3.9低版本不匹配问题---mvn -v无法找到JAVA_HOME环境变量】
- Maven(未完善)
- Maven父子结构的项目依赖使用以及打包依赖_微服务项目(maven父子级项目)怎么打包