如何从JAR文件读取pom.xml并列出Maven项目依赖?
Hey there! Let's break down your problem into two parts: first, a better approach to list Maven dependencies, and second, how to properly access your pom.xml when your plugin is packaged as a JAR.
Parsing pom.xml manually is error-prone—it misses transitive dependencies, doesn't handle scope rules, and breaks if the pom uses Maven features like properties or profiles. Instead, use Maven's official APIs to get a fully resolved dependency tree:
Leverage Maven Core's Project Model
UseMavenProjectandDependencyGraphBuilderto build a structured dependency tree. This gives you access to all direct and transitive dependencies, along with their scopes, versions, and other metadata. Here's a quick example:import org.apache.maven.project.MavenProject; import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder; import org.apache.maven.shared.dependency.graph.DependencyNode; // These components are typically injected via Maven's component system for plugins MavenProject project; DependencyGraphBuilder dependencyGraphBuilder; // Build the full dependency tree DependencyNode rootNode = dependencyGraphBuilder.buildDependencyGraph(project, null); // Traverse the node tree to extract dependency details // For example, iterate over child nodes and get groupId/artifactId/versionUse Aether (Maven's Dependency Resolution Engine)
If you need more control (like fetching dependencies from remote repos, handling snapshots, or custom resolution rules), use Aether—Maven's underlying dependency management engine. It lets you request dependency trees with fine-grained configuration, making it ideal for complex plugin logic.
The FileNotFoundException happens because when packaged as a JAR, pom.xml isn't a standalone file on the filesystem—it's a resource inside the JAR. Here's how to access it correctly:
Read via ClassLoader.getResourceAsStream()
Use your class loader to pull the pom as an input stream instead of trying to access it as a file. This works regardless of whether you're running locally or from a JAR:InputStream pomStream = getClass().getClassLoader().getResourceAsStream("pom.xml"); if (pomStream == null) { throw new IOException("Could not find pom.xml in JAR classpath"); } // Parse the stream using Maven's built-in reader (recommended) MavenXpp3Reader reader = new MavenXpp3Reader(); Model pomModel = reader.read(pomStream); // Access dependencies directly from the model List<Dependency> dependencies = pomModel.getDependencies(); pomStream.close();Ensure pom.xml is Packaged in the JAR
By default, Maven might not include yourpom.xmlin the JAR. Add this to your plugin'spom.xmlto force it to include the file in the classpath:<build> <resources> <resource> <directory>${project.basedir}</directory> <includes> <include>pom.xml</include> </includes> </resource> </resources> </build>If your plugin's pom is stored in the standard Maven metadata directory (
META-INF/maven/[groupId]/[artifactId]/pom.xml), adjust the path ingetResourceAsStream()to match that location.Avoid Manual XML Parsing
Skip raw XML parsing entirely by using Maven'sModelReader(likeMavenXpp3Readershown above). It converts the pom stream directly into a typedModelobject, so you can access dependencies, properties, and other pom data without dealing with DOM/SAX parsing.
内容的提问来源于stack exchange,提问作者Mattew Eon

