crackcell's dustbin home projects
首页 > Spring Boot使用自定义Parent POM > 正文

Spring Boot使用自定义Parent POM

1 Spring Boot的Parent

Spring Boot应用的POM文件默认使用自己的Parent,例如:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

Spring Boot官方文档有提使用自定义Parent的办法(Using Spring Boot without the Parent POM),但写得并不清楚,简单照搬会遇到一些报错。

2 自定义Parent

但在稍微大一点的项目中,一般会自己定义Parent文件。例如叫car的一个项目,包含了2个子模块:car-server和car-storage。car-server是一个Spring Boot写的Rest服务,用来对外提供服务,car-storage是一个公共库,用来提供对后端数据的存取。一种常见的文件结构是这样:

+--- car
|     +--- car-parent
|          \--- pom.xml
|     +--- car-storage
|          \--- pom.xml
|     +--- car-server
|          \--- pom.xml
|     \--- pom.xml
  • car项目顶层的pom文件,简单定义子目录作为module。作用仅仅是Maven编译时能一起联动操作
  • car-parent:真正的parent,定义了一些常用的依赖关系等
  • car-storage:底层数据存储包
  • car-server:Spring Boot的Rest服务,对外提供API服务,依赖Parent和car-storage

2.1 顶层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>

    <groupId>com.crackcell</groupId>
    <artifactId>car</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>car-parent</module>
        <module>car-storage</module>
        <module>car-server</module>
    </modules>
</project>

2.2 car-parent

<?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>
    <groupId>com.crackcell</groupId>
    <artifactId>car-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>

        <!-- default bytecode version for src/main -->
        <main.java.version>1.7</main.java.version>
        <main.signature.artifact>java17</main.signature.artifact>

        <!-- default bytecode version for src/test -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <spring-boot.version>1.4.0.RELEASE</spring-boot.version>
        <commons-codec.version>1.10</commons-codec.version>

        <maven-plugin.version>0.3.3</maven-plugin.version>
        <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
        <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
        <maven-install-plugin.version>2.5.2</maven-install-plugin.version>
        <maven-source-plugin.version>3.0.1</maven-source-plugin.version>
        <maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>
        <license-maven-plugin.version>2.11</license-maven-plugin.version>
        <maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
        <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
        <maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
        ...
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <mainClass>${start-class}</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
  • 指定Java字节码版本,不然会报版本错误
  • 制定插件版本,不然,也许能正常打包出来,但是执行不了。例如:要么格式错误,要么报找不到main函数的错误

2.3 car-server

<?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>com.crackcell</groupId>
        <artifactId>car-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../car-parent</relativePath>
    </parent>

    <artifactId>car-server</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    ...略
</project>
  • 在单独的dependencyManagement内引入Spring Boot的依赖即可,其它部分跟一般Spring Boot应用一样的写法

Date: Mon Aug 1 20:45:08 2016

Author: Menglong TAN

Created: 2016-08-01 Mon 21:58

Emacs 24.5.1 (Org mode 8.2.10)

Validate

Modified theme and code from Tom Preston-Werner.