0

我正在编写一个简单的插件,并且被迫创建一个显示页面(index.jelly)的 RootAction,并且需要一些额外的值来确认然后执行该方法。

我的问题是,index.jelly 文件总是显示在空白窗口上。但我确实需要像往常一样将它包含在主表的 Jenkins 模板中。

似乎无法弄清楚为什么会这样。

有任何想法吗?

重启JksLink.java

package org.jenkinsci.plugins.tomcat_app_restart;

import hudson.Extension;
import hudson.model.ManagementLink;

/**
 *
 *
 * @author [...]
 */
@Extension
public class RestartJksLink extends ManagementLink {
    @Override
    public String getIconFileName() {
        return "/plugin/tomcat-app-restart/images/restart.png";
    }

    @Override
    public String getUrlName() {
        return "jksrestart";
    }

    @Override
    public String getDescription() {
       return "Restart your Jenkins-Application on Tomcat";
    }

    public String getDisplayName() {
        return "Restart Jenkins-App on Tomcat";
    }
}

重启JksRootAction.java

package org.jenkinsci.plugins.tomcat_app_restart;

import java.io.IOException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;

import jenkins.model.Jenkins;
import hudson.Extension;
import hudson.model.RootAction;
import hudson.util.FormValidation;

@Extension
public class RestartJksRootAction implements RootAction {
    public String getDisplayName() {
        return "Restart Jenkins on Tomcat";
    }

    public String getIconFileName() {
        if (!Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER)) {
          return null;
        }

        if (!Jenkins.getInstance().getLifecycle().canRestart()) {
          return null;
        }

        return "/plugin/tomcat-app-restart/images/restart.png";
    }

    public String getUrlName() {
        return "jksrestart";
    }

    public FormValidation doJksRestart() {
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication ("admin", "admin".toCharArray());
            }
        });

        URL url;
        try {
            url = new URL("http://localhost:8888/manager/text/start?path=/jenkins");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            System.out.println("" + connection.getResponseMessage());

            return FormValidation.ok("Success");
        } catch (IOException e) {

            return FormValidation.error("Client error: " + e.getMessage());
        }
    }
}

index.jelly 里面:resources.org.jenkinsci.plugins.tomcat_app_restart.RestartJksRootAction

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
  <f:validateButton
   title="${%Restart Jenkins}" progress="${%Restarting...}"
   method="JksRestart" with="" />
</j:jelly>

感谢你们!

我是 jenkins 插件开发的新手,这将有助于我理解。

亲切的问候。

4

2 回答 2

1

这个演示(rootaction-example-plugin)帮助很大。你可以阅读它。

https://github.com/gustavohenrique/jenkins-plugins/tree/master/rootaction-example-plugin

于 2014-09-27T06:27:34.303 回答
1

<l:main-panel>标签和<l:layout norefresh="true">标签添加到 index.jelly 文件中。

并包括侧面板:

  • 将构建传递给 Action(通过构造函数的参数)
    • 可以从继承自 BuildStepCompatibilityLayer 类(通过扩展发布者)的 perform 方法的参数中检索构建。
  • 在 Action 类中创建一个 getBuild() 方法
  • <st:include it="${it.build}" page="sidepanel.jelly" />使用构建添加标签

果冻示例(index.jelly):

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
    <l:layout norefresh="true">
        <st:include it="${it.build}" page="sidepanel.jelly" />
        <l:main-panel>
            <f:validateButton title="${%Restart Jenkins}" progress="${%Restarting...}" method="JksRestart" with="" />
        </l:main-panel>
    </l:layout>
</j:jelly>

Java Action 类示例:

package tryPublisher.tryPublisher;

import hudson.model.Action;
import hudson.model.AbstractBuild;


public class ExampleAction implements Action {
    AbstractBuild<?,?> build;

    public ExampleAction(AbstractBuild<?,?> build) {
        this.build = build;
    }

    @Override
    public String getIconFileName() {
        return "/plugin/action.png";
    }

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

    @Override
    public String getUrlName() {
        return "ExampleActionUrl";
    }

    public AbstractBuild<?,?> getBuild() {
        return this.build;
    }
}

Java Publisher 类示例:

package tryPublisher.tryPublisher;

import java.io.IOException;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;

public class ExamplePublisher extends Publisher {

    @Override
    public BuildStepMonitor getRequiredMonitorService() {
        return BuildStepMonitor.NONE;
    }

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

        build.getActions().add(new ExampleAction(build));

        return true;
    }

}

.jelly 文件必须在插件项目的正确资源映射中。在与实现 Action 的 Java 类的名称同名的映射中。.jelly 的名称也很重要。

于 2014-12-10T11:19:56.457 回答