分布式架构--第一篇--项目拆分(maven命令生成多模块项目)
预览生成的项目结构:
ying-yue-parent // 顶级总编译控制模块
ying-yue-lib // jar模块
ying-yue-model // 模型对象模块
ying-yue-dao // 持久化层模块
ying-yue-service // 业务 层 模 块
ying-yue-web // 控制 层 模 块
包名:com.zjx.lollipop
1.准备工作 安装jdk和maven
从apache官网下载maven https://www.apache.org/
解压即安装
1.1.配置环境变量
1.2.使用mvn -v查看是否安装成功
2.使用命令生成上述结构的模块化结构项目
说明
mvn archetype:generate 固定格式
-DgroupId 组织标识(包名)
-DartifactId 项目名称
-DarchetypeArtifactId 指定ArchetypeId,maven-archetype-quickstart,创建一个Java Project;maven-archetype-webapp,创建一个Web Project
-DinteractiveMode 是否使用交互模式
2.1使用命令创建 ying-yue-parent 顶级模块
mvn archetype:generate -DgroupId=com.zjx.lollipop -DartifactId=ying-yue-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
进入生成的目录将jar打包方式更改为pom方式如下图:
下面继续使用命令生成模块, 下面的这些不需要更改打包方式默认即可, 开始:
2.2创建ying-yue-lib 模块
mvn archetype:generate -DgroupId=com.zjx.lollipop -DartifactId=ying-yue-lib -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2.3创建ying-yue-model模块
mvn archetype:generate -DgroupId=com.zjx.lollipop -DartifactId=ying-yue-model -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2.4创建ying-yue-dao模块
mvn archetype:generate -DgroupId=com.zjx.lollipop -DartifactId=ying-yue-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2.5创建ying-yue-service模块
mvn archetype:generate -DgroupId=com.zjx.lollipop -DartifactId=ying-yue-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2.6创建ying-yue-web模块
mvn archetype:generate -DgroupId=com.zjx.lollipop -DartifactId=ying-yue-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractivMode=false
这一步需要手动输入版本号默认为1.0-SNAPSHOT 我直接按回车确认(如下图时)如果想自定义版本号在后面输入然后Enter即可
是否使用com.zjx.lollipop这个包名 当然是Yes (输入Y 回车)
回车后生成web模块
执行完上图所有命令后最终生成如下图结构目录:
3.导入Eclipse或MyEclipse
使用maven方式import导入后如下图1, 图2
图1
图2
3.1打开web模块的pom.xml配置jetty插件
<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>com.zjx.lollipop</groupId> <artifactId>ying-yue-web</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ying-yue-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>ying-yue-web</finalName>
<!--配置Jetty--> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins>
<!--配置Jetty--> </build> </project>
3.2选中ying-yue-parent后右键Run As输入package命令进行打包
点击ying-yue-web右键Run As输入命令jetty:run后点击run按钮运行( 画框的可以仔细看看, 发布后默认targe目录可以将那个war拷到tomcat下运行)
3.3测试访问 localhost:8080/ying-yue-web
可以的没问题
4.我打算使用servlet+JDBC写一个带分页的用户增删改查 来体现模块间的调用与jar依赖管理, 开始:
jar的坐标不知道的可以到
中搜索;
4.1加入servlet和JDBC及其他依赖jar到ying-yue-lib的pom.xml中
需要用到的jar列表
commons-beanutils-x.x.jar
commons-logging-x.x.jar
commons-collections-x.x.jar
jstl.jar
mysql-connector-java-x.x.jar
standard.jar
可以看到当引入beanutils后其他两个logging和collections自动引入这就是maven的好处
lib模块配置后的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-lib</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>ying-yue-lib</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.2</version> </dependency>
4.2模块间引用lib及模块依赖调用
首先是dao引入lib和model
dao模块配置后的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-dao</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>ying-yue-dao</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-lib</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
service模块引入dao模块
service模块配置后的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-service</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>ying-yue-service</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
web模块引入service模块
web模块配置后的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-web</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ying-yue-web Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.zjx.lollipop</groupId> <artifactId>ying-yue-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ying-yue-web</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins> </build> </project>
待续......