Maven的pom.xml文件详解------The Basics

转载自  Maven的pom.xml文件详解------The Basics

Maven坐标

GroupId、artifactId和version构成了Maven的坐标(groupId和version可以从parent继承),指定了组件在Maven仓库中的位置。Maven中的每个组件都有一个坐标,通过这个坐标我们在自己的项目中可以设置对该组件的依赖。
 ------groupId:项目属于哪个组,往往和项目所在的组织或公司存在关联;
 ------artifactId:当前Maven项目在组中唯一的ID;
 ------version:定义当前项目的版本,如:1.0(-SNAPSHOT),SNAPSHOT表示快照,说明该项目还处于开发阶段,是不稳定版本;
 ------packaging:当我们有了groupId:artifactId:version作为地址后,还需要packaging为我们提供组件的类型,例如:<packaging>war</packaging>标识组件为一个war。如果packaging不指定,默认值为jar,当前可选值为:pom, jar, maven-plugin, ejb, war, ear, rar, par;
 ------classifier:可选,坐标也可以展示为groupId:artifactId:packaging:classifier:version。

 

POM关联

Maven的一个有力的地方就在于对项目关联的处理;包括依赖、继承和聚合(多模块项目)。

 

Dependencies

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.0</version><type>jar</type><scope>test</scope><optional>true</optional></dependency>...
</dependencies>

------groupId、artifactId、version:依赖组件的坐标,如果当Maven通过这些坐标无法从中心仓库获取该组件时,可以通过下面的方法处理:
  1、用安装插件安装本地依赖,在命令行中输入:
  mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
  2、创建你自己的仓库,并部署它
  3、设置依赖scope到system,并定义一个systemPath,但这个不推荐。
------type:对应所以来的组件的packaging;
------scop:用于控制依赖的范围,有以下几种范围供选择
  1、compile:编译依赖范围,默认,对于所有的classpath都是有效的;
  2、provided:仅对编译和测试classpath有效;
  3、runtime:编译时不需要,尽在运行时需要;
  4、test:仅用于测试;
  5、system:和provided类似,只是你需要提供JAR,组件不再在仓库中查找。
------systemPath:当scop配置为system时就需要它了

	<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.0</version><type>jar</type><scope>system</scope><systemPath>$(object.path)/lib/junit.jar</systemPath><optional>true</optional></dependency>

------optional:设置为true,标识该依赖只对该项目有效,如果其他项目依赖该项目,该依赖将不会传递。

 

Exclusions

该配置告诉Maven你不想包含的该依赖的依赖(即,依赖传递的依赖)。

<dependencies><dependency><groupId>org.apache.maven</groupId><artifactId>maven-embedder</artifactId><version>2.0</version><exclusions><exclusion><groupId>org.apache.maven</groupId><artifactId>maven-core</artifactId></exclusion></exclusions></dependency>...
</dependencies>

也可以使用通配符,表示排除所有传递的依赖。

<dependencies><dependency><groupId>org.apache.maven</groupId><artifactId>maven-embedder</artifactId><version>2.0</version><exclusions><exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion></exclusions></dependency>...
</dependencies>

 

Inheritance

继承是一个有力的工具,在maven中使用继承时,需要为parent和aggregation(多模块)项目设置packaging为pom:<packaging>pom</packaging>,然后就子项目就可以继承该POM了。

<parent><groupId>org.codehaus.mojo</groupId><artifactId>my-parent</artifactId><version>2.0</version><relativePath>../my-parent</relativePath>
</parent>

relativePath是可选的,指定parent的搜索路径,如果配置了,Maven将首先搜索relativePath,然后本地仓库,最后远程仓库查找parentPOM。
POM就像Java对象最终都继承自java.lang.Object一样,所有的POM都继承自一个Super POM,你通过查ungjianyige最小化的pom.xml并在命令行中执行 mvn help:effective-pom来查看Super POM对你的POM的影响,下面是Maven 3.0.4的Super POM,该Super POM的位置在apache-maven-version\lib\maven-model-builder-version.jar\org\apache\maven\model\下面,文件名pom-4.0.0.xml。

<project><modelVersion>4.0.0</modelVersion><repositories><repository><id>central</id><name>Central Repository</name><url>http://repo.maven.apache.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>central</id><name>Central Repository</name><url>http://repo.maven.apache.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><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><!-- NOTE: These plugins will be removed from future versions of the super POM --><!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) --><plugins><plugin><artifactId>maven-antrun-plugin</artifactId><version>1.3</version></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><version>2.2-beta-5</version></plugin><plugin><artifactId>maven-dependency-plugin</artifactId><version>2.1</version></plugin><plugin><artifactId>maven-release-plugin</artifactId><version>2.0</version></plugin></plugins></pluginManagement></build><reporting><outputDirectory>${project.build.directory}/site</outputDirectory></reporting><profiles><!-- NOTE: The release profile will be removed from future versions of the super POM --><profile><id>release-profile</id><activation><property><name>performRelease</name><value>true</value></property></activation><build><plugins><plugin><inherited>true</inherited><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><artifactId>maven-javadoc-plugin</artifactId><executions><execution><id>attach-javadocs</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><artifactId>maven-deploy-plugin</artifactId><configuration><updateReleaseInfo>true</updateReleaseInfo></configuration></plugin></plugins></build></profile></profiles></project>

 

