Java web项目创建笔记8 之《springmvc基础配置——将maven单项目改为多模块项目》

经过比较,建议使用方式二新建maven项目迁移原代码

一、方式一:在原先webapp项目上直接修改
1、将原单项目pom文件中打包方式改为pom,作为父模块
<packaging>pom</packaging>

maven中packaging标签
war:打包出来为war包,用于部署
jar:打包出来为jar包,用于服务调用划分模块
pom:父模块用于聚合子模块和传递依赖用

2、创建子模块

创建五个子模块:
webapp_base
webapp_domain
webapp_web
webapp_module1
webapp_module2

3、移动代码
1)原com.study.util包及下面代码,移动到webapp_base里
2)原com.study.testBean包及下面代码,移动到webapp_domain里
3)原com.study.resources包及下面代码,移动到webapp_web里
          src/main/resources下面的移动到webapp_web里
          src/test/java下面的移动到webapp_web里
          src/main/webapp下面的移动到webapp_web里

4、删除原webapp下的src和target目录

5、右键webapp项目,选Maven — Update Project

6、maven编译项目

7、pom文件
webapp_base的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>
  <parent>
    <groupId>com.study</groupId>
    <artifactId>webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>webapp_base</artifactId>
  
  <name>webapp_base</name>
  <packaging>jar</packaging>
</project>

webapp_domain的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>
  <parent>
    <groupId>com.study</groupId>
    <artifactId>webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>webapp_domain</artifactId>
  
  <name>webapp_domain</name>
  <packaging>jar</packaging>
</project>

webapp_module1的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>
  <parent>
    <groupId>com.study</groupId>
    <artifactId>webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>webapp_module1</artifactId>
  
  <name>wabapp_module1</name>
  <packaging>jar</packaging>
</project>

webapp_module2的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>
  <parent>
    <groupId>com.study</groupId>
    <artifactId>webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>webapp_module2</artifactId>
  
  <name>webapp_module2</name>
  <packaging>jar</packaging>
</project>

webapp_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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.study</groupId>
    <artifactId>webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>webapp_web</artifactId>
  <name>webapp_web</name>
  <packaging>war</packaging>
  
  <dependencies>
        <dependency>
            <groupId>com.study</groupId>
            <artifactId>webapp_base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.study</groupId>
            <artifactId>webapp_domain</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.study</groupId>
            <artifactId>webapp_module1</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.study</groupId>
            <artifactId>webapp_module2</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

