Permalink
Browse files

Initial commit of custom Jenkins Plugin

  • Loading branch information...
0 parents commit 6f309fcb05093d91fddae431db50d670de3f74bb @marcelbirkner committed Aug 5, 2012
50 pom.xml
@@ -0,0 +1,50 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.jenkins-ci.plugins</groupId>
+ <artifactId>plugin</artifactId>
+ <version>1.424</version>
+ <!-- which version of Jenkins is this plugin built against? -->
+ </parent>
+
+ <groupId>de.mb</groupId>
+ <artifactId>nexus-metadata-plugin</artifactId>
+ <version>1.0</version>
+ <packaging>hpi</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-client</artifactId>
+ <version>1.12</version>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-rest-api-model</artifactId>
+ <version>2.0.6</version>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-custom-metadata-plugin-client</artifactId>
+ <version>1.0</version>
+ </dependency>
+ </dependencies>
+
+ <!-- get every artifact through repo.jenkins-ci.org, which proxies all the
+ artifacts that we need -->
+ <repositories>
+ <repository>
+ <id>repo.jenkins-ci.org</id>
+ <url>http://repo.jenkins-ci.org/public/</url>
+ </repository>
+ </repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>repo.jenkins-ci.org</id>
+ <url>http://repo.jenkins-ci.org/public/</url>
+ </pluginRepository>
+ </pluginRepositories>
+
+</project>
@@ -0,0 +1,178 @@
+package de.mb;
+import hudson.Extension;
+import hudson.Launcher;
+import hudson.model.BuildListener;
+import hudson.model.AbstractBuild;
+import hudson.model.AbstractProject;
+import hudson.tasks.BuildStepDescriptor;
+import hudson.tasks.Builder;
+import hudson.util.FormValidation;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.core.MediaType;
+
+import net.sf.json.JSONObject;
+
+import org.kohsuke.stapler.DataBoundConstructor;
+import org.kohsuke.stapler.QueryParameter;
+import org.kohsuke.stapler.StaplerRequest;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+
+/**
+ * Sample {@link Builder}.
+ *
+ * <p>
+ * When the user configures the project and enables this builder,
+ * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
+ * and a new {@link HelloWorldBuilder} is created. The created
+ * instance is persisted to the project configuration XML by using
+ * XStream, so this allows you to use instance fields (like {@link #name})
+ * to remember the configuration.
+ *
+ * <p>
+ * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
+ * method will be invoked.
+ *
+ * @author Kohsuke Kawaguchi
+ */
+public class HelloWorldBuilder extends Builder {
+
+ private final String name;
+
+ // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
+ @DataBoundConstructor
+ public HelloWorldBuilder(String name) {
+ this.name = name;
+ }
+
+ /**
+ * We'll use this from the <tt>config.jelly</tt>.
+ */
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
+ // This is where you 'build' the project.
+ // Since this is a dummy, we just say 'hello world' and call that a build.
+
+ listener.getLogger().println( getDescriptor().getNexusUrl() );
+ listener.getLogger().println( getDescriptor().getNexusUser() );
+ listener.getLogger().println( getDescriptor().getNexusPassword() );
+
+ ClientConfig config = new DefaultClientConfig();
+ Client client = Client.create(config);
+ WebResource service = client.resource( getDescriptor().getNexusUrl() );
+ listener.getLogger().println( service.path("artifactory").path("api").path("build").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
+
+ // This also shows how you can consult the global configuration of the builder
+ if (getDescriptor().getUseFrench())
+ listener.getLogger().println("Bonjour, "+name+"!");
+ else
+ listener.getLogger().println("Hello, "+name+"!");
+ return true;
+ }
+
+ // Overridden for better type safety.
+ // If your plugin doesn't really define any property on Descriptor,
+ // you don't have to do this.
+ @Override
+ public DescriptorImpl getDescriptor() {
+ return (DescriptorImpl)super.getDescriptor();
+ }
+
+ /**
+ * Descriptor for {@link HelloWorldBuilder}. Used as a singleton.
+ * The class is marked as public so that it can be accessed from views.
+ *
+ * <p>
+ * See <tt>src/main/resources/hudson/plugins/hello_world/HelloWorldBuilder/*.jelly</tt>
+ * for the actual HTML fragment for the configuration screen.
+ */
+ @Extension // This indicates to Jenkins that this is an implementation of an extension point.
+ public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
+ /**
+ * To persist global configuration information,
+ * simply store it in a field and call save().
+ *
+ * <p>
+ * If you don't want fields to be persisted, use <tt>transient</tt>.
+ */
+ private boolean useFrench;
+ private String nexusUrl;
+ private String nexusUser;
+ private String nexusPassword;
+
+ /**
+ * Performs on-the-fly validation of the form field 'name'.
+ *
+ * @param value
+ * This parameter receives the value that the user has typed.
+ * @return
+ * Indicates the outcome of the validation. This is sent to the browser.
+ */
+ public FormValidation doCheckName(@QueryParameter String value)
+ throws IOException, ServletException {
+ if (value.length() == 0)
+ return FormValidation.error("Please set a name");
+ if (value.length() < 4)
+ return FormValidation.warning("Isn't the name too short?");
+ return FormValidation.ok();
+ }
+
+ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
+ // Indicates that this builder can be used with all kinds of project types
+ return true;
+ }
+
+ /**
+ * This human readable name is used in the configuration screen.
+ */
+ public String getDisplayName() {
+ return "Say hello world";
+ }
+
+ @Override
+ public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
+ // To persist global configuration information,
+ // set that to properties and call save().
+ useFrench = formData.getBoolean("useFrench");
+ nexusUrl = formData.getString("nexusUrl");
+ nexusUrl = formData.getString("nexusUser");
+ nexusUrl = formData.getString("nexusPassword");
+
+ // ^Can also use req.bindJSON(this, formData);
+ // (easier when there are many fields; need set* methods for this, like setUseFrench)
+ save();
+ return super.configure(req,formData);
+ }
+
+ /**
+ * This method returns true if the global configuration says we should speak French.
+ *
+ * The method name is bit awkward because global.jelly calls this method to determine
+ * the initial state of the checkbox by the naming convention.
+ */
+ public boolean getUseFrench() {
+ return useFrench;
+ }
+ public String getNexusUrl() {
+ return nexusUrl;
+ }
+ public String getNexusUser() {
+ return nexusUser;
+ }
+ public String getNexusPassword() {
+ return nexusPassword;
+ }
+ }
+}
+
Oops, something went wrong.

0 comments on commit 6f309fc

Please sign in to comment.