一般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中進行開發了。