fmanaa
fmanaa

Reputation: 184

Plugin cannot find Jenkins static resource

This plugin needs to read an xml data store and parse certain elements of it

    File file = new File(Functions.getResourcePath()+"/plugin/company-deploy-jenkins-plugin/release.xml");
    boolean fileExists = file.exists();
    if (fileExists) {
        LOGGER.info("Found the file");
    }
    Document document = builder.parse(new FileInputStream(file));

However, I can't seem to be able to access the file. if(fileExists) evaluates to false although the file is available at http://%JenkinsHost%:%Port%/plugin/company-deploy-jenkins-plugin/release.xml and the file is available locally directly under the src/main/webapp folder.

I am trying to follow KK's advice on handling Jenkins static resources as described here: http://jenkins-ci.361315.n4.nabble.com/ANN-Referring-to-static-resources-from-your-plugin-td4490655.html.

Upon checking the file path via file.toString(), I get /static/4874107b/plugin/company-deploy-jenkins-plugin/release.xml.

Doing any work on the object document returns a null pointer exception with message (No such file or directory).

Any ideas?

Upvotes: 1

Views: 1736

Answers (2)

Mig82
Mig82

Reputation: 5478

You can use jenkins.model.Jenkins to assist you:

import jenkins.model.Jenkins;
...
String releaseXmlUrl = Jenkins.getInstance().getRootUrl() + "plugin/company-deploy-jenkins-plugin/release.xml";

Or if you're attempting this from a Descriptor in your plugin, you can make this more resilient to changes in the short-name of your plugin like so:

import jenkins.model.Jenkins;
...
String releaseXmlUrl = Jenkins.getInstance().getRootUrl() + "plugin/" + getPlugin().getShortName() + "/release.xml";

Upvotes: 0

Work
Work

Reputation: 73

You can try this:

Jenkins.getInstance().getPlugin("plugin-name").getWrapper().baseResourceURL.getFile() 

--> this will return the /src/main/webapp location

From this location, you will be able to find your file.

Upvotes: 1

Related Questions