Proper way of showing shared library output in job console

Hi,

I’m creating a shared library to use with a pipeline.

Is there a way to view a message generated within the shared library in the job console output?

thanks in advance

When you have access to Jenkins steps, just call the echo step. It is one of the only steps safe to call in any context, but you need to have access to the steps (which are only directly accessible in pipeline scripts)

Thanks @mlasevich

I did this based on your recommendation

    def getApplicationNameFromPOM(pomContent){
        try{
            def slurper = new XmlSlurper()
            def output = slurper.parseText(pomContent)
            return output.'name'.text()
        }catch(Exception e){
            script.echo("getApplicationNameFromPOM: " + e)
        }
    }

FWIW, There is actually a readMavenPom step in Jenkins util steps plugin that can make this sort of thing a lot simpler…

or not…

Reading the documentation for it, it says to avoid using that step and suggests using sh and running mvn directly - it makes sense as sometimes poms are more tricky to resolve than reading pure XML and maven already knows how to do that.

(I am going to leave this comment here as an FYI)

Initially I was using readMavenPom which returns a model, but I faced some issues with the tests, so I decided to go with readFile.

Thanks for your help :slight_smile:

Another trick of the trade - if you are using this from classes (where the steps are not directly available) you can avoid passing the script all over the place by creating a proxy script class - for example src/jenkins/Steps.groovy - leave this file completely empty - then in your class you can add something like

import jenkins.Steps

class RandomClass{

  // get an instance of Steps proxy
  Steps  getSteps(){
    return new Steps()
  }

 // anywhere in the class code
 def someMethod(){
    steps.echo "Hello!"
    steps.sh "some shell command"
    //etc
 }
}

Good tip, thanks, I am passing Script all over the place. :slight_smile: