一、项目工程目录
先用maven创建一个空的父类工程,然后再创建子模块,本例子中springboot为父类项目,platform为子模块。
二、重要配置文件
(1)父项目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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>springboot</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>platform</module>
</modules>
<name>springboot</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<main.basedir>${basedir}</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
<project.deploy.directory>${basedir}/target</project.deploy.directory>
</properties>
<dependencyManagement>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
备注:父类pom文件中<dependencyManagement></dependencyManagement>用来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。<pluginManagement></pluginManagement>也是同样道理。
(2)子类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">
<parent>
<artifactId>springboot</artifactId>
<groupId>com.demo</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>platform</artifactId>
<name>platform</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 该插件为Spring Boot应用提供了执行Maven操作的 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 打包后的输出路径 -->
<outputDirectory>${project.deploy.directory}</outputDirectory>
</configuration>
</plugin>
<!-- 编译插件,用于指定编译的jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- 将当前项目打包为Jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<!-- 用于定制化打包方式 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>config-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<skipAssembly>false</skipAssembly>
<!-- 打包输出路径 -->
<outputDirectory>${project.deploy.directory}</outputDirectory>
<!-- 描述符文件,用于描述那些文件需要被打包 -->
<descriptor>src/main/assembly/config-zip.xml</descriptor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
其中,maven-assembly-plugin插件所引用的描述符文件config-zip.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<!-- id 标识符,添加到生成文件名称的后缀符 -->
<id>config</id>
<!-- 打包格式,支持多种文件格式 -->
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<!-- 管理一组文件的存放位置 -->
<fileSets>
<fileSet>
<directory>${basedir}/src/main/assembly</directory>
<includes>
<include>dbscript/**</include>
<include>doc/**</include>
</includes>
<!-- 指定文件集合的输出目录,该目录是相对于根目录 -->
<outputDirectory>./</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>config/**</include>
</includes>
<outputDirectory>./</outputDirectory>
</fileSet>
</fileSets>
</assembly>
(3)application.properties文件
spring.application.name=springboot_demo
#tomcat访问上下文路径
server.context-path=/springboot_demo
#指定tomcat端口
server.port=8090
#指定tomcat的编码格式
server.tomcat.uri-encoding=UTF-8
#配置字符集过滤器
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
(4)主程序类
package com.demo.platform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
最后,启动程序即可运行