Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-40514

Typing Return Value of readMavenPom Causes ClassCastException

      import org.apache.maven.model.Model
      
      Model mavenProject = readMavenPom(file: pom)
      

      The above groovy script causes a ClassCastException when executing a Pipeline script:

      [Pipeline] echo
      Marking build as failure due to the following error: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.foo:bar-api:jar:0.6.1-201612151515.0777507' with class 'org.apache.maven.model.Model' to class 'org.apache.maven.model.Model'
      [Pipeline] stage
      

          [JENKINS-40514] Typing Return Value of readMavenPom Causes ClassCastException

          Ditto with Jenkins 2.19.1, Pipeline 2.4 and Pipeline Utility Steps 1.2.2

          It used to work though with 2.19.1, Pipeline 2.4 and Pipeline Utility Steps 1.1.6

          Sascha Volkenandt added a comment - Ditto with Jenkins 2.19.1, Pipeline 2.4 and Pipeline Utility Steps 1.2.2 It used to work though with 2.19.1, Pipeline 2.4 and Pipeline Utility Steps 1.1.6

          Andrew Bayer added a comment -

          This sounds like http://stackoverflow.com/questions/34068168/groovy-binding-cannot-cast-object-with-class-custompackage-customclass-to-cla - could you try doing def mavenProject = readMavenPom(file: pom) instead of Model mavenProject ... and see if that helps?

          Andrew Bayer added a comment - This sounds like http://stackoverflow.com/questions/34068168/groovy-binding-cannot-cast-object-with-class-custompackage-customclass-to-cla  - could you try doing def mavenProject = readMavenPom(file: pom) instead of Model mavenProject ... and see if that helps?

          Jesse Glick added a comment -

          Perhaps some other plugin is also loading maven-core. Probably maven-plugin.

          I would not recommend importing org.apache.maven.model.* classes; use dynamic typing.

          Or just avoid the readMavenPom step altogether, and use something like

          def version = sh script: '''
          mvn help:evaluate -Dexpression=project.version | perl -n -e 'print $1 if /^([0-9.]+(-SNAPSHOT)?)$/'
          ''', returnStdout: true
          

          Jesse Glick added a comment - Perhaps some other plugin is also loading maven-core . Probably maven-plugin . I would not recommend importing org.apache.maven.model.* classes; use dynamic typing. Or just avoid the readMavenPom step altogether, and use something like def version = sh script: ''' mvn help:evaluate -Dexpression=project.version | perl -n -e 'print $1 if /^([0-9.]+(-SNAPSHOT)?)$/' ''', returnStdout: true

          I have the same problem. def mavenProject works, but Model mavenProject throws the same exception.

          Michael Düsterhus added a comment - I have the same problem. def mavenProject works, but Model mavenProject throws the same exception.

          Same too. This does not help writing libraries with proper type checks. Anyone has an idea for a fix or a workaround?

          Fabrice Pipart added a comment - Same too. This does not help writing libraries with proper type checks. Anyone has an idea for a fix or a workaround?

          Jesse Glick added a comment -

          My advice remains to avoid these two steps.

          Jesse Glick added a comment - My advice remains to avoid these two steps.

          Jesse Glick added a comment -

          Filed a PR to formally deprecate these steps.

          Jesse Glick added a comment - Filed a PR to formally deprecate these steps.

          Hi!

          Which two steps are you refering to?

          I definitely prefer

          import org.apache.maven.model.Model Model mavenProject = readMavenPom(file: pom)
          

          over

          mvn help:evaluate -Dexpression=project.version | perl -n -e 'print $1 if /^([0-9.]+(-SNAPSHOT)?)$/' ''', returnStdout: true
          

          It is more reliable, more readable and does not require (like any maven plugin execution) a lot of network activity in order to download / check all dependencies.
          I have to admit, though, that I am OK to keep dynamic typing. It is just that it looks more like a bug that you avoid than something that you really want.

          Fabrice Pipart added a comment - Hi! Which two steps are you refering to? I definitely prefer import org.apache.maven.model.Model Model mavenProject = readMavenPom(file: pom) over mvn help:evaluate -Dexpression=project.version | perl -n -e 'print $1 if /^([0-9.]+(-SNAPSHOT)?)$/' ''', returnStdout: true It is more reliable, more readable and does not require (like any maven plugin execution) a lot of network activity in order to download / check all dependencies. I have to admit, though, that I am OK to keep dynamic typing. It is just that it looks more like a bug that you avoid than something that you really want.

          Antoine Tran added a comment -

          Antoine Tran added a comment - Could be closed in favor of https://issues.jenkins.io/browse/JENKINS-70717?

          Jesse Glick added a comment -

          Probably.

          Jesse Glick added a comment - Probably.

            Unassigned Unassigned
            timeapen Tim Eapen
            Votes:
            3 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated: