如何用Groovy在Active Choices Parameters中连接SVN获取Jar生成下拉选项?
Yes, You Can Connect to SVN and Fetch Jar Files with Groovy in Active Choices!
Absolutely! You can modify your Groovy script to connect to an SVN repository using a username and password, list the contents of a specified directory, filter for .jar files, and populate your dropdown menu. Here's how to do it:
Prerequisite
First, ensure that SVNKit (the Java-based SVN library) is available in your Jenkins instance's classpath. Most Jenkins installations with the Subversion plugin already include this, but if not, you may need to add the SVNKit JAR to $JENKINS_HOME/lib/ and restart Jenkins.
Groovy Script for SVN Jar Fetching
import org.tmatesoft.svn.core.SVNURL import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl import org.tmatesoft.svn.core.io.SVNRepository import org.tmatesoft.svn.core.io.SVNRepositoryFactory import org.tmatesoft.svn.core.wc.SVNWCUtil // Initialize SVN repository factories to support all protocols DAVRepositoryFactory.setup() SVNRepositoryFactoryImpl.setup() FSRepositoryFactory.setup() // Configuration - update these values to match your SVN setup! def svnUrl = "https://your-svn-server/path/to/target/jar/directory" def username = "your-svn-username" def password = "your-svn-password" def jarFiles = [] try { // Create a connection to the SVN repository SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl)) // Set up authentication with your credentials ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray()) repository.setAuthenticationManager(authManager) // Fetch all entries in the specified SVN directory (revision -1 means latest) def entries = repository.getDir("", -1, null, (Collection) null) // Filter entries to only include .jar files and collect their names entries.each { entry -> if (entry.name.endsWith(".jar")) { jarFiles.add(entry.name) } } // Sort the list alphabetically for consistency jarFiles = jarFiles.sort() } catch (Exception e) { // Handle errors gracefully by showing an error message in the dropdown jarFiles.add("Error: " + e.getMessage()) } return jarFiles
Key Notes:
- Update Configuration Values: Replace
svnUrl,username, andpasswordwith your actual SVN details. - Error Handling: The script includes basic error handling to display a meaningful message in the dropdown if something goes wrong (e.g., invalid credentials, connection issues).
- Protocol Support: This script works with
http://,https://,svn://, andfile://SVN URLs, thanks to the initialized factories. - Security Tip: Storing plain-text passwords directly in the script isn't ideal. For better security, use Jenkins' credential store to retrieve passwords securely (you can leverage the
com.cloudbees.plugins.credentials.CredentialsProviderclass in Groovy to fetch stored credentials).
内容的提问来源于stack exchange,提问作者corny
相关产品推荐
相关产品推荐

