maven-learning03

学习目标:

  • 使用STS创建Maven项目
  • maven的依赖管理
  • maven的聚合
  • maven的继承

使用STS创建一个Maven项目

  • 使用STS(Spring Tool Suite)进行Maven项目的开发
    修改字体大小window–>perferences–>General–>Colors and Fonts–>Basic–>Text Font
  • 修改Maven
    Window->preference->Maven->Installations修改,点击Add,选择本机上的maven版本
  • 新建Maven Project
    next->选择archetype->[maven-archetype-quickstart,maven-archetype-webapp(站点)]
    GroupId -> 部门网址倒写+项目
    ArtifactId -> 项目模块
    Version -> 0.0.1-SNAPSHOT
  • next之后所有的文件夹就会被建好
  • 资源文件,源文件的资源放在src/main/resources;测试文件的资源文件放在src/test/resources

依赖

<dependency>
 <!--设置依赖范围-->
 <scope>xxx<scope>
</dependency>

依赖类型

  • compile:编译范围有效,在编译和打包时都会讲依赖存储进去
  • test:在测试范围有效,在编译和打包时都不会使用这个依赖
  • provided:在编译和测试的过程都有效,最后生成war包时不会加入,诸如service-api,因为service-api,tomcat等服务器已经存在了,如果再打包会冲突
  • runtime:在运行时依赖,在编译的时候不会依赖
  • system

默认的依赖范围是compile

依赖继承

假设两个项目A,B
A依赖log4j-1.2.17(直接依赖)
B依赖log4j-1.2.9(直接依赖)

C依赖B,A,且C显示依赖log4j-1.2.9(间接依赖)
级别相同,先声明的先依赖

A->dbunit-2.2->commons-logging-1.0.4
B->commons-logging-1.1.1
C 依赖A,B

依赖级别不相同,则依赖层数最小的,此例中C依赖commons-logging-1.1.1

依赖的传递,主要针对compile作用域传递,test范围不会被传递

依赖冲突

如果不想使用某个版本的依赖
可以使用

<!--
    maven-ch04依赖MySQL 5.1.37
    maven-ch05依赖MySQL 5.1.35
-->
<dependencies>
  <dependency>
      <groupId>org.learning.maven</groupId>
      <artifactId>maven-ch04</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <!--如果没有下述代码,则maven-ch06将依赖MySQL 5.1.37-->
      <exclusions>
           <exclusion>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
           </exclusion>
      </exclusions>
    </dependency>
    <!--maven-ch06将依赖MySQL 5.1.35-->
    <dependency>
      <groupId>org.learning.maven</groupId>
      <artifactId>maven-ch05</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
  </dependencies>

聚合

分模块管理项目,带来的最大问题就是需要将模块一个个依次编译,所以引入了聚合概念。

所谓的聚合就是在多个模块的根目录中创建一个pom.xml文件集中编译项目模块(将多个pom.xml文件聚合到一起)。

在eclipse中可以创建一个简单项目(跳过archetype创建),并且在package中选择pom(管理类型),这个简单项目中只需要编写pom.xml
在这个聚合pom.xml中添加

<modules>
    <module>文件所在位置(可以使用“..”这样的相对路径)</module>
    ...
</modules>

这个并不会改变目录结构,依然跟依次maven其他项目的结果一样。这样的好处就是可以一次性的操作多个模块

继承

每个项目都有一个pom.xml文件,而这些文件可能存在一些冗余信息,即这些文件存在一些共同的信息,如常量,常用的依赖包

所以就有了继承,所谓的继承就是将多个pom.xml文件中的相同信息聚合到一个文件(跟聚合类似了)

创建的过程与聚合类似,创建一个简单的项目(package 为 pom),只包含一个pom.xml文件

将多个项目中pom.xml一样的都拷贝到在本pom.xml文件

对于依赖的继承,需要使用<dependencyManager></dependencyManager>

<dependencyManagement>
  <dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.37</version>
    </dependency>
  </dependencies>
</dependencyManagement>

不仅减少了代码的编写,而且也避免了依赖包版本冲突问题

通过上述的藐视,可以发现继承和聚合很类似,所以可以将<modules></modules>添加到继承的pom.xml文件中
如:

<modules>
    <!--模块的位置-->
    <module>../maven-ch04</module>
    <module>../maven-ch05</module>
    <module>../maven-ch06</module>
  </modules>

其他项目继承这个pom.xml文件时使用:

<parent>
      <groupId>org.learning.maven</groupId>
      <artifactId>maven-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <!-- 继承的pom.xml文件的相对路径 -->
      <relativePath>../maven-parent/pom.xml</relativePath>
  </parent>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory.getPoolConfig(LettuceConnectionConfiguration.java:207) The following method did not exist: 'void org.apache.commons.pool2.impl.GenericObjectPoolConfig.setMaxWait(java.time.Duration)' The calling method's class, org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory, was loaded from the following location: jar:file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/springframework/boot/spring-boot-autoconfigure/3.1.2/spring-boot-autoconfigure-3.1.2.jar!/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration$PoolBuilderFactory.class The called method's class, org.apache.commons.pool2.impl.GenericObjectPoolConfig, is available from the following locations: jar:file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar!/org/apache/commons/pool2/impl/GenericObjectPoolConfig.class The called method's class hierarchy was loaded from the following locations: org.apache.commons.pool2.impl.GenericObjectPoolConfig: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar org.apache.commons.pool2.impl.BaseObjectPoolConfig: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar org.apache.commons.pool2.BaseObject: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory and org.apache.commons.pool2.impl.GenericObjectPoolConfig
07-24
The error message suggests that there is a version incompatibility between the classes `LettuceConnectionConfiguration$PoolBuilderFactory` from the Spring Boot autoconfigure library and `GenericObjectPoolConfig` from the Apache Commons Pool library. To resolve this issue, you need to ensure that compatible versions of these libraries are present on your application's classpath. Here's what you can do: 1. Check your application's dependencies: Verify that you have the correct versions of the Spring Boot and Apache Commons Pool libraries specified in your project's dependencies. 2. Resolve version conflicts: If you are using a build tool like Maven or Gradle, ensure that the versions specified for both Spring Boot and Apache Commons Pool are compatible. If there are any conflicting versions, try to align them to a compatible version. 3. Exclude conflicting transitive dependencies: If you are using a build tool, you can exclude the transitive dependencies of Spring Boot or Apache Commons Pool that might be causing conflicts. This can be done by adding exclusion rules in your build file. For example, in Maven, you can exclude a transitive dependency like this: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>3.1.2</version> <exclusions> <exclusion> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </exclusion> </exclusions> </dependency> ``` By resolving any version conflicts and ensuring compatible versions of the libraries are present on the classpath, you should be able to resolve the method not found error.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值