自定义插件无法从jar复制文件,因为协议是“jar”。 [英] Custom plugin can't copy files from jar because protocol is "jar"
问题描述
我正在为SonarQube开发一个插件,它必须做的部分是将文件从自身复制到SonarQube服务器的目录。我正在使用Apache Commons IO,并且发现 FileUtils.copyDirectory
运行得非常好(在使用main方法测试之后)。
麻烦来了从尝试获取我想要从插件本身的jar中复制的文件的目录。我试过这个:
I am developing a plugin for SonarQube, and part of what it must do is copy files from itself to the SonarQube Server's directory. I am using Apache Commons IO, and have found that FileUtils.copyDirectory
works wonderfully (after testing it with a main method).
The trouble comes from trying to get the directory of files that I want to copy from the jar of the plugin itself. I have tried this:
public File getSrcDir() {
URL inputUrl = getClass().getResource("/images");
File srcDir = FileUtils.toFile(inputUrl);
System.out.println(srcDir);
return srcDir;
}
当我在IDE(Eclipse)中的main方法中运行它时,它打印出 C:\ ... \ [项目目录] \ target \classes \ images
但是当我将插件安装到SonarQube中时在Sonar的plugins文件夹中的一个jar文件夹中,此方法打印出 null
。
我调查并发现 FileUtils。如果url参数具有File以外的协议,则toFile(URL url)
将返回null。所以我将这一行添加到我的方法中: System.out.println(inputUrl.getProtocol());
果然,在我的IDE中协议是文件,但是当我的插件安装时,协议是jar!
b $ b我花了最后一天倒在Stack Overflow和Google试图找到一种方法来处理有了这个,但无济于事。
我愿意尝试/重构任何东西。
我想做的就是让我的插件从内部复制一个目录它的罐子到SonarQube的服务器。
我怎么能这样做?
如果它是相关的,这是我项目中的目录(我想复制images文件夹及其中的所有内容):
When I run it in a main method in my IDE (Eclipse), it prints out C:\...\[Project Directory]\target\classes\images
But when I install the plugin into SonarQube as a jar folder in Sonar's "plugins" folder, this method prints out null
.
I investigated and found that FileUtils.toFile(URL url)
will return null if the url argument has a protocol other than "File". So I added this line to my method: System.out.println(inputUrl.getProtocol());
Sure enough, in my IDE the protocol is "file", but when my plugin is installed the protocol is "jar"!
I have spent the last day pouring over Stack Overflow and Google trying to find a way to deal with this, but to no avail.
I am willing to try/refactor anything.
All I want to do is make it so that my plugin copies a directory from within its jar to SonarQube's Server.
How can I do this?
In case it's relevant, here is the directory within my project (I want to copy the "images" folder and everything in it):
[Project Directory]
|
+--/src/main/resources
|
+--images
|
+--profiles
|
+--awards
| |
| +--Award1.png
| |
| +--Award2.png
|
+--projects
|
+--default.png
这里是要在SonarQube中复制的目录服务器:
[SONARQUBE_HOME] / war / sonar-server / images
And here is the directory to copy to in the SonarQube server:
[SONARQUBE_HOME]/war/sonar-server/images
推荐答案
当classpath中的资源(在本例中是图像或目录)来自jar时,它不再是物理文件
When resource (in this case images or directory) in classpath coming from jar, it is no longer a physical file
你需要修改代码所以它从 Stream
中读取这些资源而不是文件
You need to modify the code so that it reads those resources from Stream
instead of File
参见
- How to read file from classpath from
Stream
- Get a list of resources from classpath directory
这篇关于自定义插件无法从jar复制文件,因为协议是“jar”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!