PHP Composer, NuGet And Gems 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 23, 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.

A step-by-step guide of PHP Composer, Gems, and NuGet package types in JFrog Artifactory. Get to understand the installation and configuration process of Composer packages with examples:

In this tutorial, we will look at using the JFrog Artifactory PHP Composer, Gems, and NuGet package type.

In the first part of the article using the PHP Composer package type, we will look at the below:

  • Create Local, Remote, and Virtual repository of the PHP Composer Package type
  • Install Composer packages from Artifactory from the PHP Package Repository registry
  • PHP Composer example

I am using an AWS EC2 Ubuntu VM for the exercises given below. To install composer please look at the below site.

=> In-Depth JFrog Tutorials for Beginners

PHP Composer in JFrog Artifactory

JFrog Artifactory Featured Image

Note: Replace the SAAS URLs provided in this article with your URL.

JFrog Artifactory Local, Remote, and Virtual repository of PHP Composer Package Type

From the Administration Tab, click on Repositories and create the repositories.

Local Repository: Used for deploying artifacts.

create repositories

Remote Repository: Caching proxy for the repositories managed remotely.

Caching proxy for the repositories
Caching proxy for the repositories

Virtual Repository: Aggregate of local and remote repos which provides one single endpoint.

Virtual repository
Virtual repository

JFrog config.json and Composer.json Configuration

config.json: Once the Composer client is installed, you can access and configure to work with composer repositories in Artifactory through the command line interface for which you need to setup config.json file.

Use the  Set Me Up of the Virtual repository or Local repository to get code snippets for the config.json file.

JFrog Artifactory config.json

composer.json: This file defines the project requirements. This file mentions the dependencies which we need for our project and it will download them.

This file should be configured to include the composer remote or virtual repository. This can be configured under the repositories section of the file as shown below.

composer.Json

Sample composer.json file:

{
"repositories": [
{
"type": "composer",
"url": "http://vniranjan1972.jfrog.io/artifactory/api/composer/niranjan-composer-virtual/",
"options": {
"ssl": {
"verify_peer": false,
"allow_self_signed": true
}
}
}
],
"require": {
"doctrine/dbal": "3.2.0",
"kassner/log-parser": "2.1.0",
"monolog/monolog": "^2.0",
"cocur/slugify": "^4.2",
"nette/utils": "^3.2.8",
"contributte/utils": "^0.5.1"
},
"config": {
"secure-http": false
}
}

Composer auth.json File Configuration

The auth.json file should be configured to hold Artifactory credentials. The file should have the below contents. The password below should be the identity token of your profile.

Composer auth.json file configuration

Install Dependencies

Run the command to install dependencies in verbose mode
$ composer update -vvv

Install dependencies

List All Packages Installed

$ composer show –installed

List all packages installed

Package Details with Source of Download

$ composer show monolog/monolog

Package details with source of download

Review the Remote Repository Cache

Look at the remote repository where the packages are cached

Review the remote repository cache

Suggested Read =>> PHP Testing Frameworks and Tools

Create and Publish a Custom Composer PHP Package

PHP Packages are stored on packagist.org. These are public packages and anyone can import or use them in their packages.

In this section, we will look at the below:

  • Create a composer package
  • Host it on the GitHub repo
  • Submit to packagist.org

#1) Create a folder and add the following contents to the composer.json file