Dependency Management

dependencyManagement用于在parent项目定义一个依赖,如:

<dependencyManagement><dependencies ><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.0</version><type>jar</type><scope>compile</scope><optional>true</optional></dependency></dependencies >
</dependencyManagement>

然后从这个parent继承的POMs就能设置他们的依赖为:

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

继承的POM的dependency的其它信息可以从parent的dependencyManagement中获得,这样的好处在于可以将依赖的细节放在一个控制中心,子项目就不用在关心依赖的细节,只需要设置依赖。

 

Aggregation(或者Multi-Module)

在多模块的项目中,可以将一个模块的集合配置到modules中。
<modules><module>my-project</module><module>another-project</module>
</modules>

在这里列出的模块不需要考虑顺序,Maven将自己根据依赖关系排序。

 

Properties

Maven中的Properties就像Ant中的,可以在POM的任何地方通过${X}来返回属性的值,X就表Poperty。 存在以下5种不同的类型: 1、env.X:将返回环境变量的值,如:${env.PATH}返回PATH环境变量的值; 2、project.X:POM中的对应元素的值,'.'表示在POM中的路径,如:<project><version>1.0</version></project>可以通过${project.version}获取值; 3、settings.X:settings.xml中包含的对应元素的值,'.'表示路径,如: <settings><offline>false</offline></settings>可以通过${settings.offline}获取; 4、Java System Properties:所有可以通过java.lang.System.getProperties()获取到的属性在POM属性中都是可用的,如:${java.home}; 5、X:在POM的<properties />中设置的元素,如: <properties><someVar>value</someVar></properies>通过${someVar}获取。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322669.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php artisan快捷命令

远程启动laravel&#xff1a; php artisan serve --host 0.0.0.0 创建新的Controller php artisan make:controller loginController 创建快捷资源Controller php artisan make:controller ArticlesController --resource 对应的创建快捷路由 Route::resource(‘articles’,…

P1407-[国家集训队]稳定婚姻【tarjan,强连通分量】

正题 题目链接:https://www.luogu.org/problemnew/show/P1407 题目大意 若干对夫妻&#xff0c;和若干对绿色关系&#xff0c;求每对夫妻离婚后&#xff0c;绿色关系是否可以重新让每个人两两配对。 解题思路 我们可以让 夫妻女的连男的 交往男的连女的 然后跑tarjantarjan…

基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别

背景 目前AI 处于风口浪尖&#xff0c;作为 公司的CTO&#xff0c;也作为自己的技术专研&#xff0c;开始了AI之旅&#xff0c;在朋友圈中也咨询 一些大牛对于AI 机器学习框架的看法&#xff0c;目前自己的研究方向主要开源的 AI 库&#xff0c;如&#xff1a;Emgu CV、TensorF…

Lombok的@Data生成的hashCode和equals方法坑

一、场景复现 创建两个lombok的Data注解的类Pig实例&#xff0c;放进HashMap当key&#xff0c;map里面的数据居然被覆盖了。 package com.mk;import lombok.Data; Data public class Pig extends Animal{private String sex; }package com.mk;import java.util.HashMap; impo…

vscode多行选中

shiftalt移动光标&#xff0c;可以连续同时编辑多行内容

jzoj4671-World Tour【图论,bfs】

正题 luogu题目链接:https://www.luogu.org/problemnew/show/CF666B 题目大意 求4个点&#xff0c;使得这4个点按顺序最短路到达的长度最远。 解题思路 用bfsbfsbfs求出每个点之间的最短路&#xff0c;然后对于每个点求出最远点&#xff0c;次远点&#xff0c;反最远点和反次…

vue-cli2、vue-cli3脚手架详细讲解

转载自 vue-cli2、vue-cli3脚手架详细讲解 前言&#xff1a; vue脚手架指的是vue-cli它是vue官方提供的一个快速构建单页面&#xff08;SPA&#xff09;环境配置的工具&#xff0c;cli 就是(command-line-interface ) 命令行界面 。vue-cli是基于node环境利用webpack对文件进…

微软为.NET程序员带来了最优的跨平台开发体验-WSL

