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.
Getting Started
Add artifactory-java-client-services as a dependency to your build script.
Maven
Add the following dependency to your pom.xml file:
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();
java.io.File file = new java.io.File("fileToUpload.txt");
File result = artifactory.repository("RepoName").upload("path/to/newName.txt", file).doUploadAndExplode(true)
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.:
CustomPackageTypeImpl customPackageType = new CustomPackageTypeImpl("name");
CustomRepositorySettingsImpl settings = new CustomRepositorySettingsImpl(customPackageType);
Map<String, Object> customProperties = new HashMap<>();
customProperties.put("key", "value");
Repository repository = artifactory.repositories()
.builders()
.localRepositoryBuilder()
.key("NewRepoName")
.description("new local repository")
.repositorySettings(settings)
.customProperties(customProperties)
.build();
String result = artifactory.repositories().create(2, repository);
Smart Remote Repositories
A smart remote repository is a remote repository that proxies a repository from another instance of Artifactory. Smart remote repositories are configured with four additional properties.
User user = artifactory.security().user("userName");
String name = user.getName();
String email = user.getEmail();
Collection<String> groups = user.getGroups();
Date loggedIn = user.getLastLoggedIn();
boolean profileUpdatable = user.isProfileUpdatable();
boolean isAdmin = user.isAdmin();
boolean internalPass = user.isInternalPasswordDisabled();
String realm = user.getRealm();
List all User Names
Collection<String> userNames = artifactory.security().userNames();
for (String userName : userNames) {
User user = artifactory.security().user(userName);
}
Creating or Updating Users
UserBuilder userBuilder = artifactory.security().builders().userBuilder();
User user = userBuilder.name("userName")
.email("user@mail.com")
.admin(false)
.profileUpdatable(true)
.password("password")
.build();
artifactory.security().createOrUpdate(user);
Deleting Users
String result = artifactory.security().deleteUser("userName");
Managing User Groups
Get User Group Information
Group group = artifactory.security().group("groupName");
String description = group.getDescription();
boolean isAutoJoin = group.isAutoJoin();
boolean isAdminPrivileges = group.isAdminPrivileges();
List all User Groups
List<String> groupNames = artifactory.security().groupNames();
for (String groupName : groupNames) {
Group group = artifactory.security().group(groupName);
}
Creating or Updating User Groups
Group group = artifactory.security().builders().groupBuilder()
.name("groupName")
.autoJoin(true)
.adminPrivileges(true)
.description("new group")
.build();
artifactory.security().createOrUpdateGroup(group);
Creating or Updating User External Groups
When using LDAP integration or realm User plugin, it could be interesting to preload groups (and permissions) before any user login :
String realmAttributes = "ldapGroupName=groupName;groupsStrategy=STATIC;groupDn=cn=GROUPNAME,ou=foo,o=bar";
Group group = artifactory.security().builders().groupBuilder()
.name("groupName")
.description("GROUPNAME")
.realm("ldap")
.realmAttributes(realmAttributes)
.build();
artifactory.security().createOrUpdateGroup(group);
NB: The realmAttributes depends of realm implementation ; so firstly, use LDAP Groups Synchronization to import some groups manually in Artifactory, and analyse a JSON GET on one of this/these group(s) to understand the content of realmAttributes parameter.
ArtifactoryRequest repositoryRequest = new ArtifactoryRequestImpl().apiUrl("api/repositories")
.method(ArtifactoryRequest.Method.GET)
.responseType(ArtifactoryRequest.ContentType.JSON);
ArtifactoryResponse response = artifactory.restCall(repositoryRequest);
// Get the response headers
org.apache.http.Header[] headers = response.getAllHeaders();
// Get the response status information
org.apache.http.StatusLine statusLine = response.getStatusLine();
// A convenience method for verifying success
assert response.isSuccessResponse()
// Get the response raw body
String rawBody = response.rawBody();
// If the the response raw body has a JSON format, populate an object with the body content,
// by providing a object's class.
List<Map<String, String>> parsedBody = response.parseBody(List.class);
Building and Testing the Sources
The code is built using Gradle and includes integration tests.
Since the tests may use features which have been recently added to Artifactory, such as new package types, it is best to run the tests against the latest release of Artifactory. Some tests may therefore fail otherwise. Those tests can be manually commented out in that case.
If you'd like to build the code without tests, run:
gradle clean build -x test
Please follow these steps to build and test the code:
Startup an Artifactory-Pro instance.
Set the CLIENTTESTS_ARTIFACTORY_URL, CLIENTTESTS_ARTIFACTORY_USERNAME and CLIENTTESTS_ARTIFACTORY_PASSWORD environment variables with your Artifactory URL, username and password.
Run:
gradle clean test
Example Projects
We created sample projects demonstrating how to use the Artifactory Java Client.
Contributing Code
We welcome community contribution through pull requests.
Guidelines
If the existing tests do not already cover your changes, please add tests..
Pull requests should be created on the dev branch.