自学内容网 自学内容网

项目模块化下的maven管理工具的使用

  1. 开门见山--情境导入

见如下项目的架构,如图,我们可以看到在当前的项目目录中,有两个项目,一个是模块是实现逻辑类的功能

xf-common, 另外一个模块是现实业务逻辑功能 xf-file-manager 但是在这里我们主要用这个模块来测试模拟真实环境中调用功能的实现。

而在xf-common这个大模块下,我们又分为了三个小模块,分别实现不同的逻辑功能。

1.1 预热一波

等下我们将会详细的查看pom文件了,那么我们首先先来搞清楚,这些<xml>文件的作用,好, 这里我会选用一个来进行分析!

<dependency> 
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId> 
    <version>${hutool.version}</version> 
</dependency>

名称

作用

<groupId>

<artifactId></artifactId>

进行一个分组,其实在windows系统中,就相当于两层目录下

其实也不难理解,只是调用了这个包下面的hutool-all的jar包罢了

<version>

指定工具的版本

1.2 正式开始

🆗,先让我们来看这个大模块的(xf-common)的pom文件吧,在这里我会把一些不属于这四个模块的包进行排除,方便我们进行查阅。

  PS:关于<dependencyManagement>的作用

  1. 版本统一管理:在 <dependencyManagement> 中声明的依赖项并不会被实际引入到项目中,但它会锁定这些依赖的版本号。这意味着,当子模块或者其他模块通过 <dependencies> 引入相同的依赖时,它们将自动继承 <dependencyManagement> 中声明的版本号,除非它们明确指定了另一个版本号。这种方式有助于确保整个项目中依赖的一致性,避免版本冲突。

  2. 模块化项目中的依赖共享:在大型模块化项目中,<dependencyManagement> 使得依赖管理更加集中和方便。你可以在父 POM 文件中声明所有共享的依赖项及其版本号,然后在子模块中简单地通过 <dependencies> 引入所需的依赖项,而无需指定版本号。这大大简化了依赖管理过程,并减少了由于版本不一致导致的潜在问题。

  3. 插件管理:除了管理项目依赖外,<dependencyManagement> 还可以管理构建插件(通过 <pluginManagement>)的版本。这同样有助于确保整个项目中使用的插件版本是一致的,从而提高构建的可重复性和可靠性。

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xf</groupId>
    <artifactId>xf-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xf-common</name>
    <description>xf-common</description>
    <packaging>pom</packaging>

    <modules>
        <module>xf-common-core</module>
        <module>xf-common-file</module>
        <module>xf-common-redis</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.7.6</spring-boot.version>
        <common-core.version>0.0.1-SNAPSHOT</common-core.version>
        <common-file.version>0.0.1-SNAPSHOT</common-file.version>
        <common-redis.version>0.0.1-SNAPSHOT</common-redis.version>
        <lombok.version>1.18.34</lombok.version>
        <hutool.version>5.8.25</hutool.version>
        <log4j.version>2.23.1</log4j.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
    </dependencies>
    
    <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>

            <dependency>
                <groupId>com.xf</groupId>
                <artifactId>xf-common-core</artifactId>
                <version>${common-core.version}</version>
            </dependency>

            <dependency>
                <groupId>com.xf</groupId>
                <artifactId>xf-common-file</artifactId>
                <version>${common-file.version}</version>
            </dependency>

            <dependency>
                <groupId>com.xf</groupId>
                <artifactId>xf-common-redis</artifactId>
                <version>${common-redis.version}</version>
            </dependency>

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            
        </dependencies>
    </dependencyManagement>

    <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>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.xf.common.XfCommonApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

然后是xf-common-core的pom文件, 另外两个文件也是大差不差,这里就不过多展示了

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xf</groupId>
        <artifactId>xf-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>xf-common-core</artifactId>
    <name>xf-common-core</name>
    <description>xf-common-core</description>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
    </dependencies>
</project>

好,那我们再来看看,xf-file-manager文件下写了什么

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xf</groupId>
    <artifactId>xf-file-manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xf-file-manager</name>
    <description>xf-file-manager</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.7.6</spring-boot.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.xf</groupId>
            <artifactId>xf-common-file</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
    <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>

    <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>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.xf.manager.XfFileManagerApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
  1. 结算界面

好,时间来到了下午,先来说说今天用了几个小时来弄的配置,就是说第一次接受导入自己的私有包来使用,还挺新颖的,刚好来总结一下。

  1. 我们看到三个模块,虽然说下面的包的内容是不一样的,但是都是同样的名字,这样做的好处就是,我们在pom文件中只需要导入一个模块即可,我们不需要详细的写出是导哪个具体的模块,而是同一类的随便导入其中一个,那么我们使用到其他与这个有关联的相关内容了。

  1. 私有外部的包不能正确被springboot进行接管和实现自动装配的功能,搞了很久,发现原来在启动类加上一个配置即可导入。不然导入的私自外部包不能识别使用了。

@SpringBootApplication(scanBasePackages = "com.*")

  1. 导入外部包,如果不进行日志文件的设置,就会有很多乱七八糟的东西显示出来。like - this!

这个时候我们就需要,在yml文件中进行如下的设置即可。

logging:
  level:
    org:
      springframework:
        boot:
          autoconfigure: info

补充:当我们进行环境的更换的时候,我们到了一个新的maven仓库中,还没有想过的jar依赖,这个时候我们需要先clear后install 在需要被导入的模块中进行如下操作。


原文地址:https://blog.csdn.net/qq_39833005/article/details/142899564

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!