一般web项目会进行分模块开发。这里简单分为domain(领域层)、persist(持久层)、service(业务层)、web(交互控制层)。
用Maven构建以上各层,结构如下:
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表示它是一个被继承的模块,修改后的内容如下:
<?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>
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文件如下:
<?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>
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
<?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>
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文件自动变成如下内容:
<?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>
2)修改simple-persist目录中的pom.xml文件
添加对simple-domain模块的依赖,修改后的内容如下:
<?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>
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文件自动变成如下内容:
<?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>
2)修改simple-service目录中的pom.xml文件
simple-service依赖simple-persist和simple-domain,但是我们只需添加simple-persist的依赖即可,引文simple-persist已经依赖了simple-domain。
修改后的内容如下:
<?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>
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文件自动变成如下内容:
<?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>
2)修改simple-web目录中的pom.xml文件
注意,web项目的打包方式是war,添加对simple-service的依赖
<?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>
6、至此创建好了这几个模块,怎么运行起来呢。
1)由于最终运行的是simple-web模块,所以我们对该模块添加jetty容易支持,方便测试运行。修改simple-web项目的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>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> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins> </build> </project>
2)命令行进入simple-parent目录,执行如下命令
mvn clean install
执行完后,在simple-web目录下多出了target目录,里面有了simple-web.war
3)命令行进入simple-web目录,执行如下命令,启动jetty
mvn jetty:run
启动jetty服务器后,访问http://localhost:8080/simple-web/
7、加入Servlet的依赖支持
修改simple-web目录的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>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> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_2.4_spec</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>simple-web</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins> </build> </project>
最后,从eclipse中导入存在的Maven项目,选中simple-parent导入,即可在eclipse中进行开发了。