Search code examples
hudsonjenkinsjelly

Path functions available to Jelly script?


I'd like to be able to do things like separate the directory and file name from a full path in Hudson/Jenkins's jelly script.

For example if I have /dir1/dir2/dir3/file.ext I'd like to (in jelly script) get access to /dir1/dir2/dir3 and file.ext.

Are the java io functions like getPath() and getName() available to jelly script?


Solution

  • Dion Gillard's Jelly: Executable XML deck was really helpful in sorting this out. From the slides I learned about the invoke (and invokeStatic) tags which were exactly what I needed. The Apache FilenameUtils class has some very nice static methods for dealing with filenames and it's included with Hudson.

    <j:jelly xmlns:j="jelly:core">
      <j:set var="fullpath" value="/dir1/dir2/dir3/file.ext"/>
    
      <!-- get the path without the filename -->
      <j:invokeStatic var="justpath" method="getPath" className="org.apache.commons.io.FilenameUtils">
        <j:arg value="${fullpath}"/>
      </j:invokeStatic>
    
      <!-- get just the filename -->
      <j:invokeStatic var="justname" method="getName" className="org.apache.commons.io.FilenameUtils">
        <j:arg value="${fullpath}"/>
      </j:invokeStatic>
    </j:jelly>
    

    In the example above, justpath will be set to /dir1/dir2/dir3/ and justname will be set to file.ext.