maven: < build > < resources > < resource > label introduction

Keywords: Java Maven

Maven_Build_Resources
Function: it is mainly used to package resource files. By default, maven only packages resources under src/main/resource through:
1. Set build_resources
2. Using the build helper Maven plugin plug-in
3. Using the Maven resources plugin plug-in
You can customize the resources to be packaged

Generally, the resource files we use (various xml, properties, xsd files) are placed under src/main/resources. When packaging with maven, Maven can package these resource files into the corresponding jar or war.

Sometimes, for example, the mapper.xml file of mybatis, we are used to putting it together with Mapper.java under src/main/java. In this way, when packaging with maven, we need to modify the pom.xml file. Come on, package the mapper.xml file into jar or war, otherwise, these files will not be packaged. (Maven thinks src/main/java is just the source code path of Java.).

Method 1, where * / is written to ensure that the resource files under all levels of subdirectories are packaged.

<build>  
    <finalName>test</finalName>  
    <!--  
    This can also put all the xml File and package to the corresponding location.  
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
            <includes>  
                <include>**/*.properties</include>  
                <include>**/*.xml</include>  
                <include>**/*.tld</include>  
            </includes>  
            <filtering>false</filtering>  
        </resource>  
        <resource>  
            <directory>src/main/java</directory>  
            <includes>  
                <include>**/*.properties</include>  
                <include>**/*.xml</include>  
                <include>**/*.tld</include>  
            </includes>  
            <filtering>false</filtering>  
        </resource>  
    </resources>  
</build>

Method 2, use the build helper Maven plugin plug-in

<build>  
    ...  
    </plugins>  
        ...  
        <!--  
        this plugin Can use  
        Use this plugin´╝îIn the source code xml Documents,  
        Package to the corresponding location. This is mainly for packaging Mybatis of mapper.xml file   
        -->  
        <plugin>  
            <groupId>org.codehaus.mojo</groupId>  
            <artifactId>build-helper-maven-plugin</artifactId>  
            <version>1.8</version>  
            <executions>  
                <execution>  
                    <id>add-resource</id>  
                    <phase>generate-resources</phase>  
                    <goals>  
                        <goal>add-resource</goal>  
                    </goals>  
                    <configuration>  
                        <resources>  
                            <resource>  
                                <directory>src/main/java</directory>  
                                <includes>  
                                    <include>**/*.xml</include>  
                                </includes>  
                            </resource>  
                        </resources>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>     
        ...  
    </plugins>       
    ...  
</build>

Method 3: use Maven resources plugins plug-in

<build>  
    ...  
    </plugins>  
        ...  
        <!--  
        this plugin Can use  
        Use this plugin´╝îIn the source code xml File, packaged to the appropriate location,  
        This is mainly for packing Mybatis of mapper.xml file   
        -->  
        <plugin>  
            <artifactId>maven-resources-plugin</artifactId>  
            <version>2.5</version>  
            <executions>  
                <execution>  
                    <id>copy-xmls</id>  
                    <phase>process-sources</phase>  
                    <goals>  
                        <goal>copy-resources</goal>  
                    </goals>  
                    <configuration>  
                        <outputDirectory>${basedir}/target/classes</outputDirectory>  
                        <resources>  
                            <resource>  
                                <directory>${basedir}/src/main/java</directory>  
                                <includes>  
                                    <include>**/*.xml</include>  
                                </includes>  
                            </resource>  
                        </resources>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>     
        ...  
    </plugins>       
    ...  
</build>

The following is a description of the maven plug-in:

Resources: describes the location of resources in the project

<resource> 
    <targetPath>META-INF/plexus</targetPath> 
    <filtering>false</filtering> 
    <directory>${basedir}/src/main/plexus</directory> 
    <includes> 
        <include>configuration.xml</include> 
    </includes> 
    <excludes> 
        <exclude>**/*.properties</exclude> 
    </excludes> 
</resource>

targetPath: Specifies the directory to which the build resources are. The default is base directory

filtering: Specifies whether the variable values of the filter file (i.e. the *. property file defined in filters under build) are valid in this resource file. For example, the variable values specified above are invalid in the configuration file.

Directory: specify the directory of the property file. The build process needs to find it and put it under the targetPath. The default directory is ${basedir}/src/main/resources

Include: specify the patterns of the included files. The files that conform to the style and in the directory directory will be included in the resource files of the project.

Exclude: Specifies the patterns that are not included. If there is a conflict between includes and excludes, excludes wins. Files that conform to the conflicting styles will not be included.

testResources: This module contains test resource elements. Its content definition is similar to resources. The difference is that the default test resource path is ${basedir}/src/test/resources, and test resources are not deployed.

By default, if resources is not specified, it is currently considered that the. class file under src/main/java and. xml file under src/main/resources under classpath will be automatically placed in the folder under package under the classes folder in the target. If resources is set, the default will become invalid, and the specified includes and excludes will prevail. For example, to make the packaged jar package contain. java source files.

Posted by youropensource on Sat, 20 Nov 2021 14:23:17 -0800