Jenkins插件开发 - 持久性

时间:2013-10-16 09:26:12

标签: plugins jenkins persistence jelly

我还在学习插件开发。这是我的第一个。

我想保留我的插件的配置,但它不起作用。

请你告诉我,我做错了什么?

我已尝试对该过程进行调试,从添加插件到作业,直到保存作业配置。

我发现,在描述符的load()方法中,没有找到xml文件!

它正在寻找的路径类似于:c:\ users \ Peter \ workspace \ r-script。\ work \ whatEverDir \ xy.xml

我不认为。\部分导致找不到配置文件,但由于它是一个生成这条路径的Jenkins类,我不打赌它。虽然系统可能试图在这里首先创建它。

提前致谢!

Scriptrunner.jelly               

    <f:block>
        <f:entry title="RScript" field="command">
            <f:textarea  style="width:99%" />
        </f:entry>
        </f:block>

    <f:entry field="installedPackages" title="Installed packages">
        <f:select style="width:40%" /> 
    </f:entry> 

    <f:entry field="mirrors" title="Choose a mirror">
        <f:select style="width:40%" />
    </f:entry>

    <f:entry>
            <f:repeatableProperty field="availablePackages" minimum="1"/>
    </f:entry>            

AvailablePackage.jelly

         

<f:entry field="availablePackages">
    <f:select style="width:40%" />
    <f:repeatableDeleteButton /> 
</f:entry>

ScriptRunner.java

public class ScriptRunner extends Builder {
 private static final String fileExtension = ".R";
 private ArrayList<AvailablePackage> availablePackages;
 private String command;
 private String chosenMirror;
 private List<String> mirrorList = new ArrayList<String>();

 @DataBoundConstructor
 public ScriptRunner(String command, ArrayList<String> installedPackages,     ArrayList<String> mirrors, ArrayList<AvailablePackage> availablePackages) {
this.chosenMirror = mirrors.get(0);
this.availablePackages = availablePackages;
this.command = command;
  }

  public final String getCommand() {
  return command;
  }

  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
    BuildListener listener) throws InterruptedException {
  return perform(build, launcher, (TaskListener) listener);
  } 

  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
    TaskListener listener) throws InterruptedException {
FilePath workSpace = build.getWorkspace();
FilePath rScript = null;
if (workSpace == null) {
    try {
    throw new NoSuchFileException("Workspace could not be set!");
    } catch (NoSuchFileException e) {
    e.printStackTrace();
    }
}
try {
    try {
    String fullScript;
    if (command.contains("options(repos=structure(")) {
            fullScript = PackagesManager.singleton().createFullScript(availablePackages, "", command);
    } else {
        fullScript = PackagesManager.singleton().createFullScript(availablePackages, chosenMirror, command);
    }
    rScript = workSpace.createTextTempFile("RScriptTemp",
        getFileExtension(), fullScript, false);
    } catch (IOException e) {
    Util.displayIOException(e, listener);
    e.printStackTrace(listener.fatalError(Messages
        .CommandInterpreter_UnableToProduceScript()));
    return false;
    }

    boolean successfullyRan = false;
    try {
    EnvVars envVars = build.getEnvironment(listener);
    for (Map.Entry<String, String> e : build.getBuildVariables()
        .entrySet()) {
        envVars.put(e.getKey(), e.getValue());
    }

    if (launcher.launch().cmds(buildCommandLine(rScript))
        .envs(envVars).stdout(listener).pwd(workSpace).join() == 1) {
        successfullyRan = true;
    }
    } catch (IOException e) {
    Util.displayIOException(e, listener);
    e.printStackTrace(listener.fatalError(Messages
        .CommandInterpreter_CommandFailed()));

    }
    return successfullyRan;
} finally {
    try {
    if (rScript != null) {
        rScript.delete();
    }
    } catch (IOException e) {
    Util.displayIOException(e, listener);
    e.printStackTrace(listener.fatalError(Messages
        .CommandInterpreter_UnableToDelete(rScript)));
    } catch (Exception e) {
    e.printStackTrace(listener.fatalError(Messages
        .CommandInterpreter_UnableToDelete(rScript)));
    }
}
}

public String[] buildCommandLine(FilePath script) {
return new String[] { "Rscript", script.getRemote() };
}

