Tuesday, July 23, 2013

Writing Sample plugin for Jenkins


There are so many plugins available for Jenkins which will cater most of your needs. But there are chances where you have to write your own plugin.
Jenkins Plugin should be developed in java and we have to create a .hpi file out of that. But don't be panic about this as this can be done automatically. Process is as follows.

Step 0: Set ~/.m2/settings.xml as specified in https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial

Step 1: On a linux machine
mvn -U org.jenkins-ci.tools:maven-hpi-plugin:create


Step 2: This will give create sample code. Now
cd newly-created-directory
mvn package

Most probably you will get an error saying 

libjna-java mvn test failed with java.lang.UnsatisfiedLinkError: com.sun.na.Native.open(Ljava/lang/String;)J 
at com.sun.jna.Native.open(Native Method)  
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:236)  
at com.sun.jna.Library$Handler.<init>(Library.java:140)  
at com.sun.jna.Native.loadLibrary(Native.java:366)  
at com.sun.jna.Native.loadLibrary(Native.java:351)  
at hudson.util.jna.GNUCLibrary.<clinit>(GNUCLibrary.java:105)

This is because of a known issue.

Workaround would be add the following lines to your project's pom.xml inside the dependencies node and if not existing create dependencies node

<dependency>
               <groupId>net.java.dev.jna</groupId>
               <artifactId>jna</artifactId>
               <version>3.2.2</version> 
</dependency>

Step 4: After doing that use following command
mvn install
Now you can see a hpi file created in target directory.

Step 5: Run the newly created plugin using command
export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n"
mvn hpi:run


Step 6: Open your favorite Browser and go to http://127.0.0.1:8080 which will show you following screen

Step 7: Create a new Job and in build steps you can see "Say Hello world" as a Task. Add this with some name.

Step 8: Now build this project and you can see Hello world output as shown below

~ Yananarayana Dande


No comments:

Post a Comment