maven标签详解

本文详细介绍了Maven中dependencies和dependencyManagement的区别,dependency的type和scope的使用,以及build部分的plugins和pluginManagement的管理作用。dependencyManagement用于管理依赖版本,不引入依赖,而dependencies引入实际依赖。scope如compile、provided、runtime、test和system分别控制依赖在不同阶段的使用。pluginManagement声明插件配置,不执行,plugins则直接引入并执行插件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven标签详解

1、dependenciesdependencyManagement

dependencyManagement一般是用在顶层的POM文件中,用来管理jar包的版本,子项目中就不用再指定版本号(只是声明依赖,并没有引入依赖)。

dependencies中的依赖都会自动引入,默认被所有子项目继承。

使用dependencyManagement更换版本只需要修改父POM中的版本即可。如果子项目需要另一个版本号,只需要在dependencies中声明一个版本号,就会优先使用子POM的版本号。

   	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
2、dependency中的typescope

type定义引入包的方式,默认是jar,还有war,pom。通过pom方式,可以把很多jar包打包到一个pom中,我们依赖了pom,就可以下载所有的jar包。

scope控制dependency元素的使用范围,默认是compile。

scope取值解析
compile(默认值)被依赖项目需要参与到项目的编译、测试、打包、运行等阶段,打包时通常会包含被依赖项目,是比较强的依赖。
provided被依赖项目理论上可以参与到项目的编译、测试、运行等阶段,但是在打包时进行了exclude动作。
应用场景:例如我们在开发一个web项目,在编译的时候我们需要依赖servlet-api.jar,但在运行时我们不需要这个jar,因为它已由应用服务器提供,这是我们就需要用provided来修饰这个依赖包。
runtime顾名思义,表示该依赖项目无需参与到项目的编译,但会参与到测试与运行阶段。
应用场景:例如在编译时我们不需要JDBC API的jar,但在运行时才需要JDBC的驱动包。
test表示该依赖项目仅会参与到项目的测试阶段。
应用场景:例如,Junit 测试。
system与provided类似,但是被依赖项不会从maven仓库查找依赖,而是从本地系统中获取,systemPath元素用于指定依赖在系统中jar的路径。
import它只使用在dependencyManagement中,我们知道maven和java只能单继承,作用是管理依赖包的版本,一般用来保持当前项目的所有依赖版本统一。
例如:项目中有很多的子项目,并且都需要保持依赖版本的统一,以前的做法是创建一个父类来管理依赖的版本,所有的子类继承自父类,这样就会导致父项目的pom.xml非常大,而且子项目不能再继承其他项目。

scope为system示例:

    <dependency>
        <groupId>com.cjf</groupId>
        <artifactId>auth-center</artifactId>
        <version>1.5</version>
        <scope>system</scope>
        <systemPath>${basedir}/MyContent/WEB-INF/lib/auth-center.jar</systemPath>
    </dependency>

type为pom和scope为import示例:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
3、build中的plugins和pluginManagement

pluginManagement只是声明插件,但不会加载该插件,用来统一管理插件,确保所有子POM中插件版本一致。

plugins就是直接引入插件。

(与dependencies和dependencyManagement区别类似)

plugins示例:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.lixiao.springbootstart.SpringbootStartApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

pluginManagement示例(spring-boot-starter-parent中声明):

<build>
    <resources>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.yaml</exclude>
          <exclude>**/application*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.jetbrains.kotlin</groupId>
          <artifactId>kotlin-maven-plugin</artifactId>
          <version>${kotlin.version}</version>
          <configuration>
            <jvmTarget>${java.version}</jvmTarget>
            <javaParameters>true</javaParameters>
          </configuration>
          <executions>
            <execution>
              <id>compile</id>
              <phase>compile</phase>
              <goals>
                <goal>compile</goal>
              </goals>
            </execution>
            <execution>
              <id>test-compile</id>
              <phase>test-compile</phase>
              <goals>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <parameters>true</parameters>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <executions>
            <execution>
              <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <classesDirectory>${project.build.outputDirectory}</classesDirectory>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>${start-class}</mainClass>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>${start-class}</mainClass>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <configuration>
            <delimiters>
              <delimiter>${resource.delimiter}</delimiter>
            </delimiters>
            <useDefaultDelimiters>false</useDefaultDelimiters>
          </configuration>
        </plugin>
        <plugin>
          <groupId>pl.project13.maven</groupId>
          <artifactId>git-commit-id-plugin</artifactId>
          <executions>
            <execution>
              <goals>
                <goal>revision</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <verbose>true</verbose>
            <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
            <generateGitPropertiesFile>true</generateGitPropertiesFile>
            <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <executions>
            <execution>
              <id>repackage</id>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <mainClass>${start-class}</mainClass>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <configuration>
            <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
            <createDependencyReducedPom>true</createDependencyReducedPom>
            <filters>
              <filter>
                <artifact>*:*</artifact>
                <excludes>
                  <exclude>META-INF/*.SF</exclude>
                  <exclude>META-INF/*.DSA</exclude>
                  <exclude>META-INF/*.RSA</exclude>
                </excludes>
              </filter>
            </filters>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <version>2.3.3.RELEASE</version>
            </dependency>
          </dependencies>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <transformers>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.handlers</resource>
                  </transformer>
                  <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                    <resource>META-INF/spring.factories</resource>
                  </transformer>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.schemas</resource>
                  </transformer>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <mainClass>${start-class}</mainClass>
                  </transformer>
                </transformers>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
Maven是一个流行的构建工具,它通过`<plugins>`标签来管理项目的插件。这个标签通常位于`<build>`标签内,用于配置项目构建过程中的各种辅助工具或任务。下面是对`<plugins>`标签的一些详解: 1. **定义**: `<plugins>`元素是一个`<pluginManagement>`或`<plugins>`元素的直接子元素,用于声明项目的插件及其版本。每个插件都作为一个`<plugin>`元素。 2. **插件引用**: 每个`<plugin>`元素都有`groupId`, `artifactId`, 和 `version` 属性,分别指定了插件的组织ID、插件ID和所使用的版本。 ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> ``` 3. **配置**: 插件可以有其特定的配置(如`<configuration>`标签),设置插件的参数和选项,比如JDK版本、资源过滤等。 4. **生命周期绑定**: 通过`<execution>`标签,你可以指定插件在哪个生命周期阶段运行,以及相关的配置细节。例如,编译插件一般会在`compile`阶段执行。 ```xml <execution> <id>default-compile</id> <phase>compile</phase> <goals><goal>compile</goal></goals> </execution> ``` 5. **自动化**: Maven允许你在`pom.xml`文件中声明所有依赖和配置,使得整个项目构建过程可以自动完成,无需手动干预。 **相关问题--:** 1. Maven有哪些内置的常用插件? 2. 如何避免在`<plugins>`部分出现循环依赖问题? 3. 如何查看已安装的Maven插件列表?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值