org.kuali.maven.plugins.jenkins.UpdateJobsMavenVersionMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jenkins-maven-plugin Show documentation
Show all versions of jenkins-maven-plugin Show documentation
Automated management of Jenkins jobs via Maven. Much of the information needed by Jenkins when creating a job is already in the Maven pom.
The SCM information and CI url are present. Jenkins jobs also typically have names that reflect the groupId, artifactId, and version in some manner.
This plugin automates the process of creating Jenkins jobs by harvesting information from the POM to create XML config files in the format Jenkins needs. The
Jenkins CLI API is then used to create, update, read, and delete Jenkins jobs on the CI server. If your Jenkins instance requires authentication, add
your public key to your user account on the Jenkins server.
/**
* Copyright 2011-2013 The Kuali Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ecl2.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kuali.maven.plugins.jenkins;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
/**
* @goal updatejobsmavenversion
*/
public class UpdateJobsMavenVersionMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
try {
List configFiles = getConfigFiles();
getLog().info("Located " + configFiles.size() + " job config files");
// List mvn303Tokens = getMvn303ReplacementTokens(configFiles);
// List jdkTokens = getJdkReplacementTokens(configFiles);
// List mvnTokens = getMvnReplacementTokens(configFiles);
// getLog().info("Updating Maven Config");
// updateContent(configFiles, mvnTokens, "MAVEN3 ", ".bak.mvn");
// getLog().info("Updating JDK Config");
// updateContent(configFiles, jdkTokens, "JDK6 ", ".bak.jdk");
} catch (Exception e) {
throw new MojoExecutionException("Unexpected error", e);
}
}
protected void updateContent(List files, List rtokens, String replacement, String extension) throws IOException {
for (File file : files) {
String oldContent = FileUtils.readFileToString(file);
String newContent = getReplacementContent(oldContent, rtokens, replacement);
if (oldContent.equals(newContent)) {
continue;
}
getLog().info("Updating " + file);
File bak = new File(file.getAbsolutePath() + extension);
FileUtils.copyFile(file, bak);
FileUtils.writeStringToFile(file, newContent);
}
}
protected String getReplacementContent(String s, List rtokens, String replacement) {
for (String rtoken : rtokens) {
s = s.replace(rtoken, replacement);
}
return s;
}
protected void addTokens(String[] tokens, Map map, String open, String close) {
if (tokens == null) {
return;
}
for (String token : tokens) {
if (token == null) {
continue;
}
String s = open + token + close;
Integer count = map.get(s);
if (count == null) {
count = new Integer(1);
} else {
count++;
}
map.put(s, count);
}
}
protected List getMvn303ReplacementTokens(List files) throws IOException {
for (File file : files) {
String s = FileUtils.readFileToString(file);
int pos = s.indexOf("Maven-3.0.3");
if (pos != -1) {
getLog().info(file + "");
}
}
List rtokens = new ArrayList();
return rtokens;
}
protected List getJdkReplacementTokens(List files) throws IOException {
String open = "";
String close = " ";
Map map = new HashMap();
for (File file : files) {
String s = FileUtils.readFileToString(file);
String[] tokens = StringUtils.substringsBetween(s, open, close);
addTokens(tokens, map, open, close);
}
List rtokens = new ArrayList();
Set keys = map.keySet();
for (String key : keys) {
String rtoken = key;
getLog().info(key + "=" + map.get(key));
rtokens.add(rtoken);
}
return rtokens;
}
protected List getMvnReplacementTokens(List files) throws IOException {
String open = "";
String close = " ";
Map map = new HashMap();
for (File file : files) {
String s = FileUtils.readFileToString(file);
String[] tokens = StringUtils.substringsBetween(s, open, close);
addTokens(tokens, map, open, close);
}
List rtokens = new ArrayList();
Set keys = map.keySet();
for (String key : keys) {
String rtoken = key;
getLog().info(key + "=" + map.get(key));
rtokens.add(rtoken);
}
return rtokens;
}
protected List getConfigFiles() {
File directory = new File("/var/lib/jenkins/jobs");
File[] files = directory.listFiles();
List list = new ArrayList();
for (File file : files) {
File config = new File(file.getAbsolutePath() + "/config.xml");
if (config.exists()) {
list.add(config);
}
}
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy