Working with Gradle and GO Package Type in JFrog Artifactory

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated June 21, 2024
Edited by Kamila

Edited by Kamila

Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

This tutorial on Gradle and GO Package Type will explain how to create local, remote, and virtual repositories along with the configuration of GO Repo with pictorial examples:

In our earlier tutorials of this JFrog Artifactory article series, we did see how to use the generic, python, and docker package types. In this part, we will look at Gradle and GO package types.

For each of the package types for Gradle and GO we will do the following:

  • Create local repository
  • Create remote repository
  • Create a virtual repository with a combination of the first 2 repositories

=> Complete JFrog Guide for ALL

Getting Started with Gradle and GO Package Type

Gradle and GO Package type

Gradle Package Type

Create Local Repository

Local repository is a central location to upload and store your artifacts generated by builds. To create a local repository as an Admin user, select Administration TAB -> Repositories. Now click on + Add Repository in the top right and select Local Repository.

Local Repository

Select Gradle as the package type.

Select Gradle as package type

Enter the Repository Key and click on Create Local Repository.

Create Local Repository

Create Remote Repository

Remote repository is a cache of a repository that is managed remotely. Let’s look at how to create a Maven remote repository.

As an Admin user, select Administration TAB -> Repositories. Now click on + Add Repository –> Remote Repository in the top right. Select the NPM package type and enter the details below:

Administration TAB 4

Click on Create Remote Repository.

Create Virtual Repository

Virtual repository is a combination of both local and remote repositories having a common endpoint. To create a Virtual repository as an Admin user, select Administration TAB -> Repositories. Now click on + Add Repository -> Virtual Repository in the top right. Select NPM.

Add local and remote repositories and also set the Default deployment repository to the local repository.

Create Virtual repository

Add both local and remote repositories and set the default deployment repository as shown.

Add both local and remote repository
Create Virtual Repository

Click on Create Virtual Repository

JFrog Artifactory Gradle Example

In this section, we will look at how to build a Gradle project using the remote repository and publish it to the local repository.

To set up, as a user, go to Application TAB -> Artifactory -> Artifacts. Search for the Virtual repository and click on Set Me Up.

JFrog Artifactory Gradle Example

Click on Generate Settings. Copy the text to build.gradle file in the root folder of the project.

build.gradle file

Download the gradle.properties file. The location of the file in Linux is typically in /home/{UserName}/.gradle and for windows, it is in c:\users\{UserName}\.gradle

Copy the text and save it in build.gradle file of the Gradle project. Add the additional text in GREEN to the file.

buildscript {
  repositories {
    jcenter {

      url 'https://vniranjan2512.jfrog.io/artifactory/niranjan-gradle-virtual'
      credentials {
      username = "${artifactory_user}"
      password = "${artifactory_password}"
            }
    }
}
dependencies {
    //Check for the latest version here: http://plugins.gradle.org/plugin/com.jfrog.artifactory
    classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
  }
}
allprojects {
   apply plugin: "com.jfrog.artifactory"
   apply plugin: 'maven-publish'
   apply plugin: 'java'
}
version = '0.3.0-SNAPSHOT'

artifactory {
   contextUrl = "${artifactory_contextUrl}" //The base Artifactory URL if not overridden by the publisher/resolver
  publish {
    repository {
      repoKey = 'niranjan-gradle-local'
      username = "${artifactory_user}"
      password = "${artifactory_password}"
      maven = true
    }
    defaults {
      publications ('mavenJava')
      }
     }
    resolve {
      repository {
         repoKey = 'niranjan-gradle-virtual'
         username = "${artifactory_user}"
         password = "${artifactory_password}"
         maven = true

        }
     }
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java
    }
  }
}

gradle.properties file – Artifactory context URL and credentials are stored in a file and it looks as below.

artifactory_user=niranjan

artifactory_password=<Password generated by Artifactory>

artifactory_contextUrl=https://vniranjan2512.jfrog.io/artifactory

Gradle Project and Build JAR File

Created a Java Gradle project with a Groovy script option.

Java Gradle project

Let’s run the build command. The command will show from where the dependencies are being downloaded which should be from Artifactory.

gradle build –refresh-dependencies –info

gradle build --refresh-dependencies --info11

gradle run

gradle run12

