1

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

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

你能告诉我,我做错了什么吗?

我已经尝试调试该过程,从将插件添加到作业开始,直到保存作业配置。

我发现,在描述符的 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>            

可用包.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;
  }
    }
}

可用包.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 代码发布的第一个计时器。

4

1 回答 1

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

于 2013-10-16T11:19:52.347 回答