前言 在前几个Visual Studio Code更新中发现有一个重要得特性&#xff0c;就是nodejs可以使用VS Code在WSL中进行Debug了&#xff08;WSL是指Win10中的Linux子系统&#xff09;,之前写过一篇文章是使用SSH对Linux环境进行Debug&#xff0c;此时的想法就是如果可以在WSL中直接对…

article之api文档

查 method:get http://127.0.0.1:8000/article 单条 http://127.0.0.1:8000/article/10 method:get 新增 http://127.0.0.1:8000/article method:post 修改 http://127.0.0.1:8000/article/10 method:put 删除 http://127.0.0.1:8000/article/3 method:delete ## 查询所有数据&…

jzoj4672-Graph Coloring【图论,模拟】

正题 题目大意 一张无向图&#xff0c;每条边有一个颜色(红或蓝)&#xff0c;可以选择点使得连接的边都取反&#xff0c;求至少要选多个点可以使得所有边的颜色相同。 解题思路 不难发现如果确定所有边的颜色&#xff0c;然后知道一个点的选择后就可以知道整个联通图的选择。…

Redis 性能问题分析

转载自 Redis 性能问题分析 在一些网络服务的系统中&#xff0c;Redis 的性能&#xff0c;可能是比 MySQL 等硬盘数据库的性能更重要的课题。比如微博&#xff0c;把热点微博[1]&#xff0c;最新的用户关系&#xff0c;都存储在 Redis 中&#xff0c;大量的查询击中 Redis&am…

谈谈微服务中的 API 网关(API Gateway)

前言 又是很久没写博客了&#xff0c;最近一段时间换了新工作&#xff0c;比较忙&#xff0c;所以没有抽出来太多的时间写给关注我的粉丝写一些干货了&#xff0c;就有人问我怎么最近没有更新博客了&#xff0c;在这里给大家抱歉。 那么&#xff0c;在本篇文章中&#xff0c;我…

laravel如何生成swagger接口文档

php artisan serve --host 0.0.0.0 php artisan serve --port 8080 地址&#xff1a; http://127.0.0.1/blogkjh/public/api/documentation 1、安装包 composer require darkaonline/l5-swagger 2、配置 php artisan vendor:publish --provider “L5Swagger\L5SwaggerService…

jzoj4673,CF578D-LCS again【统计,字符串,容斥】

正题 luoguluoguluogu题目链接:https://www.luogu.org/problemnew/show/CF578D 题目大意 求有多少个字符串TTT使得其和字符串SSS的LCSLCSLCS长度为∣S∣−1|S|-1∣S∣−1 解题思路 首先考虑挖一个空再填一个字母。 这样方案数为n∗n∗mn*n*mn∗n∗m 但是我们考虑aabaabaab这样…

OAuth2 实现单点登录 SSO

转载自 OAuth2 实现单点登录 SSO 1. 前言 技术这东西吧&#xff0c;看别人写的好像很简单似的&#xff0c;到自己去写的时候就各种问题&#xff0c;“一看就会&#xff0c;一做就错”。网上关于实现SSO的文章一大堆&#xff0c;但是当你真的照着写的时候就会发现根本不是那么…

Ocelot网关

Ocelot是一个.net core框架下的网关的开源项目&#xff0c;下图是官方给出的基础实现图&#xff0c;即把后台的多个服务统一到网关处&#xff0c;前端应用&#xff1a;桌面端&#xff0c;web端&#xff0c;app端都只用访问网关即可。 Ocelot的实现原理就是把客户端对网关的请求…

linux服务器部署laravel出现putenv() has been disabled for security reasons

putenv() has been disabled for security reasons 进入 www/serve/php/72/etc 找到 disable_functions后面的函数&#xff0c;删除 putenv() 如果报错 The Process class relies on proc_open, which is not available on your PHP installation. 删除 proc_open

欢乐纪中A组周六赛【2019.3.23】

前言 做A组被虐好惨 成绩 RankRankRank是有算别人的 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCC1313132017WYC2017WYC2017WYC1901901909090901001001000001919192017HZB2017HZB2017HZB1101101101001001001010100002727272017XJQ2017XJQ2017XJQ10010010010010010…

百度OCR文字识别-身份证识别

简介 答应了园区大牛张善友 要写AI 的系列博客&#xff0c;所以开始了AI 系列之旅。 一、介绍 身份证识别 API 接口文档地址&#xff1a;http://ai.baidu.com/docs#/OCR-API/top 接口描述 用户向服务请求识别身份证&#xff0c;身份证识别包括正面和背面。 请求说明 请求示例…

Spring Boot Elasticsearch 入门

转载自 芋道 Spring Boot Elasticsearch 入门 1. 概述 如果胖友之前有用过 Elasticsearch 的话&#xff0c;可能有过被使用的 Elasticsearch 客户端版本搞死搞活。如果有&#xff0c;那么一起握个抓。所以&#xff0c;我们在文章的开始&#xff0c;先一起理一理这块。 Elas…