2

使用 Intellij IDEA 创建 maven 多模块项目

 1 year ago
source link: https://blog.diqigan.cn/posts/maven-multi-module-project-creating-with-idea.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

之前接触过的项目大都是单体应用,各应用之间有功能重合但无代码共用。同样的功能,要么重复开发,要么代码拷贝,开发繁琐且效率低下。一但在多个应用中均有使用的相同的代码出现了 bug,开发人员更是要在多个应用中逐个修复,劳神伤身。

故引入模块化开发概念,把相对独立的功能抽离成组件开发,完成之后再组合到一起服务业务。一来开发有了积淀,新项目不必从零开发;二来 bug 的修复也会简单很多,只需修复一次,升级版本即可。提高了工作效率,才会有更多的时间去做技术研究和价值创造。

简单查询了使用 Intellij IDEA 进行 maven 多模块项目的创建过程,只是模块化开发中很小的一部分,记录于此,用以备忘。

创建父模块

  1. 在 IDEA 中选择 “new project”:

  2. 直接点击 “Next”:

  3. 填写项目信息,然后点击 “Finish”:

  4. 修改 pom.xml 文件:

    • project 标签下添加 packaging 标签,取值为 pom;

    • project 标签下添加 version 标签,取值为 ${revision};

    • properties 标签下添加 revision 标签,取值为项目版本号。

    <?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.seven.module</groupId>
    <artifactId>common</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>

    <properties>
    <revision>1.0-SNAPSHOT</revision>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
    <plugins>
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>flatten-maven-plugin</artifactId>
    <version>${flatten-maven-plugin.version}</version>
    <configuration>
    <updatePomFile>true</updatePomFile>
    <flattenMode>resolveCiFriendliesOnly</flattenMode>
    </configuration>
    <executions>
    <execution>
    <id>flatten</id>
    <phase>process-resources</phase>
    <goals>
    <goal>flatten</goal>
    </goals>
    </execution>
    <execution>
    <id>flatten.clean</id>
    <phase>clean</phase>
    <goals>
    <goal>clean</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>
    </project>
  5. 删除父模块的 “src” 目录。

至此,父模块创建完成。

创建子模块

  1. 鼠标右键项目名,然后依次选择 ”New” -> “Module”:

  2. 点击 “Next” 去往下一步:

  3. 填写模块信息并点击 “Finish”:

  4. 修改 pom.xml 文件:

    为了保持父子模块版本号一致,我们需要修改如下配置:

    • project 标签下的 parent 标签下的 version 取值修改为 ${version};
    • project 标签下添加 version 标签,取值为 ${revision};
    <?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>
    <artifactId>common</artifactId>
    <groupId>com.seven.module</groupId>
    <version>${revision}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>util</artifactId>
    <version>${revision}</version>

    </project>

至此,子模块创建完成,直接开始模块开发即可。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK