一点一点学maven(10)——eclipse实现maven模块化开发

1、新建父项目modules-container,选择maven project,作为所有子模块的容器
一点一点学maven(10)——eclipse实现maven模块化开发_第1张图片
一点一点学maven(10)——eclipse实现maven模块化开发_第2张图片
一点一点学maven(10)——eclipse实现maven模块化开发_第3张图片
这里写图片描述

2、新建子项目modules-demo01,选择maven module,module name为子模块名,parent project选择父项目modules-container
一点一点学maven(10)——eclipse实现maven模块化开发_第4张图片
一点一点学maven(10)——eclipse实现maven模块化开发_第5张图片
一点一点学maven(10)——eclipse实现maven模块化开发_第6张图片

3、创建成功之后,父项目自动对子项目进行聚合

  <groupId>com.maven.demogroupId>
  <artifactId>modules-containerartifactId>
  <version>0.0.1-SNAPSHOTversion>
  
  <packaging>pompackaging>

  <name>modules-containername>
  <url>http://maven.apache.orgurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>

  
  <dependencyManagement>
    <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>3.8.1version>
          <scope>testscope>
        dependency>
    dependencies>
  dependencyManagement>

  
  <modules>
    <module>modules-demo01module>
    <module>modules-demo02module>
  modules>

4、子项目自动继承父项目,修改pom.xml将公共groupId和version去掉

  
  <parent>
    <groupId>com.maven.demogroupId>
    <artifactId>modules-containerartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>

  
  
  <artifactId>modules-demo01artifactId>
  <name>modules-demo01name>
  <url>http://maven.apache.orgurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      
      <scope>testscope>
    dependency>
  dependencies>

5、按照modules-demo01创建子项目modules-demo02,最终的目录结构如图:
一点一点学maven(10)——eclipse实现maven模块化开发_第7张图片


补充:创建module子项目过程,eclipse可能抛出如图异常:
一点一点学maven(10)——eclipse实现maven模块化开发_第8张图片
details信息为:
Unable to create project from archetype [org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE]
Unable to read parent POM

解决的办法:将父项目pom.xml中所有中文删除,包括注释,所有子项目创建完毕后,需要的话再加回来。




6、然后对父项目进行编译、测试、打包、安装操作,实现对所有聚合子项目的共同操作

[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Reactor Build Order:
[INFO]
[INFO] modules-container
[INFO] modules-demo01
[INFO] modules-demo02
[INFO]
[INFO] Using the builder
org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ————————————————————————
[INFO] Building modules-container 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ modules-container —
[INFO] Deleting E:\workspace_II\modules-container\target
[INFO]
[INFO] ————————————————————————
[INFO] Building modules-demo01 0.0.1-SNAPSHOT

[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ modules-demo01 —
[INFO] Deleting E:\workspace_II\modules-container\modules-demo01\target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ modules-demo01 —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workspace_II\modules-container\modules-demo01\src\main\resources
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ modules-demo01 —
[INFO] Compiling 1 source file to E:\workspace_II\modules-container\modules-demo01\target\classes
[INFO]
[INFO] ————————————————————————
[INFO] Building modules-demo02 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ modules-demo02 —
[INFO] Deleting E:\workspace_II\modules-container\modules-demo02\target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ modules-demo02 —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workspace_II\modules-container\modules-demo02\src\main\resources
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ modules-demo02 —
[INFO] Compiling 1 source file to E:\workspace_II\modules-container\modules-demo02\target\classes
[INFO] ————————————————————————
[INFO] Reactor Summary:
[INFO]
[INFO] modules-container …………………………… SUCCESS [ 0.187 s]
[INFO] modules-demo01 ……………………………… SUCCESS [ 0.786 s]
[INFO] modules-demo02 ……………………………… SUCCESS [ 0.042 s]
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 1.101 s
[INFO] Finished at: 2016-08-01T18:24:51+08:00
[INFO] Final Memory: 10M/153M
[INFO] ————————————————————————

你可能感兴趣的:(Maven)