Skip to content

[JENKINS-53903] 'unable to resolve class hudson.util.Secret' exception in groovy script in ANT build.xml #322

@jenkins-infra-bot

Description

@jenkins-infra-bot

I am using Jenkins and ANT build to automate our build and release process.
I am using 'hudson.util.Secret' API to encrypt and decrypt passwords in Jenkins.
I have encrypted all passwords and kept in a property file using the API. Now, during the build process, I need to decrypt the password dynamically.
For that, I decided to use groovy script for decryption process (please refer the below code) in ant build.xml file.

Code

import hudson.util.Secret
// To decrypt Old Password
def password = Secret.fromString("{AQAAABAAAAAQAoxLF+PLmbaD1FJiLZznGOZ4pEJI4hnXHRjXVkLBrFM=}")
def decPassword = password.getPlainText()
println("Decrypted Password = "+decPassword)

The above code is working fine in Jenkins 'Script Console'. But while using the same in ANT build.xml file as groovy script, 'unable to resolve class hudson.util.Secret' exception is thrown .

Code in Ant build.xml




<script language="groovy" classpath = "C:/jenkins/war/WEB-INF/lib/groovy-all-2.4.11.jar">
import hudson.util.Secret
// To decrypt Old Password
def password = Secret.fromString("{AQAAABAAAAAQAoxLF+PLmbaD1FJiLZznGOZ4pEJI4hnXHRjXVkLBrFM=}")
def decPassword = password.getPlainText()
println("Decrypted Password = "+decPassword)
</script>

Exception

BUILD FAILED
C:\tools\my_samples\sample_build.xml:4: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 2: unable to resolve class hudson.util.Secret
@ line 2, column 2.
import hudson.util.Secret
^

1 error

How to resolve 'unable to resolve class hudson.util.Secret' exception in groovy script in ANT build.xml?

Any other code change is needed in buid.xml file?

Thanks in advance.

 


Originally reported by jayram, imported from: 'unable to resolve class hudson.util.Secret' exception in groovy script in ANT build.xml
  • assignee: armfergom
  • status: Open
  • priority: Major
  • component(s): ant-plugin, groovy-plugin
  • label(s): ant, groovy, jenkins
  • resolution: Unresolved
  • votes: 0
  • watchers: 1
  • imported: 20251216-182730
Raw content of original issue

I am using Jenkins and ANT build to automate our build and release process. I am using 'hudson.util.Secret' API to encrypt and decrypt passwords in Jenkins. I have encrypted all passwords and kept in a property file using the API. Now, during the build process, I need to decrypt the password dynamically. For that, I decided to use groovy script for decryption process (please refer the below code) in ant build.xml file.

Code

import hudson.util.Secret // To decrypt Old Password def password = Secret.fromString("{AQAAABAAAAAQAoxLF+PLmbaD1FJiLZznGOZ4pEJI4hnXHRjXVkLBrFM=}") def decPassword = password.getPlainText() println("Decrypted Password = "+decPassword)

The above code is working fine in Jenkins 'Script Console'. But while using the same in ANT build.xml file as groovy script, 'unable to resolve class hudson.util.Secret' exception is thrown .

Code in Ant build.xml

<?xml version="1.0" encoding="windows-1252" ?> <project xmlns="antlib:org.apache.tools.ant" name="Sample Build XML" default="main" basedir="."> <target name="main"> <script language="groovy" classpath = "C:/jenkins/war/WEB-INF/lib/groovy-all-2.4.11.jar"> import hudson.util.Secret // To decrypt Old Password def password = Secret.fromString("{AQAAABAAAAAQAoxLF+PLmbaD1FJiLZznGOZ4pEJI4hnXHRjXVkLBrFM=}") def decPassword = password.getPlainText() println("Decrypted Password = "+decPassword) </script> </target> </project>

Exception

BUILD FAILED C:\tools\my_samples\sample_build.xml:4: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 2: unable to resolve class hudson.util.Secret @ line 2, column 2. import hudson.util.Secret ^

1 error

How to resolve 'unable to resolve class hudson.util.Secret' exception in groovy script in ANT build.xml?

Any other code change is needed in buid.xml file?

Thanks in advance.

 

  • environment: Jenkins version 2.64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions