美文网首页
Maven构建多模块项目

Maven构建多模块项目

作者: 秋枫叨叨 | 来源:发表于2022-04-06 10:50 被阅读0次

1.参考 netty-in-action

2.菜鸟教程maven手册 :https://www.runoob.com/maven/maven-tutorial.html

通过分析netty-in-action书籍的示例代码,发现打开了新大陆,发现原来maven构建项目还能通过pom继承。原谅我之前没有仔细的去了解过Maven的具体使用,平常在项目中也只是会添加依赖标签,所以在这里发现一个新奇的项目结构就迫不及待的记录下来。在这里你也需要问一下自己,你真的了解Maven吗?(不介绍Maven的安装和基本概念)

一、项目结构

1)Maven约定目录结构

\${project.basedir}
    |--\src
        |--\main
            |--\java
            |--\resources
            |--\webapp
        |-- test
            |--\java
            |--\resources
    |-- pom.xml

2)单个模块项目结构

\Client
    |--\src
        |--\main
            |--\java
            |--\resources
            |--\webapp
        |-- test
            |--\java
            |--\resources
    |--\pom.xml

这种结构在日常使用上是最多的,基本上没开发一个功能模块新建一个项目。

3)多个模块项目结构

\netty-in-action
    |--\.idea
    |--\chapter1
    |--\chapter2
        |--\Client
            |--\src
            |--\pom.xml
        |--\Server
            |--\src
            |--\pom.xml
    |--\pom.xml

之前都是同一个项目的不同模块功能各建一个工程,虽然感觉到哪里有点不对,但在使用过程也不会出现问题,显然要这才是项目结构的正解。

二、POM

1)POM示例

我们来看一看netty-in-action项目下的POM文件:

<?xml version="1.0" encoding="ISO-8859-15"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>nia</groupId>
    <artifactId>nia-samples-parent</artifactId>
    <version>2.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Sample Code for Netty in Action</name>
    <modules>
        <module>chapter1</module>
        <module>chapter2</module>
    </modules>
    <properties>
        <junit.version>4.11</junit.version>
        <netty.version>4.1.12.Final</netty.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>${netty.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

2)标签解析

1.GAV坐标

是不是没有听过这个概念?GAV是三个坐标元素的首字母缩写,groupId、artifactId、version。GA可唯一的标识POM构件,V标识构件版本

2.packaging

打包类型,默认的值为jar。常用的其他类型为pom、ear、war以及maven-plugin

3.modules

我们称包含modules的构件为聚合器,聚合器项目文件下可包含子项目,子项目包含自己的POM文件

4.properties

POM文件下面定义的常量,可被子模块的POM文件引用

5.dependencyManagement

这个标签可理解成依赖管理者,专门管理依赖的版本,若子项目中引用了dependencyManagement里面的依赖,如果没有指定版本则下载dependencyManagement里面的版本,如果指定了版本则下载指定版本的依赖。

6.pluginManagement

插件管理者,管理插件版本,功能同dependencyManagement

三、POM的继承

1)POM类型继承示例

<?xml version="1.0" encoding="ISO-8859-15"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>nia</groupId>
        <artifactId>nia-samples-parent</artifactId>
        <version>2.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>chapter2</artifactId>
    <packaging>pom</packaging>
    <name>Chapter 2. Your First Netty Application - Echo App</name>
    <description>
        Build an Echo Server and Client
    </description>

    <modules>
        <module>Client</module>
        <module>Server</module>
    </modules>
</project>

2)JAR类型继承示例

<?xml version="1.0" encoding="ISO-8859-15"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>nia</groupId>
        <artifactId>chapter2</artifactId>
        <version>2.0-SNAPSHOT</version>
    </parent>

    <artifactId>echo-client</artifactId>
    <name>Chapter 2. Echo Client</name>
</project>

我们可以明显的看得出上面两个不同类型之间的POM文件内容还是有点区别的,POM类型的文件类容指定了packaging的类型,以及子模块modules。

相关文章

网友评论

      本文标题:Maven构建多模块项目

      本文链接:https://www.haomeiwen.com/subject/opjrsrtx.html