父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.study</groupId>
	<artifactId>webapp</artifactId>
	<packaging>pom</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>webapp Maven Webapp</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<springframework.version>4.3.22.RELEASE</springframework.version>
		<aspectj.version>1.8.13</aspectj.version>
		<commons-io.version>2.6</commons-io.version>
		<commons-codec.version>1.12</commons-codec.version>
		<commons-lang3.version>3.8.1</commons-lang3.version>
		<commons-pool2.version>2.6.1</commons-pool2.version>
		<servlet.version>3.1.0</servlet.version>
		<slf4j.version>1.7.26</slf4j.version>
		<logback.version>1.2.3</logback.version>
		<logback-ext-spring.version>0.1.5</logback-ext-spring.version>
		<jcl.version>1.7.26</jcl.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- springmvc框架 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-websocket</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${springframework.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- 其他工具包 -->
		<!-- apache commons -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>${commons-codec.version}</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>${commons-io.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>${commons-lang3.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>${commons-pool2.version}</version>
		</dependency>
		<!-- servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- slf4j + logback -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${jcl.version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>${logback.version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
		</dependency>
		<dependency>
			<groupId>org.logback-extensions</groupId>
			<artifactId>logback-ext-spring</artifactId>
			<version>${logback-ext-spring.version}</version>
		</dependency>
	</dependencies>

	<!-- 环境切换参数 -->
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<env.config>dev</env.config>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>sit</id>
			<properties>
				<env.config>sit</env.config>
			</properties>
		</profile>
		<profile>
			<id>uat</id>
			<properties>
				<env.config>uat</env.config>
			</properties>
		</profile>
		<profile>
			<id>pit</id>
			<properties>
				<env.config>pit</env.config>
			</properties>
		</profile>
	</profiles>

	<build>
		<finalName>webapp</finalName>
		<!--过滤resource下的文件 -->
		<resources>
			<resource>
				<!-- 会把该目录下的文件放在WEB-INF/classes下 -->
				<filtering>true</filtering>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.2</version>
				<configuration>
					<encoding>UTF-8</encoding>
					<webResources>
						<webResource>
							<filtering>true</filtering>
							<!-- 原配置文件的目录,相对于pom.xml文件路径 -->
							<directory>src/main/webapp</directory>
							<includes>
								<include>**/web.xml</include>
							</includes>
						</webResource>
					</webResources>
				</configuration>
			</plugin>
		</plugins>

	</build>

	<modules>
		<module>webapp_base</module>
		<module>webapp_web</module>
		<module>webapp_domain</module>
		<module>webapp_module1</module>
		<module>webapp_module2</module>
	</modules>
</project>

8、例子程序打包:
webapp_2.rar
链接: https://pan.baidu.com/s/1N-P3QPgTHF-2y3MvH6Kp0g 提取码: qgfi

二、方式二:新建webapp2将老项目迁移进去
由于原项目本身是一个maven webapp项目,将原项目改为父项目会有残留的东西
1、建立父项目webapp2
New — Maven Project

2、依次建立子模块
webapp2_base
webapp2_domain
webapp2_web
webapp2_module1
webapp2_module2

New — Maven Module
1)建立webapp2_base

webapp2_base、webapp2_domain、webapp2_module1、webapp2_module2建立方式相同

2)建立webapp2_web

3、迁移pom文件和代码
1)配置父pom文件
build标签下的<finalName>改为webapp2

<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.study</groupId>
	<artifactId>webapp2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>webapp2</name>
	<modules>
		<module>webapp2_base</module>
		<module>webapp2_domain</module>
		<module>webapp2_module1</module>
		<module>webapp2_module2</module>
		<module>webapp2_web</module>
	</modules>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<springframework.version>4.3.22.RELEASE</springframework.version>
		<aspectj.version>1.8.13</aspectj.version>
		<commons-io.version>2.6</commons-io.version>
		<commons-codec.version>1.12</commons-codec.version>
		<commons-lang3.version>3.8.1</commons-lang3.version>
		<commons-pool2.version>2.6.1</commons-pool2.version>
		<servlet.version>3.1.0</servlet.version>
		<slf4j.version>1.7.26</slf4j.version>
		<logback.version>1.2.3</logback.version>
		<logback-ext-spring.version>0.1.5</logback-ext-spring.version>
		<jcl.version>1.7.26</jcl.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- springmvc框架 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-websocket</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${springframework.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- 其他工具包 -->
		<!-- apache commons -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>${commons-codec.version}</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>${commons-io.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>${commons-lang3.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>${commons-pool2.version}</version>
		</dependency>
		<!-- servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- slf4j + logback -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${jcl.version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>${logback.version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
		</dependency>
		<dependency>
			<groupId>org.logback-extensions</groupId>
			<artifactId>logback-ext-spring</artifactId>
			<version>${logback-ext-spring.version}</version>
		</dependency>
	</dependencies>

	<!-- 环境切换参数 -->
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<env.config>dev</env.config>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>sit</id>
			<properties>
				<env.config>sit</env.config>
			</properties>
		</profile>
		<profile>
			<id>uat</id>
			<properties>
				<env.config>uat</env.config>
			</properties>
		</profile>
		<profile>
			<id>pit</id>
			<properties>
				<env.config>pit</env.config>
			</properties>
		</profile>
	</profiles>

	<build>
		<finalName>webapp2</finalName>
		<!--过滤resource下的文件 -->
		<resources>
			<resource>
				<!-- 会把该目录下的文件放在WEB-INF/classes下 -->
				<filtering>true</filtering>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.2</version>
				<configuration>
					<encoding>UTF-8</encoding>
					<webResources>
						<webResource>
							<filtering>true</filtering>
							<!-- 原配置文件的目录,相对于pom.xml文件路径 -->
							<directory>src/main/webapp</directory>
							<includes>
								<include>**/web.xml</include>
							</includes>
						</webResource>
					</webResources>
				</configuration>
			</plugin>
		</plugins>

	</build>
</project>

2)配置webapp2_web的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.study</groupId>
		<artifactId>webapp2</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>webapp2_web</artifactId>
	<packaging>war</packaging>
	<name>webapp2_web</name>

	<dependencies>
		<dependency>
			<groupId>com.study</groupId>
			<artifactId>webapp2_base</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.study</groupId>
			<artifactId>webapp2_domain</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.study</groupId>
			<artifactId>webapp2_module1</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.study</groupId>
			<artifactId>webapp2_module2</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

3)移动代码
原com.study.util包及下面代码,移动到webapp2_base里
原com.study.testBean包及下面代码,移动到webapp2_domain里
原com.study.resources包及下面代码,移动到webapp2_web里
    src/main/resources下面的移动到webapp2_web里
    src/test/java下面的移动到webapp2_web里
    src/main/webapp下面的移动到webapp2_web里
删除父项目webapp2下面的src目录
4)更新&编译
更新maven项目
编译maven项目
完成

4、例子程序:
webapp2.rar
链接: https://pan.baidu.com/s/1mrXWu-CYKB06DUg0av4gog 提取码: 2nff

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值