Created
November 27, 2015 23:12
-
-
Save lourot/033a3c20942308580583 to your computer and use it in GitHub Desktop.
Variant of https://github.com/tt-gf/artifactory-client-java-example deleting all artifacts of a repo based on criteria
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import org.slf4j.LoggerFactory; | |
import ch.qos.logback.classic.Level; | |
import ch.qos.logback.classic.Logger; | |
import org.jfrog.artifactory.client.Artifactory; | |
import org.jfrog.artifactory.client.ArtifactoryClient; | |
import org.jfrog.artifactory.client.RepositoryHandle; | |
import org.jfrog.artifactory.client.model.Folder; | |
import org.jfrog.artifactory.client.model.Item; | |
import org.jfrog.artifactory.client.model.RepoPath; | |
public class Example { | |
public static void main(String[] args) { | |
// Disable Artifactory client's debug logs: | |
Logger rootLogger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); | |
rootLogger.setLevel(Level.INFO); | |
final String afUrl = prompt("Artifactory URL", "http://artifactory-ci.tomtomgroup.com/artifactory"); | |
final String afUsername = prompt("User name", "root"); | |
final String afPassword = prompt("Password", ""); | |
// Delete obsolete artifacts: | |
Artifactory af = ArtifactoryClient.create(afUrl, afUsername, afPassword); | |
final long tenYearsInMs = 10 * 365 * 24 * 3600 * 1000; | |
final String repoKey = "libs-release-local"; | |
final RepositoryHandle repoHandle = af.repository(repoKey); | |
final String rootFolderPath = "com.tomtom.aurelien/"; | |
for (RepoPath path : af.searches().artifactsCreatedSince(tenYearsInMs).repositories(repoKey).doSearch()) { | |
final String itemPath = path.getItemPath(); | |
if (itemPath.startsWith(rootFolderPath) && !itemPath.contains("+")) { | |
System.out.println("Deleting " + itemPath + " ..."); | |
repoHandle.delete(itemPath); | |
} | |
} | |
deleteFolderIfEmpty(rootFolderPath, repoHandle); | |
} | |
private static String prompt(String desc, String defaultVal) { | |
Scanner input = new Scanner(System.in); | |
System.out.format("%s [%s]: ", desc, defaultVal); | |
String val = input.nextLine(); | |
return val.isEmpty() ? defaultVal : val; | |
} | |
private static void deleteFolderIfEmpty(String path, RepositoryHandle repoHandle) { | |
if (!path.endsWith("/")) { | |
path += "/"; | |
} | |
// Delete children first: | |
final Folder folderBefore = repoHandle.folder(path).info(); | |
for (Item child : folderBefore.getChildren()) { | |
if (child.isFolder()) { | |
// Workaround because Item.getPath() returns null: | |
final String childPath = path + child.getName(); | |
deleteFolderIfEmpty(childPath, repoHandle); | |
} | |
} | |
final Folder folderAfter = repoHandle.folder(path).info(); | |
if (folderAfter.getChildren().isEmpty()) { | |
System.out.println("Deleting " + path + " ..."); | |
repoHandle.delete(path); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment