Skip to content

Instantly share code, notes, and snippets.

@rodolfodpk
Created August 30, 2012 13:38
Show Gist options
  • Save rodolfodpk/3528708 to your computer and use it in GitHub Desktop.
Save rodolfodpk/3528708 to your computer and use it in GitHub Desktop.
Script para renomear jars de releases snapshots baixados do Artifactory
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
/**
* Script para renomear jars de releases snapshots baixados do Artifactory
* Possibilita time de QA rodar versão snapshot instalada da seguinte maneira
* java -jar instalador.jar -repo http://50.56.88.164:8080/artifactory/repo
* -snapshot os jars instalados vem com um timestamp adicionado ao nome e isto
* impossibilita a execução pois os MANIFESF.MF referenciam jars no formato
* xxx-versao-SNAPSHOT.jar
*/
public class RenameSnapshot {
public static void main(String[] args) {
if (args.length != 2) {
System.out
.println("Erro: informe um diretório alvo e a versão do release !");
System.exit(1);
}
String targetDir = args[0];
String version = args[1];
Collection<File> all = new ArrayList<File>();
addTree(new File(targetDir), all);
Collection<File> artifacts = filter(all, version);
rename(artifacts, version);
}
static void addTree(File file, Collection<File> all) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
all.add(child);
addTree(child, all);
}
}
}
static Collection<File> filter(Collection<File> all, String version) {
Collection<File> target = new ArrayList<File>();
for (File f : all) {
if (f.isFile()
&& !f.getAbsolutePath().contains("backup")
&& f.getName().contains(version)
&& (f.getName().endsWith(".jar") || f.getName().endsWith(
".war"))) {
target.add(f);
}
}
return target;
}
static void rename(Collection<File> all, String version) {
String pattern = "(\\w*)(.*)(\\.jar|\\.war)";
for (File f : all) {
if (f.getName().matches(pattern)) {
String newName = f.getParent()
+ "/"
+ f.getName().replaceFirst(pattern,
"$1-" + version + "-SNAPSHOT$3");
f.renameTo(new File(newName));
System.out.println(newName);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment