maven3创建多模块web项目

实现步骤

创建项目目录 
进入“工作空间”目录,创建名为treasure的文件夹,切换至控制台,进入该文件夹。

配置模块

生成各个模块 

maven-archetype-quickstart

默认的Archetype,基本内容包括:

  • 一个包含junit依赖声明的pom.xml
  • src/main/java主代码目录及一个名为App的类
  • src/test/java测试代码目录及一个名为AppTest的测试用例

maven-archetype-webapp

一个最简单的Maven war项目模板,当需要快速创建一个Web应用的时候可以使用它。生成的项目内容包括:

  • 一个packaging为war且带有junit依赖声明的pom.xml
  • src/main/webapp/目录
  • src/main/webapp/index.jsp文件
  • src/main/webapp/WEB-INF/web.xml文件


该步骤会依次生成项目的各个模块,但是生成的模块并没有 创建依赖,只是最简单的并且符合maven 要求的项目结构的模块,关于什么是 maven 的标准项目结构,可以参考 maven 官方文档,或者《 maven 权威指南》。

# parent 模块创建

创建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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<groupId>com.mirror</groupId>
<artifactId>mirror-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>mirror::parent</name>
<packaging>pom</packaging> <properties>
<!-- 主要依赖库的版本定义 -->
<mirror.version>1.0.0-SNAPSHOT</mirror.version>
<spring.version>4.2.1.RELEASE</spring.version>
<mybatis.version>3.3.0</mybatis.version> <junit.version>4.12</junit.version>
<!-- Plugin的属性 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties> <prerequisites>
<maven>3.0.0</maven>
</prerequisites> <!-- 设定除*仓库 (repo1.maven.org/maven2/)外的其他仓库,按设定顺序进行查询,如有Nexus私服, 取消注释并指向正确的服务器地址
<repositories>
<repository>
<id>nexus</id>
<name>Team Nexus Repository</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
</repository>
</repositories>--> <!-- 如有Nexus私服, 取消注释并指向正确的服务器地址
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Team Nexus Repository</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories> --> <!-- 预定义依赖项的version,scope与exclusions,子项目中只需定义groupId 与 artifactId 即可 -->
<dependencyManagement>
<dependencies> <!-- spring-->
<!-- 纯粹为了exclude commons-loging -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> <!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

#client 模块创建 ,修改pom文件

mvn archetype:generate -DgroupId=com.mirror -DartifactId=mirror-client -DarchetypeCatalog=internal -Dversion=1.0.0-SNAPSHOT

<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> <parent>
<groupId>com.mirror</groupId>
<artifactId>mirror-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent/</relativePath>
</parent>
<artifactId>mirror-client</artifactId>
<packaging>jar</packaging> <name>mirror::mirror-client</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>
</dependency>
</dependencies>
</project>

#utils模块创建 ,修改pom文件

mvn archetype:generate -DgroupId=com.mirror -DartifactId=mirror-utils -DarchetypeCatalog=internal -Dversion=1.0.0-SNAPSHOT

<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> <parent>
<groupId>com.mirror</groupId>
<artifactId>mirror-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent/</relativePath>
</parent>
<artifactId>mirror-utils</artifactId>
<packaging>jar</packaging> <name>mirror::mirror-utils</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>
</dependency>
</dependencies>
</project>

#core 模块创建 ,修改pom文件

mvn archetype:generate -DgroupId=com.mirror -DartifactId=mirror-core -DarchetypeCatalog=internal -Dversion=1.0.0-SNAPSHOT

<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> <parent>
<groupId>com.mirror</groupId>
<artifactId>mirror-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent/</relativePath>
</parent>
<artifactId>mirror-core</artifactId>
<packaging>jar</packaging> <name>mirror::mirror-core</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>
</dependency> <dependency>
<groupId>com.mirror</groupId>
<artifactId>mirror-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies> </project>

#web-server 模块创建,修改pom和web.xml(升级版本)

mvn archetype:generate -DgroupId=com.mirror -DartifactId=mirror-web -DpackageName=com.mirror -DarchetypeArtifactId=maven-archetype-webapp -Dversion=1.0-SNAPSHOT

<?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.mirror</groupId>
<artifactId>mirror-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent/</relativePath>
</parent>
<artifactId>mirror-web</artifactId>
<packaging>war</packaging>
<name>mirror::mirror-webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
</dependencies>
<build>
<finalName>mirror-web</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.8.v20121106</version>
<configuration>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
</configuration>
</plugin> <plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/${project.build.finalName}</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <web-app>
<display-name>mirror-web</display-name>
</web-app>

配置项目模块 
在treasure根目录下新建一个 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>
<parent>
<groupId>com.mirror</groupId>
<artifactId>mirror-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>./parent/</relativePath>
</parent>
<artifactId>mirror-modules</artifactId>
<name>mirror::modules</name>
<packaging>pom</packaging> <modules>
<module>parent</module>
<module>mirror-client</module>
<module>mirror-utils</module>
<module>mirror-core</module>
<module>mirror-web</module>
</modules>
</project>

修改配置完成之后,在treasure目录下,执行:

mvn clean install
mvn eclipse:eclipse
生成 Eclipse 项目文件及包引用定义,注意,需确保定义Classpath Variables: M2_REPO,指向本地maven类库目录。

打开Eclipse ,导入工程即可。

控制端cd到mirror-web手动运行mvn -Djetty.port=80 jetty:run

或者配置对应的tomcat进行上传部署,mvn  tomcat:run

上一篇:JavaScript--详解typeof的用法


下一篇:[LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串