Suggested Reading =>> How to use Gradle to create a Project

Publish JAR Artifact Using Gradle to JFrog Artifactory

In this section, we will run the below command to publish the JAR and the POM file to JFrog Artifactory.

gradle artifactoryPublish

gradle artifactoryPublish

Look at the deployed artifact in JFrog Artifactory.

deployed artifact in JFrog Artifactory

Gradle Integration with Jenkins

In this section, we will look at how to configure Gradle and its integration with Jenkins. We will look at how to Build and Publish to Artifactory.

  • In Jenkins as admin go to Manage Jenkins -> Global Tool Configuration

Click on Add Gradle.

gradle

Add details as per your installation and click on Save.

Add details
  • Configure Jenkins Job as shown
Configure Jenkins

Add Build Step -> Invoke Gradle Script twice and add the tasks for Build and artifactoryPublish

add the tasks

Click on Save and trigger the build.

trigger the build

Go to Artifactory and look at the artifacts deployed under the local repo.

Configuring Proxy Settings in Gradle

In the gradle.properties file add the following settings:

systemProp.http.proxyHost=proxyserver

systemProp.http.proxyPort=80

systemProp.http.nonProxyHosts=*.mycorp.com|localhost

systemProp.https.proxyHost=proxyserver

systemProp.https.proxyPort=80

systemProp.https.nonProxyHosts=*. mycorp.com|localhost

GO Package Type

Creating Local, Remote, and Virtual Repositories

In this section, we will create GO repositories. As you may be aware, now to create all the repositories, here is just a snapshot of the repositories created for the GO package type.

Virtual repositories for GO package

For the GO package type, we will create a package and use that in another app.

For e.g. greetings is a package and hello is the main routine.

greetings is a package20

Pre-requisite is as follows to be installed on the VM

Install golang on the system: https://go.dev/doc/install

Install JFrog CLI on the system: https://jfrog.com/getcli/

Configure GO Repo

Once the GO virtual repos are created, use the Set Me Up to get details on how to configure, deploy, and resolve your environment.

Use JFRog CLI to add an artifactory server.

jf c add rt-server –artifactory-url https://vniranjan2512.jfrog.io/artifactory –user niranjan –password <Identity Token Pwd> –interactive=false

#1) Add code to greetings/greetings.go to file

package greetings
import "fmt"
func Hello(name string) string {
  // Return a greeting with name
  message := fmt.Sprintf("Hi, %v% Welcome to JFrog Artifactory!", name)
  return message
}

#2) Add the code to hello/hello.go file

package main
import (
  "fmt"
  "myteam.com/mygolib"
  )
func main() {
   // Get a greeting message and print it
   message := greetings.Hello("Niranjan")
   fmt.Println(message)
}

#3) To publish the greetings package to artifactory run the command in the greetings directory. Replace the below virtual and local servers for your instance.

jf go-config –server-id-resolve rt-server –repo-resolve niranjan-go-virtual –repo-deploy niranjan-go-local

repo-deploy 21

Run the below commands in the greetings folder

go mod init myteam.com/mygolib
go mod tidy
jf go build
jf gp v1.0.0
ubuntu22
Resolve the Go package23

Resolve the Go package

In the hello directory, run the commands as shown below:

#go proxy from Set Me Up of Virtual repo

export GOPROXY=https://niranjan:@vniranjan2512.jfrog.io/artifactory/api/go/niranjan-go-virtual
go mod init myteam.com/mygopkg
export GONOSUMDB=myteam.com/mygolib
go mod tidy
go build
// Run the hello.go file
go run hello.go

Conclusion

In this series on JFrog Artifactory as a binary repository manager, we have seen how to use the platform with Generic, Maven, Docker, NPM, Gradle, and Go package types, though it supports around 25+ package formats. The process of using JFrog Artifactory around other package formats would be similar and should follow the Set Me Up instructions for every repository.

Recommended Reading =>> How to Create a Gradle Project with Selenium

Also, one can follow the Virtual repository format to combine both local and remote repositories, which will provide one endpoint to deal with in your build and deployments.

Along with the above examples across this series, we have also shown how to use the Artifactory integrated with Jenkins for most of the package types.

Hope that this series so far has helped you to gain knowledge on how to use Artifactory as a binary repository manager to manage your builds and deployments.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment