Step-by-Step Guide: Creating a Java Project with Gradle and Uploading Artifacts to Artifactory
1. Install Prerequisites
- Java JDK: Ensure Java (preferably JDK 17 or newer) is installed. Check with
java -version. - Gradle: Install Gradle (version 8+ recommended). Check with
gradle -version.
2. Create a New Java Project Using Gradle
Open your terminal and execute:
textgradle init --type java-application
- Follow the interactive prompts to choose Java as the language and select other defaults as needed.
- This command will generate a new Gradle Java project with a standard directory structure1234.
Alternatively, you can manually create a folder, navigate into it, and execute gradle init to set up your project.
3. Build the Project
Navigate into your project folder (if not already there):
textcd your-project-name
Build and test your project:
textgradle build
Youโll find the generated JAR in build/libs folder.
4. Add Artifactory Publishing Support
a. Apply the Artifactory Plugin
Add the following to your build.gradle (Groovy DSL):
groovyplugins {
id 'java'
id 'maven-publish'
id 'com.jfrog.artifactory' version '5.2.0' // Use the latest compatible version
}
b. Configure Artifactory Publishing
Add this to your build.gradle:
groovypublishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
artifactory {
contextUrl = 'https://artifactory.example.com/artifactory' // <-- your Artifactory URL
publish {
repository {
repoKey = 'libs-release-local' // or your repository key
username = findProperty('artifactory_user') ?: System.getenv('ARTIFACTORY_USER')
password = findProperty('artifactory_password') ?: System.getenv('ARTIFACTORY_PASSWORD')
maven = true
}
defaults {
publications('mavenJava')
}
}
}
Important: Never hardcode credentials. Use Gradle properties files (~/.gradle/gradle.properties) or environment variables to provide artifactory_user and artifactory_password56.
c. Optional: Add Snapshots Repo
If you need to deploy snapshot versions, add a similar block for a snapshot repository with the appropriate repoKey.
5. Publish to Artifactory
Deploy your artifact to Artifactory by running:
textgradle artifactoryPublish
If everything is set up correctly, your artifact will be uploaded to the specified location in Artifactory76.
Summary Table
| Step | Command/Action |
|---|---|
| New Gradle Project | gradle init --type java-application |
| Build Project | gradle build |
| Add Artifactory Plugin | Edit build.gradle |
| Configure Publishing | Edit build.gradle/set credentials |
| Deploy Artifact | gradle artifactoryPublish |
With these steps, you can create a new Java Gradle project and publish its build artifacts directly to your Artifactory server in a secure and repeatable manner1756.
Another Method
Here is a step-by-step guide to create a new Java project using Gradle and configure it to upload artifacts to JFrog Artifactory.
โ 1๏ธโฃ Install Prerequisites
- Java JDK (version 8+)
- Gradle (latest version recommended)
- Artifactory URL & credentials (from your DevOps/admin team)
Verify installation:
java -version
gradle -v
โ 2๏ธโฃ Create a New Gradle Java Project
Option A: Using Gradle CLI
gradle init --type java-application
Follow prompts:
- Project type:
application - Language:
Java - Build script DSL:
GroovyorKotlin
This will create:
build.gradle
settings.gradle
src/main/java
src/test/java
โ
3๏ธโฃ Configure build.gradle for Publishing
Open build.gradle and make the following changes:
๐น Apply Required Plugins
plugins {
id 'java'
id 'maven-publish'
}
Code language: JavaScript (javascript)
๐น Configure Group, Version & Artifact Name
group = 'com.example'
version = '1.0.0'
Code language: JavaScript (javascript)
๐น Setup Artifactory Publishing
Add this to build.gradle:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
groupId = 'com.example'
artifactId = 'my-artifact'
version = '1.0.0'
}
}
repositories {
maven {
url = uri("https://your-artifactory-domain/artifactory/libs-release-local")
credentials {
username = project.findProperty("artifactory_user") ?: "your-username"
password = project.findProperty("artifactory_password") ?: "your-password"
}
}
}
}
Code language: JavaScript (javascript)
โ 4๏ธโฃ Store Credentials Securely
Instead of hardcoding, use gradle.properties:
๐ ~/.gradle/gradle.properties
artifactory_user=admin
artifactory_password=your-api-key-or-password
โ 5๏ธโฃ Build the Artifact
Run:
gradle clean build
Output: JAR file inside build/libs/my-artifact-1.0.0.jar.
โ 6๏ธโฃ Publish to Artifactory
Run:
gradle publish
โ This uploads the JAR to your Artifactory repository.
โ 7๏ธโฃ Verify Upload
- Open Artifactory web UI
- Navigate to your repository (
libs-release-local) - Confirm
my-artifact-1.0.0.jaris uploaded.
โ 8๏ธโฃ (Optional) Use the JFrog Gradle Plugin
JFrog provides a dedicated plugin for advanced features like build-info and CI/CD integration.
Add to build.gradle:
plugins {
id "com.jfrog.artifactory" version "5.2.0"
}
Code language: JavaScript (javascript)
Configure:
artifactory {
contextUrl = "https://your-artifactory-domain/artifactory"
publish {
repository {
repoKey = "libs-release-local"
username = "${artifactory_user}"
password = "${artifactory_password}"
}
defaults {
publications('mavenJava')
}
}
}
Code language: JavaScript (javascript)
Publish:
gradle artifactoryPublish
๐ Summary
โ
You created a Java project using Gradle
โ
Configured it to upload artifacts to JFrog Artifactory
โ
Secured credentials with gradle.properties
โ
Used gradle publish to push JAR to Artifactory
Iโm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals
Thanks for this clear guide on using Gradle with Artifactory! ๐ฆ The steps and explanations make it much easier to understand how to upload artifacts and manage builds. Very helpful for developers learning CI/CD workflows. Appreciate you sharing this!