如何假装成一个服务端开发 -- maven模块

例子

父模块html

<?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">

    <!-- 指定pom版本,全部模块的pom均需指定 -->
    <modelVersion>4.0.0</modelVersion>

    <!-- 本模块的坐标 -->
    <groupId>com.zzxzzg.guardz</groupId>
    <artifactId>pom-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 打包类型,默认为jar,含有子模块的模块自动被设置为pom -->
    <packaging>pom</packaging>

    <!-- 被聚合的子模块索引 -->
    <modules>
        <module>study-common</module>
        <module>study-plugin</module>
        <module>study-blog</module>
        <module>study-web</module>
    </modules>

    <!-- 更多模块信息,填写模块的描述还有做者信息什么的 -->
    <name>ssm学习项目</name>
    <description>模块描述</description>
    <url>https://my.oschina.net/mzdbxqh/</url>
    <developers>
        <developer>
            <name>mzdbxqh</name>
        </developer>
    </developers>

    <!-- 这里集中定义整个项目里边全部第三方依赖的版本及其余可做用于全部pom的常量 -->
    <properties>
    </properties>

    <!-- 这里集中陈列整个项目须要用到的第三方依赖及其版本 -->
    <dependencyManagement>
        <dependencies>
        </dependencies>
    </dependencyManagement>

    <!-- 模块的实际依赖 -->
    <dependencies>
    </dependencies>


    <!-- 这里配置与build过程相关的内容 -->
    <build>
        <defaultGoal></defaultGoal>
        <directory></directory>
        <finalName></finalName>

        <!-- 这里定义build过程当中将引入的资源文件 -->
        <!-- 这里引入的键值对能够用于替换resources里边指定的资源文件中的${}占位符 -->
        <filters>
        </filters>

        <!-- 这里定义build过程当中将输出的资源文件,包括源地址/目标地址/需输出资源文件/不输出的资源文件,等 -->
        <resources>
        </resources>

        <!-- 这里定义build过程所需使用的插件 -->
        <plugins>
        </plugins>
    </build>

    <!-- 配置信息集-例如能够给开发环境/测试环境/生产环境预约义三套不一样的配置 -->
    <profiles>
    </profiles>

</project>

dependencyManagement

前言

咱们知道在写一个大型项目的时候会出现不少module,而且,每一个module都会有本身的依赖。java

当依赖多起来以后就会出现依赖冲突,好比moduleA和moduleB都依赖了同一个三方库,可是却使用了不一样的版本,这就致使最后编译的时候出现了依赖冲突。web

为了解决依赖冲突,其中的一个措施就是使用dependencyManagement(即使使用了,也没法彻底解决依赖冲突,好比依赖了两个三方包,两个三方包又同时依赖了同一个三方包,而且版本不一样,这时候也会出现版本冲突。)apache

正文

好比咱们抽出一个itoo-base-parent 基础module来管理子项目的公共的依赖。api

在咱们项目顶层的POM文件中,咱们会看到dependencyManagement元素。经过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,而后它就会使用在这个dependencyManagement元素中指定的版本号。eclipse

itoo-base-parent/pom.xmlmaven

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.jpa</artifactId>
                <version>${org.eclipse.persistence.jpa.version}</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>${javaee-api.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

itoo-base/pom.xmlide

<!--依赖关系-->
        <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

区别

dependencies即便在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(所有继承)学习

dependencyManagement里只是声明依赖,并不实现引入,所以子项目须要显示的声明须要用的依赖。若是不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,而且没有指定具体版本,才会从父项目中继承该项,而且version和scope都读取自父pom;另外若是子项目中指定了版本号,那么会使用子项目中指定的jar版本。测试

parent

前言

使用parent控制版本依赖是很常见的事情。相似于java中的父子类继承关系,子类能够引用父类中非private的变量和方法,Maven中的parent定义是相似的,继承者能够直接使用parent中的maven depandencies。

子模块

<?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>pom-test</groupId>
        <artifactId>com.zzxzzg.guardz</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!-- ### groupId,version直接继承自Parent,packaging默认为jar ### -->
    <!-- <groupId>pom-test</groupId> -->
    <artifactId>pom-common</artifactId>
    <!-- <version>1.0-SNAPSHOT</version> -->
    <!-- <packaging>jar</packaging> -->

    <dependencies>
    </dependencies>
</project>

引用

https://www.cnblogs.com/feibazhf/p/7886617.html https://my.oschina.net/mzdbxqh/blog/846018

相关文章
相关标签/搜索