一般web项目会进行分模块开发。这里简单分为domain(领域层)、persist(持久层)、service(业务层)、web(交互控制层)。

  用Maven构建以上各层,结构如下:

                                                  

maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache

  

1、创建simple-parent,用来给各个子模块继承。

  1)进入命令行,输入以下命令:


mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


  可以看到在当前目录生成了simple-parent目录,里面有一个src目录和一个pom.xml文件。

  将src文件夹删除。

  2)修改pom.xml文件


将<packaging>jar</packaging>修改为<packaging>pom</packaging>


  pom表示它是一个被继承的模块,修改后的内容如下:


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.luxh</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-parent</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


 

2、创建simple-domain模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:


mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


  可以看到在simple-parent目录中生成了simple-domain,里面包含src目录和pom.xml文件。

  同时,在simple-parent目录中的pom文件自动添加了如下内容:


<modules>
    <module>simple-domain</module>
  </modules>


  这时,simple-parent的pom.xml文件如下:


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.luxh</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-parent</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <modules>
    <module>simple-domain</module>
  </modules>
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


   2)修改simple-domain目录中的pom.xml文件


把<groupId>cn.luxh</groupId>和<version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>


  因为groupId和version会继承simple-parent中的groupId和version,packaging设置打包方式为jar


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.luxh</groupId>
    <artifactId>simple-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>simple-domain</artifactId>
  <packaging>jar</packaging>
  <name>simple-domain</name>
  <url>http://maven.apache.org</url>
 
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


 

3、创建simple-persist模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:


mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-persist -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


  可以看到在simple-parent目录中生成了simple-persist,里面包含src目录和pom.xml文件。

  同时,在simple-parent目录中的pom文件自动变成如下内容:


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.luxh</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-parent</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <modules>
    <module>simple-domain</module>
    <module>simple-persist</module>
  </modules>
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


  2)修改simple-persist目录中的pom.xml文件

  添加对simple-domain模块的依赖,修改后的内容如下:


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.luxh</groupId>
    <artifactId>simple-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>simple-persist</artifactId>
  <packaging>jar</packaging>
  <name>simple-persist</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>cn.luxh</groupId>
      <artifactId>simple-domain</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


 

4、创建simple-service模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:


mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


  可以看到在simple-parent目录中生成了simple-service,里面包含src目录和pom.xml文件。

  同时,在simple-parent目录中的pom文件自动变成如下内容:


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.luxh</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-parent</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <modules>
    <module>simple-domain</module>
    <module>simple-persist</module>
    <module>simple-service</module>
  </modules>
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


  2)修改simple-service目录中的pom.xml文件

  simple-service依赖simple-persist和simple-domain,但是我们只需添加simple-persist的依赖即可,引文simple-persist已经依赖了simple-domain。

  修改后的内容如下:


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.luxh</groupId>
    <artifactId>simple-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>simple-service</artifactId>
 <packaging>jar</packaging>
<name>simple-service</name>
  <url>http://maven.apache.org</url>
  <dependencies>
     <dependency>
      <groupId>cn.luxh</groupId>
      <artifactId>simple-persist</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


 

5、创建simple-web模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:


mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


  可以看到在simple-parent目录中生成了simple-web,里面包含src目录和pom.xml文件,在\simple-web\src\main\webapp目录中还生成了一个简单的index.jsp,将里面的内容改为

  Hey,Maven!

  simple-web\src\main\webapp\WEB-INF目录中生成了web.xml

  同时,在simple-parent目录中的pom文件自动变成如下内容:


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.luxh</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-parent</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <modules>
    <module>simple-domain</module>
    <module>simple-persist</module>
    <module>simple-service</module>
    <module>simple-web</module>
  </modules>
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


  2)修改simple-web目录中的pom.xml文件

     注意,web项目的打包方式是war,添加对simple-service的依赖


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.luxh</groupId>
    <artifactId>simple-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>simple-web</artifactId>
  <packaging>war</packaging>
  <name>simple-web Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>cn.luxh</groupId>
      <artifactId>simple-service</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>simple-web</finalName>
  </build>
</project>


maven在父级的pom中pproperties在子级中可以使用吗 maven多层parent_apache_02


 6、导入eclipse