如何配置Java,使其编译器从test-classes文件夹读取指定XSD文件?
Hey there! Let's break down how to adjust your setup so your program reads the XSD file from the test-classes folder instead of target when running the JAR.
First, a quick context check: test-classes is the directory where build tools like Maven store test classes and test resources, and it's not included in the main JAR by default. To make your program access files here, we'll cover both build configuration tweaks and code adjustments:
1. Adjust Maven Build Configuration (if using Maven)
If your project uses Maven, the main JAR only includes content from src/main/resources and target/classes by default. Here are two ways to make test-classes resources accessible:
Option 1: Package test resources into the main JAR
Add thismaven-jar-pluginconfig to yourpom.xmlto include the XSD file fromtest-classesinto your main JAR:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <additionalResources> <resource> <directory>${project.build.testOutputDirectory}</directory> <includes> <include>**/*.xsd</include> </includes> </resource> </additionalResources> </configuration> </plugin> </plugins> </build>After building, the XSD will live inside the JAR, so your program won't depend on the external
test-classesfolder anymore.Option 2: Add test-classes to the classpath at runtime
If you don't want to modify the JAR content, just appendtest-classesto the classpath when running the JAR:# Linux/macOS java -cp your-app.jar:target/test-classes com.your.package.MainClass # Windows java -cp your-app.jar;target/test-classes com.your.package.MainClassThe JVM will scan all directories in the classpath, so it'll pick up the XSD from
test-classes.
2. Fix How Your Code Reads the Resource
No matter which build config you use, the way your code fetches the XSD matters a lot. Always use classpath-based reading instead of hardcoding absolute paths:
- ❌ Bad (depends on absolute file path):
File xsdFile = new File("target/your-schema.xsd"); - ✅ Good (reads from classpath):
This way, the program will find the XSD as long as it's present in any directory on the classpath (including// Use this in main classes InputStream xsdStream = getClass().getClassLoader().getResourceAsStream("your-schema.xsd"); // Or this in test classes (leading slash ensures root classpath lookup) InputStream xsdStream = getClass().getResourceAsStream("/your-schema.xsd");test-classes).
3. For Test-Specific JARs
If you're building a JAR specifically to run tests, use Maven's test-jar goal to package test classes and resources:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin>
When you run this test JAR, the classpath will automatically include test-classes content, so your XSD will be found without extra steps.
内容的提问来源于stack exchange,提问作者Matt

