首页 > 学院 > 开发设计 > 正文

46、Maven创建多模块项目(多个war,2017版Eclipse Neon.2)

2019-11-06 07:07:33
字体:
来源:转载
供稿:网友

运行环境:

2017年3月 Eclipse Neon.2

Apache Maven 3.3.9

jdk1.8

注意,此文跟网上所有的创建maven项目有所不同,

就是都会勾选 create a simple PRoject,网上的都是不勾选的。下面就不额外提出了。

首先看看总目录结构:

Basic父目录:用于控制各个项目;

BasicJar:主要用于控制pom文件下载jar包用;

BasicWeb:主web项目;

BasicMainPage:模拟主Web项目中的一个主页模块;

1、首先创建Basic项目

创建MavenProject,注意packing选的是pom;

Basic项目中的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.test.basic</groupId>	<artifactId>Baisc</artifactId>	<version>0.0.1-SNAPSHOT</version>	<packaging>pom</packaging>	<!-- 用于控制子项目的版本 -->	<properties>        <project.version>0.0.1-SNAPSHOT</project.version>  	</properties>	<!-- 包含的子类项目,新建项目时,eclipse会自动加上的 -->	<modules>		<module>BasicJar</module>		<module>BasicWeb</module>		<module>BasicMainPage</module>	</modules></project>

2、创建BasicJar,用于控制引入的jar包;

创建Maven Module,而不是Maven Project;

注意ParentProject选择的是我们刚刚选择Basic,packing注意选的是jar;

BasicJar的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>	<parent>		<groupId>com.test.basic</groupId>		<artifactId>Baisc</artifactId>		<version>0.0.1-SNAPSHOT</version>	</parent>	<artifactId>BasicJar</artifactId>	<!-- 控制版本 -->	<properties>		<servlet-api.version>2.5</servlet-api.version>		<spring.version>4.3.7.RELEASE</spring.version>	</properties>	<dependencies>		<!-- 加入spring的包 -->		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-context</artifactId>			<version>${spring.version}</version>		</dependency>		<!-- 加入servlet的包 -->		<dependency>			<groupId>javax.servlet</groupId>			<artifactId>servlet-api</artifactId>			<version>${servlet-api.version}</version>		</dependency>	</dependencies></project>3、创建BasicWeb项目,web主项目:

同样,创建的是Maven Modulbe而不是Maven Project,Parent Project选的是Baisc,packing选的是war

创建完项目之后,我们右键项目- properties - project facets;

然后勾掉Dynamic Web Module - apply  ,然后再勾回来,看到下方有further configuration available提示

点击进去:

在Content directory中填写 /src/main/webapp,注意勾选 Generate web.xml deployment descritor,这样才会创建web.xml

BasicWeb的pom文件,我下面再交代如何配置,现在暂时跳过;

4、创建BasicMainPage,web项目中的一个模块

创建的方法这里就省略了,创建的是web项目 packing为war,创建方法跟BasicWeb是一样的,不过注意,

在Content directory中填写 /src/main/webapp之后,注意不要勾选 Generate web.xml deployment descritor,

因为项目有主web项目管理,所以不创建web.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>  <parent>    <groupId>com.test.basic</groupId>    <artifactId>Baisc</artifactId>    <version>0.0.1-SNAPSHOT</version>  </parent>  <artifactId>BasicMainPage</artifactId>  <packaging>war</packaging>    <!-- 加入基础依赖 -->  <dependencies>  	<dependency>  		<artifactId>BasicJar</artifactId>  		<groupId>com.test.basic</groupId>  		<version>${project.version}</version>  	</dependency>  </dependencies>      <!-- 配置项目,忽略web.xml配置 不独立配置web.xml -->  <build>        <finalName>BasicMainPage</finalName>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-war-plugin</artifactId>                <configuration>                    <failOnMissingWebXml>false</failOnMissingWebXml>                </configuration>            </plugin>        </plugins>    </build></project>最后,我们再进行配置BasicWeb的pom.xml,主要是合拼BasicMainPage的war,需要在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>	<parent>		<groupId>com.test.basic</groupId>		<artifactId>Baisc</artifactId>		<version>0.0.1-SNAPSHOT</version>	</parent>	<artifactId>BasicWeb</artifactId>	<packaging>war</packaging>	<dependencies>		<dependency>			<artifactId>BasicJar</artifactId>			<groupId>com.test.basic</groupId>			<version>${project.version}</version>		</dependency>		<dependency>			<artifactId>BasicMainPage</artifactId>			<groupId>com.test.basic</groupId>			 <version>${project.version}</version>            <type>war</type>		</dependency>	</dependencies>	<build>		<plugins>			<!-- 合并多个war -->			<plugin>				<groupId>org.apache.maven.plugins</groupId>				<artifactId>maven-war-plugin</artifactId>				<configuration>					<packagingExcludes>WEB-INF/web.xml</packagingExcludes>					<overlays>						<overlay>							<groupId>com.test.basic</groupId>							<artifactId>BasicMainPage</artifactId>						</overlay>					</overlays>				</configuration>			</plugin>		</plugins>	</build>	</project>

我们在BaiscWeb项目中的webapp文件夹下创建index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>标题</title></head><body>Test!<a href="/BasicMainPage/index2.jsp">点击我跳转到BasicMainPage</a></body></html>在BasicMainPage的webapp文件夹下创建index2.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>index2</title></head><body>	index2</body></html>这样,两个项目就可以互相跳转了!

另外,我们可以在BasicWeb的web.xml中,指定index2.jsp为welcome-file

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>TestWeb</display-name>  <welcome-file-list>    <welcome-file>index2.jsp</welcome-file>  </welcome-file-list></web-app>运行BasicWeb项目时,会自动跳转到BasicMainPage的index2.jsp页面了


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表