0
点赞
收藏
分享

微信扫一扫

java pom两个模块需要互相引用怎么办

在Maven项目中,如果两个模块(通常称为子模块)需要互相引用,这通常意味着你的项目结构可能需要重新考虑或调整。Maven默认不支持直接的循环依赖,因为这会导致构建过程中的死锁。但是,有几种方法可以处理这种情况或避免它:

1. 重新设计项目结构

首先,考虑是否有可能重新设计项目结构来避免循环依赖。这可能涉及到将共享的代码或功能提取到一个新的、单独的模块中,然后让原先的两个模块都依赖于这个新的模块。

2. 使用Maven的provided作用域

如果两个模块之间的依赖是编译时的,但运行时不会直接依赖于对方(例如,它们可能都依赖于一个共同的库,但只是在编译时需要对方的接口),你可以考虑将依赖设置为provided作用域。然而,这通常不适用于直接的模块间依赖,因为它假设运行时环境将提供所需的依赖。

java pom两个模块需要互相引用怎么办_spring

3. 聚合与继承

  • 聚合(Aggregation):使用<modules>标签在父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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--定义该工程用于进行构建管理-->
    <packaging>pom</packaging>
    <!--定义工程管理那些pom-->
    <!--管理的工程列表-->
    <modules>
    <!--具体的工程名称-->
        <module>../ssm_controller</module>
        <module>../ssm_dao</module>
        <module>../ssm_service</module>
        <module>../ssm_pojo</module>
    </modules>

    <!--声明此处进行依赖管理-->
    <dependencyManagement>

    <!--具体的依赖-->
        <dependencies>
            <!--添加自己的工程模块的依赖-->
           <!--spring环境-->
            <!--spring环境-->
            <!--spring环境-->

            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_pojo</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>

            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_service</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>

            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_dao</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>


            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>


            <!--mybatis环境-->
            <!--mybatis环境-->
            <!--mybatis环境-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.3</version>
            </dependency>
            <!--mysql环境-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <!--spring整合jdbc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <!--spring整合mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.3</version>
            </dependency>
            <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.16</version>
            </dependency>
            <!--分页插件坐标-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
            </dependency>


            <!--springmvc环境-->
            <!--springmvc环境-->
            <!--springmvc环境-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <!--jackson相关坐标3个-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.0</version>
            </dependency>
            <!--servlet环境-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>


            <!--其他组件-->
            <!--其他组件-->
            <!--其他组件-->
            <!--junit单元测试-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <!--spring整合junit-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <pluginManagement>

        <!--设置插件-->
        <plugins>
                <!--具体的插件配置-->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <port>80</port>
                        <path>/</path>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>7</source>
                        <target>7</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

  • 继承(Inheritance):一个模块可以通过<parent>标签继承另一个模块的POM设置。但是,这同样不解决直接的循环依赖问题。

<!--声明此处进行依赖管理-->
<dependencyManagement>
    <!--具体的依赖-->
    <dependencies>
        <!--spring环境-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    <dependencies>
<dependencyManagement>

### **3.4)继承依赖使用**

在子工程中定义依赖关系,无需声明依赖版本,版本参照父工程中依赖的版本

```xml
<dependencies>
    <!--spring环境-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
</dependencies>

4. 分离公共接口

将两个模块共同需要的接口、类、工具类等提取到一个单独的模块中,然后让两个模块都依赖于这个公共模块。这是解决循环依赖问题的最常见和最推荐的方法。

5. 使用Maven的dependency:unpack-dependencies插件

虽然不推荐,但理论上你可以使用Maven的dependency:unpack-dependencies插件来将一个模块的编译产物(如jar)作为另一个模块的源文件或资源来处理。然而,这种方法既复杂又容易出错,且违背了Maven的依赖管理原则。

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-dependency-plugin</artifactId>  
            <version>2.8</version>  
        </plugin>  
    </plugins>  
</build>  

结论

通常情况下,最佳做法是重新设计你的项目结构,将共享的代码或功能提取到单独的模块中,并避免直接的循环依赖。这样不仅可以解决构建问题,还可以提高代码的可维护性和可重用性。如果必须保持当前的模块结构,那么可能需要仔细考虑是否真的需要这种循环依赖,或者是否有其他方法可以实现相同的功能。

举报

相关推荐

0 条评论