
org.kuali.maven.plugins.jenkins.UpdateJobsMavenVersionMojo Maven / Gradle / Ivy
/**
* Copyright 2011-2012 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