java+pom.xml+是什么_pom.xml详解

一、什么是POM

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

一个完整的pom.xml文件,放置在项目的根目录下。

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">

4.0.0

二、基本设置

1、maven的协作相关属性

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">

4.0.0

org.codehaus.mojo

my-project

1.0

war

groupId : 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。

artifactId : 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。

version : 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。

packaging : 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par

2、POM之间的关系

主要用于POM文件的复用。

a)依赖关系:依赖关系列表(dependency list)是POM的重要部分

junit

junit

4.0

test

groupId , artifactId , version :

scope : compile(default),provided,runtime,test,system

exclusions

b)继承关系:继承其他pom.xml配置的机制。

比如父pom.xml:

[...]

junit

junit

4.4

test

[...]

在子pom.xml文件继承它的依赖(还可以继承其他的:developers and contributors、plugin lists、reports lists、plugin executions with matching ids、plugin configuration):

[...]

com.devzuz.mvnbook.proficio

proficio

1.0-SNAPSHOT

[...]

在这种机制下,maven还提供了一个类似java.lang.Object的顶级父pom.xml文件:

4.0.0

Maven Default Project

central

Maven Repository Switchboard

default

http://repo1.maven.org/maven2

false

central

Maven Plugin Repository

http://repo1.maven.org/maven2

default

false

never

target

target/classes

${project.artifactId}-${project.version}

target/test-classes

src/main/java

src/main/scripts

src/test/java

src/main/resources

src/test/resources

maven-antrun-plugin

1.1

maven-assembly-plugin

2.2-beta-2

maven-clean-plugin

2.2

maven-compiler-plugin

2.0.2

maven-dependency-plugin

2.0

maven-deploy-plugin

2.3

maven-ear-plugin

2.3.1

maven-ejb-plugin

2.1

maven-install-plugin

2.2

maven-jar-plugin

2.2

maven-javadoc-plugin

2.4

maven-plugin-plugin

2.4.1

maven-rar-plugin

2.2

maven-release-plugin

2.0-beta-7

maven-resources-plugin

2.2

maven-site-plugin

2.0-beta-6

maven-source-plugin

2.0.4

maven-surefire-plugin

2.4.2

maven-war-plugin

2.1-alpha-1

target/site

release-profile

performRelease

true

true

org.apache.maven.plugins

maven-source-plugin

attach-sources

jar

true

org.apache.maven.plugins

maven-javadoc-plugin

attach-javadocs

jar

true

org.apache.maven.plugins

maven-deploy-plugin

true

可以通过下面命令查看当前pom.xml受到超pom.xml文件的影响:

mvn help:effective-pom

c)聚合关系:用于将多个maven项目聚合为一个大的项目。

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">

4.0.0

org.codehaus.mojo

my-parent

2.0

my-project

3、属性

maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。有以下分类:

env.X:操作系统环境变量,比如${env.PATH}

project.x:pom文件中的属性,比如:1.0,引用方式:${project.version}

settings.x:settings.xml文件中的属性,比如:false,引用方式:${settings.offline}

Java System Properties:java.lang.System.getProperties()中的属性,比如java.home,引用方式:${java.home}

自定义:在pom文件中可以:c:/apps/cargo-installs,引用方式:${installDir}

4、构建设置

构建有两种build标签:

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">

build中的主要标签:Resources和Plugins。

Resources:用于排除或包含某些资源文件

META-INF/plexus

false

${basedir}/src/main/plexus

configuration.xml

**/*.properties

Plugins:设置构建的插件

org.apache.maven.plugins

maven-jar-plugin

2.0

false

true

test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中读取Maven的pom.xml文件可以使用以下步骤: 1. 创建文件对象 首先需要创建一个File对象,用于表示pom.xml文件的路径。可以使用以下代码创建File对象: ``` File pomFile = new File("pom.xml"); ``` 2. 加载pom.xml文件 使用Java的DOM解析器来加载pom.xml文件。可以使用以下代码来加载pom.xml文件: ``` DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); ``` 3. 获取根元素 通过Document对象获取pom.xml文件的根元素。可以使用以下代码获取根元素: ``` Element root = doc.getDocumentElement(); ``` 4. 获取元素值 通过Element对象获取pom.xml文件中的元素值。可以使用以下代码获取元素值: ``` String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); ``` 以上代码将获取pom.xml文件中的groupId、artifactId和version元素的值。您可以根据需要获取其他元素的值。 完整的代码示例: ``` import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; public class ReadPomXml { public static void main(String[] args) throws Exception { File pomFile = new File("pom.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); Element root = doc.getDocumentElement(); String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); System.out.println("groupId: " + groupId); System.out.println("artifactId: " + artifactId); System.out.println("version: " + version); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值