Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkinsfile - How do I access custom properties in my pom.xml?

Let's say I have a custom property in my pom.xml set like this:

<properties>
 <app>com.myProject.app</app>
</properties>

How can I access it in my jenkinsfile?

This:

def pom = readMavenPom file: 'pom.xml'
def appName = pom.app

returns

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field org.apache.maven.model.Model app

Thanks in advance!

like image 666
LordHW4 Avatar asked Nov 16 '17 07:11

LordHW4


People also ask

How use properties file in POM xml?

properties file in the maven project folder src/main/resources`. In pom. xml, add resources inside the building element and set filtering=true. filtering enables replacement variables from a project, or system to the resource files.

How do you get Pom properties at runtime?

Get the pom properties using the properties plugin This will write all properties defined in the properties section of the pom into the version. properties file. But this has its downside. If you want a property to appear in the file, you will have to define it in the properties section.

What is configuration in POM xml?

The pom. xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc.

How does Jenkinsfile use Jenkins credentials?

From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click Manage Jenkins > Manage Credentials. Under Stores scoped to Jenkins on the right, click on Jenkins. Under System, click the Global credentials (unrestricted) link to access this default domain. Click Add Credentials on the left.


1 Answers

I know two approaches:

  1. Use properties-maven-plugin to write the properties to a file. Use readProperties in the Jenkinsfile to read the properties.
    Works only if properties aren't needed until after Maven ran.
    Also, with the right circumstances, the properties file may be the stale one from a previous run, which is insiduous because the property values will be right anyway 99.9% of the time.
  2. Use pom = readMavenPom 'path/to/pom.xml'. Afterwards, access the property like this: pom.properties['com.myProject.app'].

I like approach 2 much better: No extra plugin configuration in the POM, no file written, less sequencing constraints, less fragilities.

like image 144
toolforger Avatar answered Sep 18 '22 14:09

toolforger