[英]Download a Maven artifact and install into a specific local repository (not .m2)
[英]Search and list .m2 (local) maven repository
对于一个项目,我想搜索本地 maven repo (.m2) 并列出工件。 我怎么能在 Java 中做到这一点? 我找到了 https://github.com/apache/maven-indexer并检查了我是否可以使用它来列出我的本地 maven 存储库。 但是还是解决不了。 如果您对我可以使用的简单库有任何想法,请提出建议?
提前致谢。 !
考虑使用Files.walkFileTree(Path start, FileVisitor<? super Path> visitor)
class MyM2Visitor extends SimpleFileVisitor<Path> {
private final Path root;
public MyM2Visitor(Path root) {
this.root = root;
}
//implement rest as necessary
}
//later...
//Current user's .m2 directory:
Path m2 = Path.of(System.getProperty("user.home"), ".m2");
MyM2Visitor visitor = new MyM2Visitor(m2);
try {
Files.walkFileTree(m2, visitor);
} catch(IOException e) {
e.printStackTrace();
}
另请参阅: SimpleFilevisitor JavaDoc
您应该自定义访问者以在遇到目录或文件时采取适当的操作,例如:
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
logger.trace("File: {}", file);
int count = file.getNameCount();
Path versionPath = file.getParent();
String versionAsString = versionPath.getFileName();
//or:
versionAsString = file.getName(count-2).toString();
String artifactId = file.getName(count-3).toString();
Path groupIdPath = file.getParent().getParent().getParent();
Path relativeToM2 = root.relativize(groupIdPath);
String groupId = relativeToM2.toString().replace(File.separatorChar, '.');
//do other things
return super.visitFile(file, attrs);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.