如何在Maven Central中搜索继承指定类或实现指定接口的类?
Great question! Looking for classes that extend a specific parent (like FileSystemProvider for your use case) or implement an interface across the entire Maven Central repo is a common need when hunting for examples, extensions, or alternative implementations. Unlike searching within your local classpath, this requires using external tools since Maven Central itself doesn’t natively support class hierarchy queries. Here’s how to do it:
1. Sonatype Repository Search (Quickest Web-Based Method)
Sonatype runs the backend for Maven Central, and their search tool supports specific syntax for class inheritance/implementation checks:
- For subclasses of a specific class: Use the
c:extends:prefix followed by the fully qualified class name. For yourFileSystemProviderexample, you’d search:c:extends:java.nio.file.spi.FileSystemProvider - For classes implementing an interface: Use the
c:implements:prefix instead. For example, to find all implementations ofjava.util.Map:c:implements:java.util.Map
You can refine results further by adding filters like group ID, artifact ID, or version ranges. Each result will show you the Maven coordinates you need to add the dependency to your project.
2. Local Analysis with Class Scanning Tools (Custom, Offline Option)
If you need more control (like offline searching or custom filtering), you can download relevant JARs and analyze them locally:
- Download JARs: Use the Maven Dependency Plugin to bulk-download JARs you suspect might contain relevant classes. For example, to download all JARs related to NIO:
mvn dependency:copy -Dartifact=*:nio*:*:jar -DoutputDirectory=./maven-jars - Scan for Classes: Use a library like ClassGraph or JDepend to scan the downloaded JARs. Here’s a quick ClassGraph snippet to find
FileSystemProvidersubclasses:try (ScanResult scanResult = new ClassGraph() .overrideClasspath("./maven-jars") .enableClassInfo() .scan()) { // Find subclasses of FileSystemProvider ClassInfoList subclasses = scanResult.getSubclasses("java.nio.file.spi.FileSystemProvider"); subclasses.forEach(ci -> System.out.println("Found subclass: " + ci.getName())); // Find classes implementing an interface (example) ClassInfoList implementors = scanResult.getClassesImplementing("your.fully.qualified.interface.Name"); implementors.forEach(ci -> System.out.println("Found implementor: " + ci.getName())); }
This method takes more work but lets you dig deeper without relying on web tools.
3. IDE Maven Dependency Search (Indirect but Convenient)
Most modern IDEs (IntelliJ IDEA, Eclipse) let you bridge Maven Central search with class hierarchy analysis:
- In IntelliJ, go to
File > Project Structure > Libraries, click the+button, and selectFrom Maven. - Search for the target class (e.g.,
FileSystemProvider), and check "Include transitive dependencies" to pull in related JARs. - Once the libraries are added, use the
Type Hierarchyfeature (Ctrl+H on Windows/Linux, Cmd+H on Mac) to view all subclasses or implementors of your target class/interface.
This is perfect if you want to explore implementations in context and quickly add the relevant dependency to your project.
Key Note: Maven Central vs. Local Classpath
Remember, classpath searches only cover dependencies already in your project. Maven Central searches span every public JAR hosted in the repo—so you’ll find many more implementations, but you’ll need to use external tools to parse the class hierarchy.
内容的提问来源于stack exchange,提问作者Olivier Cailloux

