如何通过Maven将资源文件保留在重打包JAR的根目录?
Absolutely! You don’t have to manually tinker with the JAR to get my-file.txt in the root directory—Maven has you covered. Here are two solid approaches that work with your Spring Boot 1.4.1 setup and the repackage goal you’re using:
Approach 1: Use additionalResources in the Spring Boot Maven Plugin
This is the cleanest method since it integrates directly with the repackaging process you’re already using. You can configure the plugin to include your file as an extra resource that lands straight in the executable JAR’s root.
Add this configuration to your spring-boot-maven-plugin in pom.xml:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.1.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <!-- Add my-file.txt as an additional resource for the JAR root --> <additionalResources> <additionalResource> <directory>src/main/resources</directory> <includes> <include>my-file.txt</include> </includes> </additionalResource> </additionalResources> </configuration> </execution> </executions> </plugin>
Note: By default, this will place my-file.txt in both the JAR root and BOOT-INF/classes/ (since Maven’s default resource copying still applies). If you want to avoid the duplicate in BOOT-INF/classes/, add an exclusion to the Maven Resources Plugin:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <!-- Use a version compatible with your Maven setup --> <configuration> <excludes> <exclude>my-file.txt</exclude> </excludes> </configuration> </plugin>
Approach 2: Copy the File Explicitly Before Repackaging
If you prefer a more transparent file-copy step, use the Maven Resources Plugin to move my-file.txt directly to the build directory that gets packaged into the executable JAR’s root.
Add these configurations to your pom.xml:
<!-- Copy my-file.txt to the build output directory root --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>copy-root-resource</id> <phase>prepare-package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>my-file.txt</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <!-- Keep your existing Spring Boot repackage setup --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.1.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
The prepare-package phase runs right before the package phase, so the file will be in the correct spot when the Spring Boot plugin repackages the JAR—ensuring it ends up in the root directory.
After setting up either approach, run mvn clean package and check the resulting JAR. You’ll find my-file.txt at the root level exactly as you want!
内容的提问来源于stack exchange,提问作者Mr. Nicky

