Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automatically migrate Maven type Job to free style type in batch

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to automatically migrate Maven type Job to free style type". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Why migrate?

Recently conducted an upgrade exercise of Jenkins from 1.x to 2.x.

The latest version of Jenkins2 can only run under the JDK8 or JDK11 version, and the JDK version I use is JDK8

When the JDK version of the Maven Job,Job configuration is JDK7, the build reports an error

$/ usr/local/java/bin/java-cp / data/jenkins/maven31-agent.jar:/usr/local/maven/boot/plexus-classworlds-2.5.2.jar:/usr/local/maven/conf/logging jenkins.maven3.agent.Maven31Main / usr/local/maven/ data/jenkins/slave.jar / data/jenkins/maven31-interceptor.jar / data/jenkins/maven3-interceptor-commons.jar 45631Exception in thread "main" java.lang.UnsupportedClassVersionError: hudson/remoting/Launcher: Unsupported major.minor Version 52.0at java.lang.ClassLoader.defineClass1 (Native Method) at java.lang.ClassLoader.defineClass (ClassLoader.java:800) at java.security.SecureClassLoader.defineClass (SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass (URLClassLoader.java:449) at java.net.URLClassLoader.access$100 (URLClassLoader.java:71) at java.net.URLClassLoader$1.run (URLClassLoader.java:361) at java .net.URLClassLoader $1.run (URLClassLoader.java:355) at java.security.AccessController.doPrivileged (Native Method) at java.net.URLClassLoader.findClass (URLClassLoader.java:354) at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf (ClassRealm.java:401) at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass (SelfFirstStrategy.java:42) at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass (ClassRealm.java At org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:247) at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:239) at jenkins.maven3.agent.Maven31Main.main (Maven31Main.java:138) at jenkins.maven3.agent.Maven31Main.main (Maven31Main.java:67)

After positioning, the original Maven type Job is compatible with the JDK version, which is described in part as follows:

Maven jobs and Java versions compatibility: Because java serialized classes are exchanged between Jenkins master and Maven Jobs it is required that the JVM used to launch Maven is superior or equal to the version of Java for which Jenkins Master is built for.

Jenkins > = 1.520 requires Java 6 thus Maven jobs must be launched with Java > = 6.

Jenkins > = 1.612 requires Java 7 thus Maven jobs must be launched with Java > = 7.

Jenkins > = 2.54 requires Java 8 thus Maven jobs must be launched with Java > = 8.

Our Job must be built with the specified JDK version, and the JDK version cannot be modified at will.

The free style type of Job will not have this problem.

Therefore, you plan to migrate the Maven type Job to the free style type

How to migrate?

So how do you migrate?

According to the statistics of the Job of Maven type, there are hundreds. It is not realistic to migrate manually.

Try to run the script, which may be related to the environment, and it will report an error

Because I am not familiar with groovy, it is troublesome to solve it.

So referring to its logic, we use python script to realize the function of migration.

The python script is shown below, and you can modify it if necessary:

#-*-coding:utf-8-*-import xml.etree.ElementTree as ETimport sysfrom jenkinsapi.jenkins import Jenkinsreload (sys) sys.setdefaultencoding ('utf8') def get_jenkins_server (): JENKINS_URL = "http://127.0.0.1:8080/" JENKINS_USERNAME =" jenkins "JENKINS_PASSWORD =" jenkins "return Jenkins (JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_PASSWORD Timeout=30) def get_modified_xml (job_config_xml_tree): # create builders node builders_element = ET.Element ("builders") job_config_xml_tree.append (builders_element) # Move prebuilders to builders prebuilders_node = job_config_xml_tree.find ("prebuilders") if prebuilders_node is not None and prebuilders_node.getchildren () is not None: for pre_builder in prebuilders_node.getchildren (): Builders_element.append (pre_builder) job_config_xml_tree.remove (prebuilders_node) # Create a maven block maven_node = ET.Element ("hudson.tasks.Maven") builders_element.append (maven_node) move (job_config_xml_tree.find ("goals") Job_config_xml_tree, maven_node, "targets") maven_name_node = job_config_xml_tree.find ("mavenName") if maven_name_node: move (maven_name_node, job_config_xml_tree) Maven_node) else: maven_name_node = ET.Element ("mavenName") maven_name_node.text = "Maven" maven_node.append (maven_name_node) move (job_config_xml_tree.find ("rootPOM"), job_config_xml_tree, maven_node) move (job_config_xml_tree.find ("mavenOpts"), job_config_xml_tree Maven_node) move (job_config_xml_tree.find ("settings"), job_config_xml_tree, maven_node) move (job_config_xml_tree.find ("globalSettings"), job_config_xml_tree, maven_node) # items that don't exist in maven step: remove (job_config_xml_tree, ['resolveDependencies',' processPlugins', 'siteArchivingDisabled',' archivingDisabled' 'mavenValidationLevel', 'disableTriggerDownstreamProjects',' blockTriggerWhenBuilding', 'fingerprintingDisabled',' incrementalBuild', 'processPlugins',' siteArchivingDisabled', 'ignoreUpstremChanges' 'rootModule']) # Move postbuilders to builders postbuilders_node = job_config_xml_tree.find ("postbuilders") if postbuilders_node is not None and postbuilders_node.getchildren () is not None: for post_builder in postbuilders_node.getchildren (): builders_element.append (post_builder) job_config_xml_tree.remove (postbuilders_node) # rename top-level element # new parent Node new_job_config_xml_tree = ET.Element ("project") for child_node in job_config_xml_tree.getchildren (): new_job_config_xml_tree.append (child_node) return new_job_config_xml_treedef move (from_node From_parent_node, to_node To_name=None): if from_node is not None: if to_name: tmp_element_node = ET.Element (to_name) tmp_element_node.text = from_node.text tmp_element_node.attrib = from_node.attrib to_node.append (tmp_element_node) from_parent_node.remove (from_node) Else: to_node.append (from_node) from_parent_node.remove (from_node) def remove (from_node Name_list): for name in name_list: tmp_node = from_node.find (name) if tmp_node is not None: from_node.remove (tmp_node) def main (): jenkins_server = get_jenkins_server () job_list = jenkins_server.get_jobs_list () for job_name in job_list: if not job_name.endswith ("- DEPRECATED" ): job_obj = jenkins_server.get_job (job_name) config_text = job_obj.get_config () config_xml_tree = ET.fromstring (config_text) if config_xml_tree.tag = "maven2-moduleset": new_config_xml_tree = get_modified_xml (config_xml_tree) New_config_xml = ET.tostring (new_config_xml_tree) jenkins_server.rename_job (job_name Job_name + "- DEPRECATED") jenkins_server.create_job (job_name, new_config_xml) print ("migrate job from Maven Type to FreeStyle Type:" + job_name) if _ _ name__ = = "_ _ main__": main () "how to automatically migrate Maven type Job to free style type in batch" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report