springboot maven-resources-plugin使用


springboot maven-resources-plugin使用

                              

官网:Apache Maven Resources Plugin – Introduction

                                   

                                            

**********************

maven resources plugin

                        

The Resources Plugin handles the copying of project resources to the output directory. 
# resources插件用来把项目文件复制到output directory

There are two different kinds of resources: main resources and test resources. 
The difference is that the main resources are the resources associated to the main source code while the test resources are associated to the test source code.
# 资源类型有两种:main resources、test resources
# main resources和main 源代码相关联
# test resources和test 源代码相关联

Thus, this allows the separation of resources for the main source code and its unit tests
# 因此,可以将main rsources、单元测试代码分离开来

                                

resources goals

# resources:resources
copies the resources for the main source code to the main output directory.
This goal usually executes automatically, because it is bound by default to the process-resources life-cycle phase. It always uses the project.build.resources element to specify the resources, and by default uses the project.build.outputDirectory to specify the copy destination.
# 复制main resources到main output目录,默认自动执行
# 源文件:project.build.resources
# output directory:project.build.outputDirectory (target/classes)

# resources:testResources 
copies the resources for the test source code to the test output directory.
This goal usually executes automatically, because it is bound by default to the process-test-resources life-cycle phase. It always uses the project.build.testResources element to specify the resources, and by default uses the project.build.testOutputDirectory to specify the copy destination.
# 复制test resources到test output目录,默认自动执行
# 源文件:project.build.testResources
# output directory:project.build.testOutputDirectory (target/test-classes)

# resources:copy-resources 
copies resources to an output directory.
This goal requires that you configure the resources to be copied, and specify the outputDirectory
# 复制resources到指定目录
# 不会自动执行,需要自行配置需要复制的资源、输出的目录

                                    

resources:resources goal 参数说明

# 必选参数
outputDirectory:资源输出目录,默认为:${project.build.outputDirectory}

# 可选参数
addDefaultExcludes:.gitignore, .cvsignore等文件默认不会复制
                    设置false,可复制所有文件

delimiters:资源文件分隔符
<delimiters>
  <delimiter>${*}</delimiter>
  <delimiter>@</delimiter>
</delimiters>

includeEmptyDirs:是否复制空的目录,默认为false
encoding:读、写过滤资源文件使用的编码,默认为:${project.build.sourceEncoding}
escapeString:转义符,默认为:\
escapeWindowsPaths:Whether to escape backslashes and colons in windows-style paths
                    默认为true

fileNameFiltering:是否支持过滤文件夹,默认为false
filters:过滤的文件

mavenFilteringHints:过滤提示
nonFilteredFileExtensions:不过滤的文件拓展,默认: jpg, jpeg, gif, bmp, png(这些文件不会过滤)

overwrite:output directory文件更新时,是否进行覆盖,默认false
propertiesEncoding:属性文件编码,如果没有设置,默认为系统encoding的值

skip:是否不执行该goal,不推荐(Its use is NOT RECOMMENDED, but quite convenient on occasion)
      默认为false
supportMultiLineFiltering:是否支持多行过滤,默认为false
useBuildFilters:build时是否过滤,默认为true
useDefaultDelimiters:是否使用默认分隔符,默认为true

                                       

resources:testResources goal 参数说明

# 必选参数
outputDirectory:资源输出目录,默认为:${project.build.outputDirectory}
resources:需要复制的test资源,默认为:${project.testResources}

# 可选参数
addDefaultExcludes:.gitignore, .cvsignore等文件默认不会复制
                    设置false,可复制所有文件
 
delimiters:资源文件分隔符
<delimiters>
  <delimiter>${*}</delimiter>
  <delimiter>@</delimiter>
</delimiters>
 
includeEmptyDirs:是否复制空的目录,默认为false
encoding:读、写过滤资源文件使用的编码,默认为:${project.build.sourceEncoding}
escapeString:转义符,默认为:\
escapeWindowsPaths:Whether to escape backslashes and colons in windows-style paths
                    默认为true
 
fileNameFiltering:是否支持过滤文件夹,默认为false
filters:过滤的文件
 
mavenFilteringHints:过滤提示
nonFilteredFileExtensions:不过滤的文件拓展,默认: jpg, jpeg, gif, bmp, png(这些文件不会过滤)
 
overwrite:output directory文件更新时,是否进行覆盖,默认false
propertiesEncoding:属性文件编码,如果没有设置,默认为系统encoding的值
 
skip:是否不执行该goal,不推荐(Its use is NOT RECOMMENDED, but quite convenient on occasion)
      默认为false
supportMultiLineFiltering:是否支持多行过滤,默认为false
useBuildFilters:build时是否过滤,默认为true
useDefaultDelimiters:是否使用默认分隔符,默认为true

                                     

resources:copyResources goal 参数说明

# 必选参数
outputDirectory:资源输出目录,需自定义
resources:需要复制的资源,需自定义

# 可选参数
addDefaultExcludes:.gitignore, .cvsignore等文件默认不会复制
                    设置false,可复制所有文件
 
delimiters:资源文件分隔符
<delimiters>
  <delimiter>${*}</delimiter>
  <delimiter>@</delimiter>
</delimiters>
 
includeEmptyDirs:是否复制空的目录,默认为false
encoding:读、写过滤资源文件使用的编码,默认为:${project.build.sourceEncoding}
escapeString:转义符,默认为:\
escapeWindowsPaths:Whether to escape backslashes and colons in windows-style paths
                    默认为true
 
fileNameFiltering:是否支持过滤文件夹,默认为false
filters:过滤的文件
 
mavenFilteringHints:过滤提示
nonFilteredFileExtensions:不过滤的文件拓展,默认: jpg, jpeg, gif, bmp, png(这些文件不会过滤)
 
overwrite:output directory文件更新时,是否进行覆盖,默认false
propertiesEncoding:属性文件编码,如果没有设置,默认为系统encoding的值
 
skip:是否不执行该goal,不推荐(Its use is NOT RECOMMENDED, but quite convenient on occasion)
      默认为false
supportMultiLineFiltering:是否支持多行过滤,默认为false
useBuildFilters:build时是否过滤,默认为true
useDefaultDelimiters:是否使用默认分隔符,默认为true

                                                                                     

                                                                             

**********************

示例

                        

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>hello</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <!-- 将资源文件输出到${project.build.directory}/resources目录 -->
                        <configuration>
                            <outputDirectory>${project.build.directory}/resources</outputDirectory>
                            <resources>
                                <resource>
                                    <!-- 如果设置了targetPath, 则输出到targetPath -->
                                    <targetPath>${project.build.directory}/java</targetPath>
                                    <directory>src/main/java</directory>
                                </resource>
                                    <!-- 如果没有设置targetPath, 则输出到outputDirectory设置的目录 -->
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

                         

说明:直接在build标签设置resources,也可实现文件复制

    <build>
        <finalName>hello</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <targetPath>${project.build.directory}/java</targetPath>
                <directory>src/main/java</directory>
            </resource>
            <resource>
                <targetPath>${project.build.directory}/resources</targetPath>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

                            

maven 打包

                                  

src/main/java 复制到了target/java目录

src/main/resources 复制到了 target/resources目录

                         

                                         

Guess you like

Origin blog.csdn.net/weixin_43931625/article/details/120581211