Maven

129 阅读3分钟

1.可选依赖

<groupId>org.example</groupId>
<artifactId>maven_demo1</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
  <dependency>
    <groupId>org.example</groupId>
    <artifactId>maven_demo0</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>
<groupId>org.example</groupId>
<artifactId>maven_demo0</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <optional>true</optional>
  </dependency>
</dependencies>

optional可选依赖,对外隐藏当前所依赖的资源。即maven_demo0无法得知maven_demo1引用了该依赖。(不透明)

image.png

2.排除依赖

<groupId>org.example</groupId>
<artifactId>maven_demo</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.18</version>
  </dependency>
</dependencies>
<groupId>org.example</groupId>
<artifactId>maven_demo01</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
  <dependency>
    <groupId>org.example</groupId>
    <artifactId>maven_demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <exclusions>
      <exclusion>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
</dependencies>
<exclusions>
    <exclusion>
    
    </exclusion>
</exclusions>

排除依赖放在exclusion中,用于主动断开依赖,并且排除的依赖无需指定版本。(不需要)

image.png

3.依赖范围

<scope></scope>

依赖范围共有complie(默认)、test、provided、runtime,作用范围如下图:

image.png

当该依赖被传递时,依赖范围会随着直接依赖和间接依赖的依赖范围变化而变化,对应表如下图:

image.png

4.插件

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <phase>verify</phase>
          <goals>
              <goal>jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

每个插件都有对应的坐标,插件中通过phase可以指定该插件在哪个阶段生效。goal可以指定作用对象,不同的插件的作用对象不同,可在官网查到各个插件的goal的名称和作用。

5.其他注意事项

1.分模块开发时,用到其他的自定义模块,需要先将被依赖的模块进行install安装到本地仓库,才能正常使用该模块中。

6.聚合、继承

6.1.聚合

聚合是指用一个单独的模块来统一管理多个模块

<packaging>pom</packaging>

表示该模块为一个聚合模块

<modules>
  <module></module>
</modules>

module中写具体的工程名称(不是坐标),来表示管理的模块


6.2.继承

继承一般用于管理各个依赖的版本,将依赖版本交给父工程管理,防止各个模块中依赖版本发生不兼容问题。

子工程可以沿用父工程的groupId和version。父工程的打包方式也是pom

<dependencyManagement>
  <dependencies>
  
  </dependencies>
</dependencyManagement>

在父工程中,将各个依赖存在dependencyManagement下


<parent>
  <groupId></groupId>
  <artifactId></artifactId>
  <version></version>
</parent>

在子工程中,将继承的父工程写在parent下。若子工程中的依赖存在于父工程,则无需指定版本号,默认为父工程的版本号。若不存在,则可以单独指定版本号。

插件版本管理同依赖版本管理。


7.打包方式

<packaging></packaging>

打包方式有jar(默认)、war、。pom,jar用于普通的Java应用,war用于web应用,pom用于聚合模块


8.属性

<properties>
  <springboot.version>3.1.5</springboot.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>${springboot.version}</version>
  </dependency>
</dependencies>

在properties下定义属性名和版本,在依赖中使用${}来指定属性。


<groupId>org.example</groupId>
<artifactId>maven_demo01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
  <dependency>
    <groupId>org.example</groupId>
    <artifactId>maven_demo</artifactId>
    <version>${version}</version>
  </dependency>
</dependencies>

当为${version}时,版本即为当前工程的版本号(即1.0-SNAPSHOT)。


9.资源配置

maven中的属性可以管理pom文件下的版本,也可以管理配置文件(application.yml)中的属性值。

server:
  port: @port123@

yml文件或properties文件引用maven下的属性时,需要用@属性名@。


<properties>
    <port123>1234</port123>
</properties>
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

在yml引用时,需要开启maven的资源过滤。directory中放入资源的目录,若想要让所有的模块都能访问当前模块的属性,可以用

<directory>${project.basedir}/src/main/resources</directory>

10.多环境开发

同一个项目可能会有多个开发环境,而不同的开发环境的依赖版本、属性等可能不相同。通过maven的多环境开发可以设置不同环境下的不同配置。

<profiles>
  <profile>
    <id>developed</id>
    <properties>
      <port>8081</port>
    </properties>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
  </profile>
  <profile>
    <id>test</id>
    <properties>
      <port>8082</port>
    </properties>
  </profile>
</profiles>

在profiles中定义不同的profile,不同的profile通过不同的id进行区分。activeByDefault为true时,表示当前环境为默认。

使用maven时在命令后加上-P id,即可指定使用的环境。