protected String getFileExtension() {
return fileExtension;
}

public List<String> getMirrorList() {
return mirrorList;
}

public void setMirrorList(List<String> mirrorList) {
this.mirrorList = mirrorList;
}

public String getChosenMirror() {
return chosenMirror;
}

public void setChosenMirror(String chosenMirror) {
this.chosenMirror = chosenMirror;
}

public ArrayList<AvailablePackage> getAvailablePackages() {
return availablePackages;
}

@Override
public ScriptBuildStepDescriptorImplementation getDescriptor() {
    return (ScriptBuildStepDescriptorImplementation)super.getDescriptor();
}

@Extension
public static class ScriptBuildStepDescriptorImplementation extends
    BuildStepDescriptor<Builder> {

private boolean showInstalled;
private String command;
private String chosenMirror;
private ArrayList<AvailablePackage> availablePackages;

public ScriptBuildStepDescriptorImplementation() {
    load();
}

 @Override
    public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
        req.bindJSON(this, formData);
        save();
        return super.configure(req,formData);
    }

@Override
public String getDisplayName() {
    return "Advanced R script runner";
}

@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
    return true;
}

public ListBoxModel doFillInstalledPackagesItems() {
    ListBoxModel mirrors = new ListBoxModel();
    Set<String> mirrorsList = PackagesManager.singleton()
        .getInstalledPackages();
    for (String entry : mirrorsList) {
    mirrors.add(entry);
    }
    return mirrors;
}

public ListBoxModel doFillAvailablePackagesItems() {
    ListBoxModel packages = new ListBoxModel();
    List<String> packageList = PackagesManager.singleton().getAvailablePackages();
    Set<String> alreadyInstalled = PackagesManager.singleton().getInstalledPackages();
    for (String entry : packageList) {
    if (!alreadyInstalled.contains(entry)) {
    packages.add(entry);
    }
    }
    return packages;
}

public ListBoxModel doFillMirrorsItems() {
    ListBoxModel mirrors = new ListBoxModel();
    String[] mirrorsList = MirrorManager.singleton().getMirrors();
    int selected = 34;
    for (int i = 0; i < mirrorsList.length; i++) {
    String[] splitCurrent = mirrorsList[i].split(" - ");
    if (chosenMirror != null && chosenMirror.equals(splitCurrent[1])) {
        selected = i;
    }
    mirrors.add(splitCurrent[1], splitCurrent[0]);
    }   
    mirrors.get(selected).selected = true;
    return mirrors;
}

public boolean getShowInstalled() {
    return showInstalled;
}

public void setShowInstalled(boolean showInstalled) {
    this.showInstalled = showInstalled;
}

public String getCommand() {
    return command;
}

public void setCommand(String command) {
    this.command = command;
}

 public String getChosenMirror() {
    return chosenMirror;
 }

   public void setChosenMirror(String chosenMirror) {
     this.chosenMirror = chosenMirror;
  }
    }
}

AvailablePackage.java

public class AvailablePackage extends AbstractDescribableImpl<AvailablePackage> {
private String name;

@DataBoundConstructor
public AvailablePackage(String availablePackages) {
this.name = availablePackages;
}

public String getName() {
return name;
}

@Override
public DescriptorImpl getDescriptor() {
    return (DescriptorImpl)super.getDescriptor();
}

@Extension
public static class DescriptorImpl extends Descriptor<AvailablePackage> {
private String name;

public DescriptorImpl() {
    load(); 
}

 @Override
 public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
     req.bindJSON(this, formData);
     save();
     return super.configure(req,formData);
    }

    public ListBoxModel doFillAvailablePackagesItems() {
        return PackagesManager.singleton().getAvailablePackagesAsListBoxModel(name);
    }

    @Override
    public String getDisplayName() {
        return "";
    }

    public String getName() {
        return name;
    }
}

}

抱歉代码格式化! stackoverflow代码发布的第一个计时器。

1 个答案:

答案 0 :(得分:0)

我想你可能需要评论这一行

@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
    req.bindJSON(this, formData);
    save();
    //return super.configure(req,formData);
    return true;
}

因为它会再次保存,但没有字段。

找到Jenkins插件示例的好地方在https://github.com/jenkinsci