zoukankan      html  css  js  c++  java
  • 关于maven多module的依赖问题

    之前的项目因为历史的原因,都是一个project里只包含了一个module,今年进入了新的项目组,出现了多个module,最近刚好也是在学《maven实战》因此想要将这个东西记录下来

    工程情况如下图

    示例的工程名为inaction

    其中位于根目录的pom.xml内的代码如下

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>com.example</groupId>
     8     <artifactId>inaction</artifactId>
     9     <packaging>pom</packaging>
    10     <version>1.0-SNAPSHOT</version>
    11 
    12     <modules>
    13         <module>inactionuser</module>
    14         <module>inactioncore</module>
    15         <module>inactionparent</module>
    16     </modules>
    17 
    18 </project>

    因为是多module,所以必然是有自己编写的类或接口,是可以放在公共的module里的这里我把它命名为 inaction-core

    然后有一个inaction-user表示用户模块的

    一般的,如果我们需要将本地一些代码抽取到公共模块,那么还需要一个module来进行管理,在我的项目中,这个为 inaction-parent

    记得要设置<realtivePath>../pom.xml</realtivePath>

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5 
     6     <modelVersion>4.0.0</modelVersion>
     7 
     8     <groupId>com.example</groupId>
     9     <artifactId>inaction-parent</artifactId>
    10     <version>1.0-SNAPSHOT</version>
    11 
    12     <!-- dependmanager不会向子类真的导入依赖,只是起到一个约束的作用 -->
    13     <dependencyManagement>
    14         <dependencies>
    15             <!-- 这里声明本地公共的module -->
    16             <dependency>
    17                 <groupId>com.example</groupId>
    18                 <artifactId>inaction-core</artifactId>
    19                 <version>1.0-SNAPSHOT</version>
    20             </dependency>
    21         </dependencies>
    22     </dependencyManagement>
    23 
    24 </project>

    再看core和user 两个module

    (ps 在paren头结点下,记得补充 <relativePath>../inactionparent/pom.xml</relativePath>)

    <?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">
        <parent>
            <groupId>com.example</groupId>
            <artifactId>inaction-parent</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>inaction-user</artifactId>
        <packaging>jar</packaging>
    
    
        <dependencies>
            <!--注意到这里我们已经不需要声明版本了,因为在parent那边已经对版本进行了管理-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>inaction-core</artifactId>
            </dependency>
        </dependencies>
    </project>

    core模块就很简单,只需要声明其父module即可

    <?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">
        <parent>
            <groupId>com.example</groupId>
            <artifactId>inaction-parent</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <packaging>jar</packaging>
    
        <artifactId>inaction-core</artifactId>
    
    
    </project>

    然后,在user模块中去引用core的类就不会有编译问题了

    如果多module没有引用本地的module,那么可以不需要parent,直接声明到根目录下

    注意,你的root,parent和公共的module,一定不要有 maven plugin,尤其是公共的module,会导致在 mvn clean install 的时候,报找不到类的错误

  • 相关阅读:
    【css】border-image
    函数的调用
    函数的返回值
    定义函数的三种形式
    文件处理实战之购物车系统
    文件处理小结
    文件修改的两种方式
    with管理文件操作上下文
    绝对路径和相对路径
    基本的文件操作
  • 原文地址:https://www.cnblogs.com/westlin/p/10908778.html
Copyright © 2011-2022 走看看