Maven构建中如何从依赖Jar读取或合并属性文件?
Hey there, let's work through your Maven build issue step by step—here are practical solutions for each part of your question:
1. Directly Reading Properties from B.jar (The Simplest Approach)
Yes, you absolutely can read properties directly from B.jar without copying them into A.jar. Java's classloader is designed to access resources from any JAR in the classpath, and Maven automatically includes your dependencies (like B.jar) in the runtime classpath.
In your execution class, use this pattern to load the properties files:
// Replace with the actual path to your properties files inside B.jar InputStream propStream1 = getClass().getClassLoader().getResourceAsStream("path/to/first.properties"); InputStream propStream2 = getClass().getClassLoader().getResourceAsStream("path/to/second.properties"); Properties props = new Properties(); props.load(propStream1); // Repeat for the second file
Just make sure the path matches the location of the files inside B.jar (e.g., if the file is at src/main/resources/configs/app.properties in B's project, use configs/app.properties as the path).
For exec-maven-plugin, it automatically includes your compile/runtime dependencies in the execution classpath by default, so as long as B.jar is declared as a <dependency> in your pom.xml, this should work out of the box.
2. Copying B.jar's Properties into A.jar (If You Need To)
If you must package B.jar's properties directly into A.jar, use the maven-dependency-plugin to unpack the specific files into your project's build directory before the JAR is created. Add this configuration to your pom.xml:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack-b-properties</id> <phase>process-resources</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <!-- Replace with B.jar's groupId, artifactId, and version --> <groupId>com.yourcompany</groupId> <artifactId>B</artifactId> <version>1.0.0</version> <type>jar</type> <!-- List the exact properties files you want to extract --> <includes>**/first.properties,**/second.properties</includes> <!-- Output to target/classes so maven-jar-plugin packages them --> <outputDirectory>${project.build.directory}/classes</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin>
This will run during the process-resources phase, extracting the specified properties files into target/classes, which maven-jar-plugin will then include in A.jar.
3. Does maven-jar-plugin Have an Overlay Feature Like maven-war-plugin?
Unfortunately, no—the maven-jar-plugin does not support overlays natively (this feature is exclusive to the WAR plugin for merging web resources). However, the maven-dependency-plugin unpack approach above effectively simulates this behavior for JARs, letting you pull specific files from B.jar into A.jar.
If you need to merge entire JAR contents (not just specific files), you can use the maven-assembly-plugin to create a fat JAR that combines A and B's classes/resources.
4. Troubleshooting exec-maven-plugin Execution
If your execution class still can't find the properties files, double-check:
- B.jar is declared as a
<dependency>with the correct scope (usecompileorruntime—notprovidedortest). - The path in
getResourceAsStream()is correct (case-sensitive, no leading slash). - Your
exec-maven-pluginconfiguration uses the defaultclasspathScope(which iscompile) or explicitly includes runtime dependencies:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <configuration> <mainClass>com.yourcompany.YourExecutionClass</mainClass> <!-- Ensure runtime dependencies are included --> <classpathScope>runtime</classpathScope> </configuration> </plugin>
内容的提问来源于stack exchange,提问作者hell_storm2004

