2

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;
}

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

And here is the directory to copy to in the SonarQube server: [SONARQUBE_HOME]/war/sonar-server/images

James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • A classpath resource is not per definition a local disk file system resource. All with all I believe that you're going in completely the wrong direction as to solving the original problem for which you incorrectly thought that *this* is the right solution. Likely related: http://stackoverflow.com/questions/8885201/uploaded-image-only-available-after-refreshing-the-page/8889096#8889096 – BalusC Jul 29 '13 at 16:08

1 Answers1

1

When resource (in this case images or directory) in classpath coming from jar, it is no longer a physical file

You need to modify the code so that it reads those resources from Stream instead of File


See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thanks, this worked, although I had to follow your links a ways and experiment a bunch. I ended up using code from the answer jabber gave to this question: http://stackoverflow.com/questions/1386809/copy-directory-from-a-jar-file – James Dunn Jul 30 '13 at 00:36