Maven继承

Maven 在设计时,借鉴了 Java 面向对象中的继承思想,提出了 POM 继承思想。

当一个项目包含多个模块时,可以在该项目中再创建一个父模块,并在其 POM 中声明依赖,其他模块的 POM 可通过继承父模块的 POM 来获得对相关依赖的声明。对于父模块而言,其目的是为了消除子模块 POM 中的重复配置,其中不包含有任何实际代码,因此父模块 POM 的打包类型(packaging)必须是 pom。

如下图,一个项目中存在如下多个模块。
 

如上图所示:

  • App-UI-WAR 依赖于 App-Core-lib 和 App-Data-lib。
  • Root 是 App-Core-lib 和 App-Data-lib 的父模块。
  • Root 在它的依赖部分定义了 junit 4.9、mysql-connector-java 5.1.18 以及 c3p0 0.9.1 作为其依赖。

 App-UI-WAR 的 pom.xml 配置如下

<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>net.biancheng.www</groupId>
    <artifactId>App-UI-WAR</artifactId>
    <version>1.0</version>
    <dependencies>
        <!-- 依赖 App-Core-lib-->
        <dependency>
            <groupId>net.biancheng.www</groupId>
            <artifactId>App-Core-lib</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- 依赖 App-Data-lib-->
        <dependency>
            <groupId>net.biancheng.www</groupId>
            <artifactId>App-Data-lib</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

父模块 Root 的 pom.xml 配置如下。 

<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>net.biancheng.www</groupId>
    <artifactId>Root</artifactId>
    <version>1.0</version>
    <!--定义的父类 POM 打包类型使pom  -->
    <packaging>pom</packaging>
   
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.18</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1</version>
        </dependency>
    </dependencies>
</project>

在父模块 Root 的 pom.xml 中,其打包类型(packaging)为 pom,并声明了 3 个依赖:junit 4.9、mysql-connector-java 5.1.18 以及 c3p0 0.9.1 。

子模块App-Core-lib 的 pom.xml 配置如下:

<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>net.biancheng.www</groupId>
    <artifactId>App-Core-lib</artifactId>
    <version>1.0</version>
    <parent>
        <groupId>net.biancheng.www</groupId>
        <artifactId>Root</artifactId>
        <version>1.0</version>
        <relativePath>../Root</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
</project>

子模块App-Data-lib 的 pom.xml 配置如下:

<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>net.biancheng.www</groupId>
    <artifactId>App-Data-lib</artifactId>
    <version>1.0</version>
    <parent>
        <groupId>net.biancheng.www</groupId>
        <artifactId>Root</artifactId>
        <version>1.0</version>
        <!-- <relativePath>../Root</relativePath> -->
    </parent>
</project>

在子模块 App-Core-lib 和 App-Data-lib 的 pom.xml 中,使用 parent 元素声明父模块,其子元素如下表:

元素描述是否必需
groupId父模块的项目组 id。
artifactId父模块 id。
version父模块版本。
relativePath父模块 POM 的相对路径,默认值为 ../pom.xml。
项目构建时,Maven 会先根据 relativePath 查找父模块 POM,如果找不到,再从本地仓库或远程仓库中查找。

子模块的 POM 中,当前模块的 groupId 和 version 元素可以省略,但这并不意味着当前模块没有 groupId 和 version,子模块会隐式的从父模块中继承这两个元素,即由父模块控制子模块的公司组织 id 以及版本,这样可以简化 POM 的配置。 

可继承的 POM 元素

在上面的例子中,我们可以看出 groupId、version 以及项目的依赖配置 dependencies 是可以被继承的,除了这 3 个元素之外,还有哪些元素可以被继承呢?

Maven 可通过继承获得 POM 元素,如下表。
 

元素描述
groupId项目组 ID,项目坐标的核心元素
version项目版本,项目坐标的核心元素
description项目的描述信息
organization项目的组织信息
inceptionYear项目的创始年份
url项目的URL地址
developers项目的开发者信息
contributors项目的贡献者信息
distributionManagement项目的部署配置
issueManagement项目的缺陷跟踪系统信息
ciManagement项目的持续集成系统信息
scm项目的版本控制系统信息
mailingLists项目的邮件列表信息
properties自定义的Maven属性
dependencies项目的依赖配置
dependencyManagement项目的依赖管理配置
repositories项目的仓库配置
build包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等
reporting包括项目的报告输出目录配置、报告插件配置等
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来为你介绍一下在 IDEA 最新版下如何使用 Maven 继承 Jetty。 首先,我们需要在父工程的 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.31.v20200723</version> </dependency> ``` 这个依赖会将 Jetty Maven 插件添加到项目中。 然后,在父工程的 pom.xml 文件中添加以下插件: ``` <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.31.v20200723</version> <configuration> <webApp> <contextPath>/</contextPath> </webApp> <stopKey>stop</stopKey> <stopPort>8081</stopPort> <httpConnector> <port>8080</port> <idleTimeout>60000</idleTimeout> </httpConnector> </configuration> </plugin> </plugins> </build> ``` 这个插件会配置 Jetty 服务器的端口号为 8080,并且指定了停止服务器的端口号为 8081。 接下来,在子工程的 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0-SNAPSHOT</version> <type>pom</type> </dependency> ``` 这个依赖会将父工程中的 Jetty Maven 插件引入到子工程中。 最后,在子工程的 pom.xml 文件中执行以下命令启动 Jetty 服务器: ``` mvn jetty:run ``` 这个命令会启动 Jetty 服务器并且在浏览器中打开项目的首页。 以上就是在 IDEA 最新版下使用 Maven 继承 Jetty 的详细步骤。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值