maven分模块,聚合 在实际项目里面的应用

  • Post author:
  • Post category:其他





首先新建一个maven项目















择 quickstart version1.1 这个maven骨架


















命名

工程名字为 moduletest











修改



moduletest下的pom.xml中的<packaging>jar</packaging>为pom














<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.pcx</groupId>
  <artifactId>moduletest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>moduletest</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>






新建



一个maven module模块依赖于我们的moduletest工程












创建一个模块名字为:  moduletest-service













老样子

还是使用quickstart骨架















创建成功后的模块划分变成了这样











看在看下moduletest项目下包含了

moduletest-service项目,可见现在

moduletest-service是依赖于

moduletest









这是moduletest的pom.xml文件












<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.pcx</groupId>
  <artifactId>moduletest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>moduletest</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <modules>
  	<module>moduletest-service</module>
  </modules>
</project>








这是moduletest-service的pom.xml文件












<?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>com.pcx</groupId>
    <artifactId>moduletest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.pcx</groupId>
  <artifactId>moduletest-service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>moduletest-service</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
















根据这个思路我们依次添加moduletest-facade,

moduletest-biz,

moduletest-utils,

moduletest-integration,

moduletest-dal模块

















现在我们的项目变成了这种结构






















































最后我们来添加

moduletest-web模块



















老样子新建一个module




























































maven骨架

这里我们选择webapp 1.0这个因为是web工程




























































在moduletest-web/src/main下面新建一个java文件夹










































run configuration中新建一个maven 命令组合






























































moduletest-web的pom.xml文件中添加jetty插件



























<?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>com.pcx</groupId>
    <artifactId>moduletest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.pcx</groupId>
  <artifactId>moduletest-web</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>moduletest-web Maven Webapp</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>
  <build>
    <finalName>moduletest-web</finalName>
     <plugins>
		    <plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>8.1.14.v20131031</version>
				<configuration>
					<webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<stopPort>9999</stopPort>
					<webAppConfig>
						<contextPath>/moduletest</contextPath>
					</webAppConfig>
					<connectors>
						<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
						<port>8080</port>
						<maxIdleTime>60000</maxIdleTime>
						</connector>
					</connectors>
				</configuration>
			</plugin>
	  </plugins>
  </build>
</project>



























运行  moduletest clean install命令






























































在配置一个jetty的运行命令














































配置

完后直接运行  run
















































打开浏览器输入地址






























































最后我们来讲讲这些模块的划分意义






























































moduletest-dal:主要用来写对数据库的增删改查的



















moduletest-service:用来封装dal的把它写成服务























moduletest-biz:主要是用来对

moduletest-service的服务做进一步抽象的





























moduletest-facade:主要是用来写接口给外部系统调用的

































moduletest-integration:用来写调用别的系统的接口的





































moduletest-utils:写一些工具类,比如金额的验证,数据格式化加个小数点啊




























moduletest-web:主要用来写页面的















模块划分的好处就是结构更加清晰了,当然模块怎么划分可以按照自己的喜好,这里我只是提供一种思路。








版权声明:本文为u011325787原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。