{
"name": "vniranjan1972/composer-vn",
"description": "A hello world composer private package.",
"autoload": {
"psr-4": {
"vniranjan1972\\HelloWorldPackage\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}

The package being submitted to packagist.org is vniranjan1972/composer-vn

#2) Create a src folder and add a PHP file

<?php

namespace HelloWorldPackage;

class HelloWorld
{
public static function sayHelloVN()
{
echo "Hello World Artifactory";
}
}
Create and publish a custom composer php package

#3) Commit and Push the code to a PUBLIC repository

Commit and Push the code

#4) Create an account in Packagist and click on Submit. Provide the GitHub repo URL. Click on Check.

Submit Package
submit package

Click on Submit.

Packagist

Suggested Read =>> GitHub Tutorial For Developers

#5) Add the below content to the composer.json

"repositories": [

{

"type": "composer",

"url": "http://vniranjan1972.jfrog.io/artifactory/api/composer/niranjan-composer-virtual/",

"options": {

"ssl": {

"verify_peer": false,

"allow_self_signed": true

}

}

}

],

"require": {

"doctrine/dbal": "3.2.0",

"kassner/log-parser": "2.1.0",

"monolog/monolog": "^2.0",

"cocur/slugify": "^4.2",

"nette/utils": "^3.2.8",

“vniranjan1972/composer-vn”: “^2.0.0”
},

"config": {
"secure-http": false
}

#6) The Final file with the package just submitted looks as below:

{

"repositories": [

{

"type": "composer",

"url": "http://vniranjan1972.jfrog.io/artifactory/api/composer/niranjan-composer-virtual/",

"options": {

"ssl": {

"verify_peer": false,

"allow_self_signed": true

}

}

}

],

"require": {

"doctrine/dbal": "3.2.0",

"kassner/log-parser": "2.1.0",

"monolog/monolog": "^2.0",

"cocur/slugify": "^4.2",
"nette/utils": "^3.2.8",
"vniranjan1972/composer-vn": "^2.0.0."
},
"config": {
"secure-http": false
}
}

#6) Run the update command.

Run the update command
Run the update command 2

#7) Look at the remote repository for the package availability in JFrog Artifactory.

remote repository for the package

Local, Remote, and Virtual Repository of NuGet Package Type

NuGet is a package manager for .NET. With its client tools, you can produce and consume the packages. In this section, we will look at how to install/resolve NuGet packages with and without Visual Studio.

Following are the local, remote, and virtual repositories created in Artifactory with default settings:

NuGet Package Type

#1) Download the NuGet CLI from NuGet Gallery | Downloads

#2) To configure the NuGet CLI to work with Artifactory, you need to add this repository to the list of sources. To add this repository, use the following command

nuget sources Add -Name Artifactory -Source https://vniranjan1972.jfrog.io/artifactory/api/nuget/v3/vn-nuget-virtual -username niranjan -password <GeneratedPassword>

configure the NuGet CLI

#3) The NuGet.config file is updated and found @ C:\Users\<UserName>\AppData\Roaming\NuGet

NuGet.config

#4) Lastly to authenticate against Artifactory with the NuGet API key, run the following command below:

nuget setapikey niranjan:<GeneratedPassword> -Source Artifactory

authenticate against Artifactory with the NuGet

#5) Install/Resolve package – Example

nuget install Newtonsoft.Json -Version 13.0.2

Resolve package

Search and Install a NuGet Package With Visual Studio

In the previous section, we looked at how to install the NuGet package using the CLI. In this section, we will look at how to search and install the NuGet package using Visual Studio.

I am using VS 2019 in this example.

#1) Ensure the NuGet.config file is updated as in the previous section and stored at C:\Users\<UserName>\AppData\Roaming\NuGet folder

#2) Ensure the ‘Artifactory’ Package Source is present in Tools->Options->NuGet Package Manager->Package Sources

Search for Artifactory’ Package Source

#3) In the Solution Explorer load the Project and select right-click on Project -> Manage NuGet Packages

load the Project

#4) In the NuGet Package Manager window select the Browse Tab. Ensure the Package source selected is Artifactory

NuGet Package Manager

#5) Search for the Package to install.

Search for Package to install

#6) Click on Install and look at the Output Tab below.

Output Tab

Local, Remote Repository of Gems Package Type

JFrog Artifactory has full support for RubyGems repositories which includes a local repository, caching remote RubyGems repository (RubyGems.org), and a virtual repository that aggregates both local and remote repository.

In this article let’s look at how to deploy and resolve gems packages.

Gems – Local Repository

Gems - Local Repository

Gems – Remote Repository

Gems - Remote Repository

Resolve/Install Gems Package

Select the Gems-Remote repository and click on SetMeUp.

For the gem client to upload and download Gems from the Artifactory repository you need to add the same to your ~/.gemrc file using the following command:

gem source -a https://niranjan:<APIKEY> @vniranjan1972.jfrog.io/artifactory/api/gems/niranjan-remote-gems/

Resolve or Install Gems Package

$ gem source –list

gem source –list

~/.gemrc file

gemrc file

Install the colorize gem using the command

gem install <package>

example

$ sudo gem install colorize

sudo gem install colorize
sudo gem install colorize

Use the alternate command specifying the source.

$ sudo gem install <PACKAGE> –source https://niranjan:<IdentityToken>@vniranjan1972.jfrog.io/artifactory/api/gems/niranjan-remote-gems

Use the alternate command specifying the source

Use a 3rd Party Gem (colorize gem) in Your Gem Application

I am going to use the colorize gem installed from the Artifactory repository in the previous step in my basic hello world application.

The standard ruby convention is to create the project in a lib folder.

Create a file called artifactory_gem.rb in the lib folder. Add the below code to the file.

require ‘colorize’

puts “Hello, world from Artifactory!”.colorize(:blue)

$ ruby artifactory_gem.rb

ruby artifactory_gem.rb

Create and Publish GEM to Artifactory

Every gem project has the *.gemspec file containing information for a gem. In our project, this information is stored in the artifactory_gem.gemspec file.

Add the following data in the file which is placed in the root of the project.

Gem::Specification.new do |s|
s.name = "artifactory_gem"
s.version = "0.0.2"
s.date = %q{2023-01-08}
s.authors = "V Niranjan"
s.summary = %q{This is an gem for Artifactory}
s.files = [
"lib/artifactory_gem.rb"
]
s.require_paths = ["lib"]
end
Create and Publish GEM to Artifactory

Generate the GEM file. Run the command.

$ gem build artifactory_gem.gemspec

gem build artifactory_gem.gemspec

Push the GEM to Artifactory

Setup credentials.

$ curl -uniranjan:<IdentityToken> https://vniranjan1972.jfrog.io/artifactory/api/gems/niranjan-local-gems/api/v1/api_key.yaml > ~/.gem/credentials

$ chmod 600 credentials

chmod 600 credentials

To push gems to this repository, you need to set the global variable $RUBYGEMS_HOST to point to the local repository as follows.

$ export RUBYGEMS_HOST=https://vniranjan1972.jfrog.io/artifactory/api/gems/niranjan-local-gems

$ gem push artifactory_gem-0.0.2.gem –host https://vniranjan1972.jfrog.io/artifactory/api/gems/niranjan-local-gems

gem push artifactory

The custom-built gem is now available in the local repository of Artifactory.

the local repository of Artifactory

Conclusion

In this tutorial on JFrog Artifactory as a binary repository manager, we have seen how to use the platform with Generic, Maven, Docker, NPM, Python, Gradle, Go, PHP Composer, Gems, and NuGet package types though it supports around 30+ 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.

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 tutorial, I have also shown how to use the Artifactory integrated with Jenkins for most of the package types.

I do hope that this JFrog series so far, has helped you gain knowledge on how to use Artifactory as a binary repository manager to manage your builds and deployments. JFrog Artifactory truly is a very important part of DevSecOps.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment