Jenkins groovy 正则表达式匹配字符串:错误:java.io.NotSerializableExceptio

2024-08-24Java开发问题
15

本文介绍了Jenkins groovy 正则表达式匹配字符串:错误:java.io.NotSerializableException:java.util.regex.Matcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试从 groovy 中的正则表达式中获取匹配的字符串.匹配的字符串打印到控制台没有问题,但是当我尝试在 git 命令中使用匹配的字符串时,我收到以下错误:

I'm trying to get the matched string from a regex in groovy. The matched string prints to the console without problems, but when I try use the matched string in a git command I get the following error:

Err: Incremental Build failed with Error: java.io.NotSerializableException: java.util.regex.Matcher

代码如下:

                def binaryName = "298_application_V2_00_Build_07.hex"

                def matches = (binaryName =~ /(V)(d+)(_)(d+)(_)(Build)(_)(d+)/)
                versionTag = ""+matches[0].getAt(0)                 
                echo "${matches}"
                echo "$versionTag"
                bat("git tag $versionTag")
                bat("git push origin --tags")

如何从正则表达式中获取匹配的字符串?

How can I get the matched string from the regex?

推荐答案

这个问题是Jenkins的CPS,它将所有管道执行序列化以存储为可恢复状态.

This problem is caused by Jenkins' CPS, which serializes all pipeline executions to store as resumable state.

对不可序列化方法的调用必须包装在一个用 @NonCPS 注释的方法中:

Calls to non-serializable methods have to be wrapped in a method annotated with @NonCPS:

@NonCPS
String getVersion(String binaryName) {
  def matches = (binaryName =~ /(V)(d+)(_)(d+)(_)(Build)(_)(d+)/)
  versionTag = ""+matches[0].getAt(0)
  versionTag
}

现在可以从您的管道中调用此方法.如果您的 Jenkins master 在执行此方法期间重新启动,它将完全运行它 - 在许多情况下,例如您的,绝对没有问题.

this method can now be called from your pipeline. In case your Jenkins master restarts during execution of this method, it would just run through it completely - which is in many cases, such as yours, absolutely no problem.

这篇关于Jenkins groovy 正则表达式匹配字符串:错误:java.io.NotSerializableException:java.util.regex.Matcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13