Artifactory Java client provides simple yet powerful Artifactory connection and management within your Java code.
The client allows managing Artifactory repositories, users, groups, permissions and system configuration. It also allows
searches, upload and download artifacts to or from Artifactory and a lot more.
Table of Contents
Getting Started
Add artifactory-java-client-services as a dependency to your build script.
Maven
Add the following dependency to your pom.xml file:
<dependency>
<groupId>org.jfrog.artifactory.client</groupId>
<artifactId>artifactory-java-client-services</artifactId>
<version>RELEASE</version>
</dependency>
Gradle
Add the following snippets to your build.gradle file:
repositories {
mavenCentral()
}
dependencies {
compile 'org.jfrog.artifactory.client:artifactory-java-client-services:+'
}
Examples:
This section includes a few usage examples of the Java client APIs from your application code.
Setting up Artifactory
Artifactory artifactory = ArtifactoryClientBuilder.create()
.setUrl("ArtifactoryUrl")
.setUsername("username")
.setPassword("password")
.build();
Trusting your own self-signed certificates without ignoring any SSL issue:
Artifactory artifactory = ArtifactoryClientBuilder.create()
.setUrl("ArtifactoryUrl")
.setUsername("username")
.setPassword("password")
.setSslContextBuilder(SSLContexts.custom()
.loadTrustMaterial(< your trust strategy here >))
.build();
Adding a request interceptor for logging or modifying outgoing requests:
Artifactory artifactory = ArtifactoryClientBuilder.create()
.setUrl("ArtifactoryUrl")
.setUsername("username")
.setPassword("password")
.addInterceptorLast((request, httpContext) -> {
System.out.println("Artifactory request: " + request.getRequestLine());
})
.build();
Using HTTP proxy:
ProxyConfig proxy = new ProxyConfig();
proxy.setHost("localhost");
proxy.setPort(9090);
Artifactory artifactory = ArtifactoryClientBuilder.create()
.setUrl("ArtifactoryUrl")
.setUsername("username")
.setPassword("password")
.setProxy(proxy)
.setNoProxyHosts(".gom.com,*.jfrog.com,.not.important.host:443")
.build();
NOTE: If hosts to ignore proxy are not set through "setNoProxyHosts(String noProxyHosts)" method,
they are set through NO_PROXY env variable.
Uploading and downloading artifacts
Uploading an Artifact
- Using java.io.File as source:
java.io.File file = new java.io.File("fileToUpload.txt");
File result = artifactory.repository("RepoName").upload("path/to/newName.txt", file).doUpload();
- Using an InputStream as source:
try (InputStream inputStream = Files.newInputStream(Paths.get("fileToUpload.txt"))) {
File result = artifactory.repository("RepoName").upload("path/to/newName.txt", inputStream).doUpload();
}
Upload and explode an Archive
java.io.File file = new java.io.File("fileToUpload.txt");
File result = artifactory.repository("RepoName").upload("path/to/newName.txt", file).doUploadAndExplode(true)
Uploading an Artifact with Properties
java.io.File file = new java.io.File("fileToUpload.txt");
File deployed = artifactory.repository("RepoName")
.upload("path/to/newName.txt", file)
.withProperty("color", "blue")
.withProperty("color", "red")
.doUpload();
Uploading and Artifact with an UploadListener
Can be used for tracking the progress of the current upload:
java.io.File file = new java.io.File("fileToUpload.txt");
File result = artifactory.repository("RepoName")
.upload("path/to/newName.txt", file)
.withListener((bytesRead, totalBytes) -> {
System.out.println("Uploaded " + format.format((double) bytesRead / totalBytes));
})
.doUpload();
The code snippet above would print the percentage of the current upload status.
Important: The totalBytes is calculated from the size of the input File. In case the source is a java.io.File object, the upload will use the File.length() to determine the total number of bytes. If the source is an InputStream, the total size of the upload must be specified using the withSize(long size) method.
e.g.:
Path sourceFile = Paths.get("fileToUpload.txt");
try (InputStream inputStream = Files.newInputStream(sourceFile)) {
File result = artifactory.repository("RepoName")
.upload("path/to/newName.txt", inputStream)
.withSize(Files.size(sourceFile))
.withListener((bytesRead, totalBytes) -> {
System.out.println("Uploaded " + format.format((double) bytesRead / totalBytes));
})
.doUpload();
}
Copy an Artifact by SHA-1
java.io.File file = new java.io.File("fileToUpload.txt");
String sha1 = calcSha1(file)
File deployed = artifactory.repository("RepoName")
.copyBySha1("path/to/newName.txt", sha1)
.doUpload();
Downloading an Artifact
InputStream iStream = artifactory.repository("RepoName")
.download("path/to/fileToDownload.txt")
.doDownload();
InputStream iStream = artifactory.repository("RepoName")
.download("path/to/fileToDownload.txt")
.withProperty("colors", "red")
.doDownload();
InputStream iStream = artifactory.repository("RepoName")
.download("path/to/fileToDownload.txt")
.withMandatoryProperty("colors", "red")
.doDownload();
Downloading Artifact with custom headers
Map<String, String> headers = new HashMap<>();
headers.put("Range", "bytes=0-10");
InputStream iStream = artifactory.repository("RepoName")
.download("path/to/fileToDownload.txt")
.doDownloadWithHeaders(headers);
File, Folder and Repository Info
File Info
File file = artifactory.repository("RepoName").file("path/to/file.txt").info();
boolean isFile = file.isFolder();
long fileSize = file.getSize();
String fileUri = file.getDownloadUri();
String md5Checksum = file.getChecksums().getMd5();
String sha1Checksum = file.getChecksums().getSha1();
String sha2Checksum = file.getChecksums().getSha256();
Folder Info
Folder folder = artifactory.repository("RepoName").folder("path/to/folder").info();
boolean isFolder = folder.isFolder();
String repoName = folder.getRepo();
String folderPath = folder.getPath();
int childrenItemsSize = folder.getChildren().size();
Repository Info
Repository repo = artifactory.repository("RepoName").get();
String repoKey = repo.getKey();
String desc = repo.getDescription();
String layout = repo.getRepoLayoutRef();
RepositoryType repoClass = repo.getRclass();
RepositorySettings settings = repo.getRepositorySettings();
PackageType packageType = settings.getPackageType();
if (PackageType.bower == packageType) {
BowerRepositorySettings settingsForBower = (BowerRepositorySettings) settings;
String bowerRegistryUrl = settingsForBower.getBowerRegistryUrl();
}
Storage Summary Info