我正在使用 JavaPoet 生成一些类。全部正确!但我需要将该代码生成到 src 文件夹中,而不是到 target 或 bin 或 classes 文件夹中
现在,我正在使用以下代码编写类:
URL[] urls = ((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs();
File root = new File(urls[0].toURI()); // this is 'target' folder
File source = new File(root, packageName.replace(".", "/").concat("/").concat(className).concat(".java"));
FileWriter writer = new FileWriter(source);
JavaFile.builder(packageName, spec).build().writeTo(writer);