建设公司需要网站吗,角门网站建设,浙江建设培训中心网站,郑州网站建设哪家本文摘要 Maven作为Java后端使用频率非常高的一款依赖管理工具#xff0c;在此咱们由浅入深#xff0c;分三篇文章#xff08;Maven基础、Maven进阶、私服搭建#xff09;来深入学习Maven#xff0c;此篇为开篇主要介绍Maven进阶知识#xff0c;包含坐标、依赖、仓库、生…本文摘要 Maven作为Java后端使用频率非常高的一款依赖管理工具在此咱们由浅入深分三篇文章Maven基础、Maven进阶、私服搭建来深入学习Maven此篇为开篇主要介绍Maven进阶知识包含坐标、依赖、仓库、生命周期、插件、继承 文章目录 本文摘要1. 坐标1.1 坐标含义 2. 依赖2.1 依赖范围1. classpath2. scope 2.2 依赖传递2.3 依赖冲突2.4 依赖阻断 3. 仓库3.1 仓库配置3.1.1 本地仓库3.1.2 中央仓库-阿里云仓库配置 4. 生命周期5. 插件6. 继承6.2 可继承的pom元素6.3 dependencyManagement6.4 porperties 1. 坐标 坐标是构件的唯一标识Maven 坐标的元素包括groupId、artifactId、version、packaging、classifier。上述5个元素中groupId、artifactId、version 是必须定义的packaging 是可选的 ( 默认为 jar ) 1.1 坐标含义
groupIdorg.example/groupId
artifactIdstudy-maven/artifactId
version1.0-SNAPSHOT/version坐标元素含义举例groupId组织标识一般为公司网址反写项目名org.exampleartifactId项目名称一般为:项目名-模块名study-mavenversion版本号0.0.1-SNAPSHOPT 第一个0表示大版本号 第二个0表示分支版本号 第三个0表示小版本号 SNAPSHOT快照 ALPHA内测版本 BETA公测版本 RELEASE稳定版本 GA正式版本study-mavenpackaging打包方式pom jarmaven-pluginejbwar…clissifier用来帮助定义构件输出的一些附属构件通常不用
2. 依赖
2.1 依赖范围
1. classpath
Maven项目在开发工程中有三套classpath
主代码main下面的都是主代码在编译的时候的依赖测试代码test下是测试代码编译的时候的依赖运行时main代码在运行的时候对包的依赖 依赖范围的使用,通过在引用第三方依赖时的标签进行设置,例如: 2. scope Maven中共 6 种 scope包括compile、provided、runtime、test、system、import但实际生产中用得最多的也就标红的四种 compile在编译、测试、打包时都会将jar包打进项目中test只在运行测试classpath里边的代码才会生效provided只能测试、main的classpath代码生效runtime的classpath无效runtime只有在runtime的classpath环境下生效
2.2 依赖传递 依赖传递指A项目依赖了B项目B项目依赖了C项目则A项目中也会引入C项目其中A依赖B被称为第一传递依赖B依赖C被称为第二传递依赖 依赖传递原则 2.3 依赖冲突 依赖冲突指A项目依赖B项目B项目依赖C项目的1.0版本同时A项目依赖D项目的1.1版本根据依赖传递则A项目中同时具有D项目的1.0、1.1版本此时就存在依赖冲突 冲突解决 谁先声明谁有效如下1.0版本先声明故a项目中使用的是c项目的1.0版本
!-- b项目依赖c项目1.0版本 --dependencygroupIdorg.example/groupIdartifactIdmaven-b/artifactIdversion1.0-SNAPSHOT/version/dependency!-- b项目依赖c项目1.1版本 --dependencygroupIdorg.example/groupIdartifactIdmaven-b/artifactIdversion1.1-SNAPSHOT/version/dependency直接引入比依赖传递更具优先性如下a项目直接引入c项目故a项目中c项目的1.1版本生效
!-- b项目依赖c项目1.0版本 --dependencygroupIdorg.example/groupIdartifactIdmaven-b/artifactIdversion1.0-SNAPSHOT/version/dependency!-- a目直接项依赖c项目1.1版本 --dependencygroupIdorg.example/groupIdartifactIdmaven-c/artifactIdversion1.1-SNAPSHOT/version/dependency排除依赖直接排除b项目中的c依赖故而a项目使用c项目的1.1版本
!-- b项目依赖c项目1.0版本 --
dependencygroupIdorg.example/groupIdartifactIdmaven-b/artifactIdversion1.0-SNAPSHOT/versionexclusionsexclusiongroupIdorg.example/groupIdartifactIdmaven-b/artifactId/exclusion/exclusions
/dependency!-- b项目依赖c项目1.1版本 --
dependencygroupIdorg.example/groupIdartifactIdmaven-b/artifactIdversion1.1-SNAPSHOT/version
/dependency2.4 依赖阻断
当项目b被其它项目引用时不传递c依赖 dependencygroupIdorg.example/groupIdartifactIdmaven-c/artifactIdversion1.0-SNAPSHOT/versionoptionaltrue/optional/dependency3. 仓库 仓库分类仓库分为本地仓库、中央仓库、远程仓库其中本地仓库即个人配置的本地仓库、远程仓库即公司配置的私服仓库、中央仓库即为apache或阿里配置的仓库 3.1 仓库配置
3.1.1 本地仓库
localRepositoryD:\repo/localRepository3.1.2 中央仓库-阿里云仓库配置
mirrorsmirroridalimaven/idmirrorOfcentral/mirrorOfnamealiyun maven/nameurlhttp://maven.aliyun.com/nexus/content/groups/public//url/mirror
/mirrors4. 生命周期 生命周期maven有三种生命周期clean、default、site每个生命周期又包含不同的阶段后一阶段的执行都必须先执行前一阶段指令后才执行下一阶段指令 生命周期cleandefaultsite阶段(phase)执行顺序由上至下pre-cleanvalidatepre-sitecleaninitializesitepost-cleangenerate-sourcespost-siteprocess-sourcessite-deploygenerate-resourcesprocess-resourcescompileprocess-classesgenerate-test-sourcesgenerate-test-resourcesprocess-test-resourcestest-compileprocess-test-classestestprepare-packagepackagepre-integration-testintegration-testpost-integration-testverifyinstalldeploy
5. 插件 maven插件的执行实际就是从中央仓库拉取插件jar包然后通过执行对应命令方式来执行插件因此当我们在执行插件时卡着不动可以考虑将阿里云中央仓库给注释掉直接从apache中央仓库拉取 buildplugins!--配置编译插件通常自带编译插件版本较老因而我们需要重新配置 --plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.1/versionconfigurationsource1.8/sourcetarget1.8/targetencodingUTF-8/encoding/configuration/plugin!-- Javaweb项目我们可以通过配置tomcat插件来运行项目 --plugingroupIdorg.apache.tomcat.maven/groupIdartifactIdtomcat7-maven-plugin/artifactIdversion2.2/versionconfiguration!--端口控制--port8080/port!--项目路径控制意味着http://localhost:8080/abc--path/abc/path!--编码--uriEncodingUTF-8/uriEncoding/configuration/plugin/plugins
/build6. 继承
父工程pom packaging为pommodules中包含了子工程坐标 子工程 6.2 可继承的pom元素
groupId项目组 ID 项目坐标的核心元素version项目版本项目坐标的核心元素properties自定义的 Maven 属性dependencies项目的依赖配置dependencyManagement醒目的依赖管理配置
6.3 dependencyManagement 父类通过dependencyManagement来控制所要引入的依赖和对应版本子类根据选择只需要引入对应坐标即可无需重新定义版本号从而来控制依赖版本此时依赖只做依赖定义并未进行依赖引入 dependencyManagementdependenciesdependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.83/version/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactId/dependency/dependencies6.4 porperties properties用于定义统一版本 !-- 通过properties定义通过${}方式使用 --
propertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetfastjson1.2.83/fastjson
/propertiesdependencyManagementdependenciesdependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion${fastjson}/version/dependency/dependencies
/dependencyManagement