Skip to content

Latest commit

 

History

History
443 lines (361 loc) · 16.2 KB

README.md

File metadata and controls

443 lines (361 loc) · 16.2 KB

🐸 Artifactory Gradle Plugin 🐘

Scanned by Frogbot


Branch Main Dev
Status Test Test

Table of Contents


📚 Overview

The minimum supported Gradle version to use this plugin is v6.9

The Gradle Artifactory Plugin provides tight integration with Gradle. All that is needed is a simple modification of your build.gradle script file with a few configuration parameters, and you can deploy your build artifacts and build information to Artifactory.

Integration Benefits: JFrog Artifactory and Gradle Repositories

🧩 Dependencies resolution with Artifactory

Define the project to preform dependency resolution resolve dependencies from the default dependency resolution from Artifactory:

repositories { 
    maven {
        // The Artifactory (preferably virtual) repository to resolve from 
        url = uri("http://repo.myorg.com/artifactory/libs-releases")
        // Optional resolver credentials (leave out to use anonymous resolution)
        credentials {
            // Artifactory's username
            username = "resolver"
            // Password or API Key
            password = "resolverPaS*" 
        } 
    }

    ivy {
        url = uri("http://localhost:8081/artifactory/ivy-releases")
        // Optional section for configuring Ivy-style resolution.
        patternLayout {
            ivy("[organization]/[module]/[revision]/ivy.xml")
            artifact("[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]")
            // Convert any dots in an [organization] layout value to path separators, similar to Maven's groupId-to-path conversion. False if not specified.
            setM2compatible(true)
        }
    }
}
build.gradle
repositories {
  maven {
    url = "http://repo.myorg.com/artifactory/libs-releases"
    credentials {
      username = "resolver"
      password = "resolverPaS"
    }
  }
  ivy {
    url = "http://localhost:8081/artifactory/ivy-releases"
    patternLayout {
      ivy = "[organization]/[module]/[revision]/ivy.xml"
      artifact = "[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"
      m2Compatible = true
    }
  }
}

Follow this documentation for different ways to configure your repositories.


⬇️ Download and Installation

Add the following snippet to your build.gradle.kts

// Replace <plugin version> with the version of the Gradle Artifactory Plugin.
plugins {
    id("com.jfrog.artifactory") version "<plugin version>"
}
build.gradle
plugins {
  id "com.jfrog.artifactory" version "<plugin version>"
}

📦 Artifactory Publication

The plugin adds the artifactoryPublish task for each project, at the 'publishing' group. The task does the following to the project and its submodules:

  1. Collects all the publication artifacts - follow this documentation about defining publications.
  2. Extract module-info (intermediate file) that describes each module build information.
  3. Extract build-info file in the root project that describes all the information about the build.
  4. Deploy the generated artifacts and build-info file to your Artifactory repository.

Running the task with:

gradle artifactoryPublish
gradle wrapper in Unix
./gradlew artifactoryPublish
gradle wrapper in Windows
gradlew.bat artifactoryPublish

⚙️ Artifactory Configuration

To use the artifactoryPublish task you need to define, at the root project build.gradle.kts, the Artifactory convention. This configuration will define the information needed by the tasks to access the Artifactory instance that the artifacts will be published to.

artifactory {
    publish {
        // Define the Artifactory URL to publish the artifacts
        contextUrl = 'http://127.0.0.1:8081/artifactory'
        // Define the project repository that the artifacts will be published to
        repository {
          // The Artifactory repository key
          repoKey = 'libs-snapshot-local'
          // The publisher username
          username = "${artifactory_user}"
          // The publisher password
          password = "${artifactory_password}"

          // This is an optional section (relevant only when publishIvy = true) for configuring Ivy publication.
          ivy { 
              ivyLayout = '[organization]/[module]/ivy-[revision].xml'
              artifactLayout = '[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]'
              //Convert any dots in an [organization] layout value to path separators, similar to Maven's groupId-to-path conversion. True if not specified
              mavenCompatible = true
          }
        }
        // (default: true) Publish generated build-info file to Artifactory
        publishBuildInfo(true)
        // (default: 3) Number of threads that will work and deploy artifacts to Artifactory
        forkCount = 5
    }
}
build.gradle
artifactory {
    publish {
      contextUrl = 'http://127.0.0.1:8081/artifactory'
      repository {
        repoKey = 'libs-snapshot-local'
        username = "${artifactory_user}"
        password = "${artifactory_password}"
      }
    }
}
  • In addition to the required configuration above, you are required to configure what publications will be included in the artifactoryPublish task for each project.

⚙️ Task Configurations

You can configure your root project or/and under its submodules (projects) to control the task operation for specific project.

Configure the task attributes (or only part of them) for each project in the following way:

