maven使用IDEA创建单模块是很简单的,但是一般企业级的项目是需要多个子模块的组合,下面使用多个模块的简单介绍。
概要
tool: idea
projectName: zeus
groupId: com.example
parent artifactId: zeus-all
common artifactId: zeus-common
dao artifactId: zeus-dao
service artifactId: zeus-service
web artifactId: zeus-web
项目创建步骤
1. 创建项目父模块
父模块的结构如下
创建项目父模块修改父pom.xml文件的artifactId值为zeus-all
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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.example</groupId>
<artifactId>zeus-all</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
删除父模块下的src文件夹,删除后的目录结构如下
删除父模块下的src文件夹2. 新建子模块
建子模块方法如下
建子模块
分别创建zeus-common子模块,zeus-dao子模块,zeus-service子模块,zeus-web子模块
创建子模块后的项目文件结构3. 修改pom.xml文件
3.1 修改zeus-common模块的pom.xml文件
因zeus-common属于最低层依赖,故无需修改
3.2 修改zeus-dao模块的pom.xml文件
添加对zeus-common模块的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>zeus-all</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zeus-dao</artifactId>
<!--add zeus-common module dependency-->
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>zeus-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3.3 修改zeus-service模块的pom.xml文件
添加zeus-common、zeus-dao模块的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>zeus-all</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zeus-service</artifactId>
<!-- add zeus-common, zeus-dao module dependency -->
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>zeus-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>zeus-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3.4 修改zeus-web模块的pom.xml文件
添加zeus-common、zeus-dao、zeus-service模块的依赖,以及spring boot的starter web等的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>zeus-all</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zeus-web</artifactId>
<dependencies>
<!-- add zeus-common, zeus-dao, zeus-service module dependency-->
<dependency>
<groupId>com.example</groupId>
<artifactId>zeus-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>zeus-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>zeus-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- add spring boot starter and web dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- add projectName, spring boot maven plugin -->
<build>
<finalName>zeus</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.5 修改父模块的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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.example</groupId>
<artifactId>zeus-all</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>zeus-common</module>
<module>zeus-dao</module>
<module>zeus-service</module>
<module>zeus-web</module>
</modules>
<!-- add spring boot parent dependency -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
</project>
4. 创建spring main文件
创建文件
创建Main文件文件内容
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class ZeusApplication {
@RequestMapping("/")
@ResponseBody
public String home() {
return "hello zeus! ";
}
public static void main(String[] args) {
SpringApplication.run(ZeusApplication.class, args);
}
}
编译
在项目主模块目录下,执行如下
$ mvn compile
打包
在项目主模块目录下,执行如下
$ man package
运行
进入zeus-web模块的target目录下
$ java -jar zeus.jar
运行成功后会返回
2017-10-22 23:18:50.137 INFO 3060 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.example.ZeusApplication.home()
2017-10-22 23:18:50.142 INFO 3060 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-10-22 23:18:50.143 INFO 3060 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-10-22 23:18:50.198 INFO 3060 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-22 23:18:50.199 INFO 3060 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-22 23:18:50.306 INFO 3060 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-22 23:18:50.543 INFO 3060 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-10-22 23:18:50.691 INFO 3060 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-10-22 23:18:50.699 INFO 3060 --- [ main] com.example.ZeusApplication : Started ZeusApplication in 13.912 seconds (JVM running for 14.418)