springboot项目的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>org.example</groupId>
    <artifactId>nettyWsServer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR5</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--swagger依赖开始-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.3</version>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.43.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.10.0</version>
        </dependency>
    </dependencies>

    <!--不同环境打包,默认为dev -->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
        </profile>
    </profiles>


    <build>
        <finalName>nettyWsServer</finalName>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>application-*.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application-${package.environment}.yml</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
  1. build中resource解释:对资源文件进行复制时的选项。正常情况下maven只会将src/main/resource下的资源文件复制到jar包中,但是有时我们会在src/main/java下放文件如xxx.xml,此时需要进行配置达到一起打进jar包的目的。
    (1)includes:包含文件;excludes:不包含文件
    (2)targetPath:指定build资源到哪个目录
    (3)directory:指定属性文件的目录,build的过程需要找到它,并且将其放到targetPath下
    (4)filtering:表示资源文件在复制到目标目录时,是否选择替换占位符。当filtering = true时替换资源文件中的占位符;当filtering = false时不进行占位符的替换。
    比如:
<build>
    .......
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
    ......
</build>

<!-- 上面的配置,解释如下:-->
<!-- 
	将"src/main/java"目录下的所有的.properties和.xml文件进行打包,
	并替换所有的.properties和.xml文件中的占位符。 
    占位符:${name}这样的符号
-->
  1. build中plugins的spring-boot-maven-plugin,加入repackage后的作用:
    (1)在原始Maven打包形成的jar包基础上,进行重新打包,新形成的jar包不但包含应用类文件和配置文件,而且还会包含应用所依赖的jar包以及Springboot启动相关类(loader等),以此来满足Springboot独立应用的特性;
    (2)将原始Maven打包的jar重命名为XXX.jar.original作为原始文件
  2. profiles,用来在多环境时使用,一般有dev、test、prod环境,配置好以后可以在application文件和pom文件中引用。在IDEA的maven -> Profiles中可以选择启动服务时用哪个环境;maven打包时用 -Ptest 可以打对应的包。