artifactoryPublish {
    // Specify what publications to include when collecting artifacts to publish to Artifactory
    publifications(
            // Publication can be specified as Object
            publishing.publications.ivyJava,
            // Publication can be specified as String
            'mavenJava',
            // If this plguin constant string is specified the plugin will try to apply all the known publications
            'ALL_PUBLICATIONS'
    )
    // Properties to be attached to the published artifacts.
    setProperties(mapOf(
            'qa.level' to 'basic',
            'dev.team' to 'core'
    ))
    // (default: false) Skip this task for the project (don't include its artifacts when publishing) 
    skip = true
    // (default: true) Publish generated artifacts to Artifactory, can be specified as boolean/string
    publishArtifacts = false
    // (default: true) Publish generated POM files to Artifactory, can be specified as boolean/string
    publishPom = false
    // (default: true) Publish generated Ivy descriptor files to Artifactory, can be specified as boolean/string
    publishIvy = false
}
build.gradle
artifactoryPublish {
    publifications('ALL_PUBLICATIONS')
  
    properties = ['qa.level': 'basic', 'dev.team' : 'core']
    // Properties can be also defined with closure in the format: configName artifactSpec, key1:val1, key2:val2
    properties {
      simpleFile '**:**:**:*@*', simpleFile: 'only on settings file'
    }
  
    skip = true
    publishArtifacts = false
    publishPom = false
    publishIvy = false
}

Defaults - Global task Configurations to apply to all the projects

You can specify the configurations of artifactoryPublish task as defaults under publish in the main artifactory convention.

artifactory {
    publish {
        repository {
            // Required repository information...
        }
        defaults {
            // artifactoryPublish task attributes...
        }
    }
}

This task configurations will be added to the specific tasks configurations and apply to all the projects, allowing you to configure global configurations at one place that are the same for all the projects instead of configuring them for each project.

⚙️ Build-Info Configurations

The build-info.json file will be generated at the root build folder as default place. You can configure and control the information and attributes by using the buildInfo closure.

import java.util.*

artifactory {
    publish {
        // Required publish information...
    }
    
    buildInfo {
        // Set specific build and project information for the build-info
        setBuildName('new-strange-name')
        setBuildNumber('' + Random(System.currentTimeMillis()).nextInt(20000))
        setProject('project-key')
        // Add a dynamic property to the build-info
        addEnvironmentProperty('test.adding.dynVar',Date().toString())
        // Generate a copy of the build-info.json file in the following path
        setGeneratedBuildInfoFilePath("/Users/gradle-example-publish/myBuildInfoCopy.json")
        // Generate a file with all the deployed artifacts information in the following path
        setDdeployableArtifactsFilePath("/Users/gradle-example-publish/myArtifactsInBuild.json")
    }
}
build.gradle
artifactory {
    buildInfo {
        // Add a dynamic property to the build-info
        addEnvironmentProperty('test.adding.dynVar',Date().toString())
        // Set specific build and project information for the build-info
        setBuildName('new-strange-name')
        setBuildNumber('' + Random(System.currentTimeMillis()).nextInt(20000))
        setProject('project-key')
    }
}
  • Alternatively to the closure you can configure the attributes by using the clientConfig.info object

⚙️ Client Configurations

Redefine basic properties of the build info object can be applied using the clientConfig object under the main artifactory convention.

import java.util.*

artifactory {
    publish {
        // Required publish information...
    }
    
    // (default: 300 seconds) Artifactory's connection timeout (in seconds).
    clientConfig.timeout = 600
    // (default: 0 retries) Artifactory's connection retires
    clientConfig.setConnectionRetries(4)
    // (default: false) Set to true to skip TLS certificates verification.
    clientConfig.setInsecureTls(false)
    // (default: false) Set to true to include environment variables while running the tasks
    clientConfig.setIncludeEnvVars(true)
    // Set patterns of environment variables to include/exclude while running the tasks
    clientConfig.setEnvVarsExcludePatterns('*password*,*secret*')
    clientConfig.setEnvVarsIncludePatterns('*not-secret*')
}
build.gradle
artifactory {
  clientConfig.setIncludeEnvVars(true)
  clientConfig.setEnvVarsExcludePatterns('*password*,*secret*')
  clientConfig.setEnvVarsIncludePatterns('*not-secret*')
  clientConfig.timeout = 600
  clientConfig.setInsecureTls(false)
}

Proxy Configurations

Optionally, if needed, you can use and configure your proxy information to use in the task.

artifactory {
    publish {
        // Required publish information...
    }
  
    proxy {
        host = "ProxyHost"
        port = 60
        username = "ProxyUserName"
        password = "ProxyPassword"
    }
}
build.gradle
artifactory {
    proxy {
        setHost('ProxyHost')
        setPort('ProxyPort')
        setUsername('ProxyUserName')
        setPassword('ProxyPassword')
    }
}
  • Alternatively to the closure you can configure the attributes by using the clientConfig.proxy object

⚙️ Using CI Server

Gradle project can be used with one of the Artifactory CI clients or plugins:

The Artifactory configuration in this case (repositories, Artifactory credentials, etc.) is done from the CI client UI. You can still add the artifactory closure to the build script and have default values configured there, but the values configured in the CI Server override them.


🚦 Troubleshooting

💡 Examples

The following are links to the build scripts of different types of projects that configured and uses the plugin.

Sample project that uses the Gradle Artifactory Plugin with Gradle Publications.

Sample project that configures the Gradle Artifactory Plugin with the Gradle Kotlin DSL.

We highly recommend also using our gradle project examples as a reference when configuring your build scripts.

🐞 Debug

We highly recommend running Gradle with the -d option to get useful and readable debug information if something goes wrong with your build.

🔥 Reporting Issues

Please help us improve the plugin by reporting issues you encounter.


🫱🏻‍🫲🏼 Contributions

We welcome pull requests from the community. To help us improve this project, please read our Contribution guide.