maven中的pom

pom的最低要求配置

总共5个

  1. project-根元素
  2. modelVersion -设置为4.0.0即可
  3. groupId-项目分组的id
  4. artifactId-分组下具体的artifact的id
  5. version-分组下具体的artifact的版本

3 4 5称为组成maven的坐标 gav

形如:

<project>
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.mycompany.app</groupId>
 <artifactId>my-app</artifactId>
 <version>1</version>
</project>

实例:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xl.test</groupId>
  <artifactId>z-pom-test</artifactId>
  <version>1.0-test</version>
</project>

在这里插入图片描述
摘自maven官网:

Introduction to the POM

pom的默认行为

  • 为什么没有看见常见的配置<packaging>war</packaging>

<packaging>属于maven的一个默认配置项,如果不配置则默认打包方式为jar,也可显示的配置为:<packaging>jar</packaging>

摘自官网:Introduction to the POM 之 Minimal POM

Also, as mentioned in the first section, if the configuration details are not specified, Maven will use their defaults. One of these default values is the packaging type. 
Every Maven project has a packaging type. If it is not specified in the POM, then the default value "jar" would be used
  • 为什么Eclipse中的打开pom.xml文件点击“Effective POM”多次许多内容?<repositories>
    <pluginRepositories>等等在这里插入图片描述
    所有新建的maven工程都会自动继承一个super pom.xml, 上图中多出来的这些内容都是maven自带的默认的super pom.xml。
The Super POM is Maven's default POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects.

You can see the Super POM for Maven 3.6.3 in Maven Core reference documentation.

摘自官网:Introduction to the POM 之 Super POM

packaging有哪些

  1. jar(默认)
  2. pom
  3. war
  4. ejb
  5. ear
  6. rar
  7. maven-plugin
  8. java-source
  9. javadoc

关于dependency

元素包含:

  • groupId
  • artifactId
  • version
  • type
  • scope
  • optional
  • classifier
  • systemPath

形如:

 	<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>

前3个前面已经介绍,后面3个参考官网:POM Reference中的Dependencies;下面介绍下中间的两个typescope

type

Corresponds to the chosen dependency type. This defaults to jar. While it usually represents the extension on the filename 
of the dependency, that is not always the case: a type can be mapped to a different extension and a classifier. The type 
often corresponds to the packaging used, though this is also not always the case. Some examples are jar, ejb-client and
 test-jar: see default artifact handlers for a list. New types can be defined by plugins that set extensions to true, so this is 
 not a complete list.

大致翻译:
type指的是依赖的类型。这个类型一般与依赖的打包方式是一致的(如果type是war那么packaging也是war),但是也有例外的情况,对照关系可参照下表:
在这里插入图片描述

摘自官网:POM Reference中的Dependencies

scope

Dependency scope is used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath.

翻译:
scope用于限制依赖的传递性(约束传递的范围)以及在 类路径过程中 依赖什么时候被包含(是在编译类路径过程?测试类路径过程?还是运行类路径过程?)。

scope与classpath关系一览表:
在这里插入图片描述

依赖的传递性可参考:

maven高级——依赖的传递性

Maven依赖传递

scope有如下几种:

  1. compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. 
Furthermore, those dependencies are propagated to dependent projects

compile是默认的scope,如果不显示指定那默认就是compile.Compile在项目的所有路径中都可用。

  1. provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For 
example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet 
API and related Java EE APIs to scope provided because the web container provides those classes. A dependency with 
this scope is added to the classpath used for compilation and test, but not the runtime classpath. It is not transitive.

scope为provided的依赖只用于编译和测试的类路径过程中,在运行时有JDK或者容器提供支持。

  1. runtime
This scope indicates that the dependency is not required for compilation, but is for execution. Maven 
includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath.

scope为runtime时,在运行和测试时的类路径过程中需要,但是在编译的类路径过程中不需要。

  1. test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive. Typically this scope is used for test libraries such as JUnit and Mockito. It is also used for non-test libraries such as Apache Commons IO if those libraries are used in unit tests (src/test/java) but not in the model code (src/main/java).

scope为test的依赖只用于测试阶段(测试阶段编译和执行)的类路径过程中。

  1. system
This scope is similar to provided except that you have to provide the JAR which 
contains it explicitly. The artifact is always available and is not looked up in a repository.

scope为system的依赖,须是本地系统的依赖(JAR包形式),不能是maven仓库中的依赖。

  1. import
This scope is only supported on a dependency of type pom in the 
<dependencyManagement> section. It indicates the dependency is to be replaced with 
the effective list of dependencies in the specified POM's <dependencyManagement> 
section. Since they are replaced, dependencies with a scope of import do not 
actually participate in limiting the transitivity of a dependency.

scope为import的依赖只用于元素<dependencyManagement>,并且依赖的type必须为pom类型
形如:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在<dependencyManagement>中的scope为import并且type为pom的依赖dependency本身并不是有效的依赖,但是 :其代表了一系列的实际有效的依赖集合。

在<dependencyManagement>中的scope为import并且type为pom的依赖dependency本身并不是有效的依赖,但是 :其代表了一系列的实际有效的依赖集合。

在<dependencyManagement>中的scope为import并且type为pom的依赖dependency本身并不是有效的依赖,但是 :其代表了一系列的实际有效的依赖集合。

解释:

如上面的依赖spring-cloud-alibaba-dependencies并不是有效的依赖,而是spring-cloud-alibaba的一系列的有效依赖的集合的代表:
在这里插入图片描述
在这里插入图片描述

摘自官网:Introduction to the Dependency Mechanism 之 Dependency Scope

关于 Dependency Management

  • 一般用于parent项目的pom中‘’
  • 好处:
    1. 有多个child项目继承同一个parent项目时,可以避免在每一个项目中都声明版本号,只需在parent项目中声明即可;
    2. 需要修改版本时,也只需修改parent项目中的版本即可
    3. 某一个child项目需要不一样的版本,只需要在自己的项目中单独加上版本号即可
  • 注意!!!<dependencyManagement>里面只是声明依赖,并不引入实现,所以child项目需要显示的声明需要使用的依赖,只是不用写版本号而已!
  • 示例 1
    在这里插入图片描述
    在这里插入图片描述
  • 示例 2, 子项目中如何引入类型为pom以及scope为import的依赖

前面已经介绍过import 、 pom类型的依赖,:代表的是一些列有效的依赖的集合。子项目中要引入只需要“ctrl + 左键” 进去后找到对应的依赖集合,选择其中一个或多个引入到子项目即可。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

构建maven聚合工程,父子工程

  1. 新建maven项目,packaging选择pom,删除掉出pom.xml以外的文件/文件夹
    在这里插入图片描述

  2. 鼠标右键刚才新建的父工程,创建新maven工程。。。
    在这里插入图片描述
    在这里插入图片描述

maven官方文档 !!!

POM Reference
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值