maven Learning Series 8 - Packing files in the resources directory outside the jar package

Keywords: Maven xml

By default, maven types files from src/main/resources into the jar package together with class files, but in many scenarios, files from resources need to be packaged outside the jar package, so modifying files from resources directory does not require re-typing jar.

Assuming the directory structure is as follows, there are three files in the resources directory

The default jar package is as follows: the files under resources are typed into the jar package

If you want some or all of the files in the resources directory not to go inside the jar package, you can use the configuration below maven

 <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.txt</include>
                </includes>
                <excludes>
                    <exclude>*.xml</exclude>
                    <exclude>*.yaml</exclude>
                </excludes>
            </resource>
        </resources>
 </build>
So a.xml and b.yaml won't be in the jar package

Maven also has a maven-resources-plugin, which allows you to copy files from the resources directory to the specified directory when packaging, so that you don't need users to copy them by themselves.

 <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/conf</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
 
                    </execution>
                </executions>
            </plugin>
</plugins>

After compiling, the conf directory is generated in the target directory and all files in the resources directory are automatically copied to the target/conf/directory, as follows


In conjunction with the maven configuration in the previous chapter (maven Learning Series 7 - Packaging dependency files into jars), you can package external dependencies (dependency jar packages, dependency resources files) outside the jar, such as dependency jar packages into lib directory, dependency resources files into conf directory.

It is reproduced in: https://blog.csdn.net/ITsenlin/article/details/53107304

Posted by biffta on Sun, 24 Mar 2019 11:00:27 -0700