Pushing a file to Jfrog artifactory each time build runs - gradle

import groovy.json.*
subprojects {
afterEvaluate {
if (eventsJsonProjectsName.contains(project.name)) {
def Projectgroup = ''
for (i in group)
if (i == '.') Projectgroup += '/'
else Projectgroup += i
File file = new File(projectDir, "${project.name}.json")
file.createNewFile()
def data = JsonOutput.toJson(
ApplicationName: project.name,
ApplicationVersion: project.version,
Modality: "IGT",
ArtifactoryPath: artifactory_publish_contextUrl + '/' +
artifactory_publish_repoKey + '/' + Projectgroup + '/' +
project.name + '-' + project.version + '/' +
project.name + '-' + project.version + '.jar'
)
file.text = data
}
}
}
enter image description here
I actually working on multiple projects and each sub project have its own build and this code actually creates json for each builds and store as a .json file.
And I actually these files to Jfrog for each project
afterEvaluate {
subprojects {
if (eventsJsonProjectsName.contains(project.name)) {
repositories {
maven {
url "${artifactory_publish_contextUrl}/ASP/CI-CD-Test"
}
}
apply plugin: "com.jfrog.artifactory"
apply plugin: "java"
apply plugin: "maven-publish"
dependencies {
testImplementation "junit:junit:4.7"
}
publishing {
publications {
mavenJava(MavenPublication) {
// from components.java
artifact file("${project.name}.json")
}
}
}
artifactory {
contextUrl = "${artifactory_publish_contextUrl}
publish {
repository {
repoKey = "${artifactory_publish_repoKey}"
username = "${artifactory_publish_user}"
password = "${artifactory_publish_password}"
}
}
}
}
}
}
enter image description here
I actually use this block of code for publishing it to Jfrog. But it is not reflecting in Jfrog. I want to know what is wrong with this.
I am stuck with it for a long time now.please help me with this.
I want upload the JSON file to artifactory each time build runs

Related

com.jfrog.artifactory gradle plugin 401 Unauthorised

I have previously successfully set up bintray and artifactory accounts to publish snapshot versions to the OSS JFrog Artifactory repository, but after setting up a GitHub/Bintray/Artifactory organisation under the same user, I am unable to publish snapshots.
When attempting to run
./gradlew artifactoryPublish -Dsnapshot=true -DbintrayUser=myBintrayUser -DbintrayKey=myBintrayApiKey -DbuildNumber=#
I get the following error:
java.io.IOException: Failed to deploy file. Status code: 401 Response message: Artifactory returned the following errors:
Unauthorized Status code: 401
I've tried using both bintray users (my personal and the organisation) but get the same response. I've also tried regenerating a new API key at https://bintray.com/profile/edit, but has not worked (and now also seems to be out of sync with the key at https://oss.jfrog.org/artifactory/webapp/#/profile) which I can't edit.
The build.gradle file is:
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
}
plugins {
id 'java-library'
id 'maven'
id 'maven-publish'
// Automatic SEMVER
// ./gradlew release
id 'net.vivin.gradle-semantic-build-versioning' version '4.0.0' apply false
// SNAPSHOT publishing to oss-jfrog-artifactory
// ./gradlew artifactoryPublish -Dsnapshot=true -DbintrayUser=<YOUR_USER_NAME> -DbintrayKey=<YOUR_API_KEY> -DbuildNumber=NNN
id 'com.jfrog.artifactory' version '4.6.2'
// RELEASE publishing to bintray
// ./gradlew bintrayUpload -DbintrayUser=<YOUR_USER_NAME> -DbintrayKey=<YOUR_API_KEY>
id 'com.jfrog.bintray' version '1.8.1'
}
wrapper.gradleVersion = '4.5.1'
def groupName = 'noxtech'
group = 'uk.co.noxtech'
archivesBaseName = 'noxtech-java-utils'
description = 'Assorted Java 8 utilities'
def projectUrl = "https://github.com/noxtech/noxtech-java-utils"
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
api 'joda-time:joda-time:2.9.9'
implementation 'org.projectlombok:lombok:1.16.20'
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-all:1.3'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
javadoc.failOnError = false
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
def pomConfig = {
licenses {
license {
name "The Apache Software License, Version 2.0"
url "http://www.apache.org/licenses/LICENSE-2.0.txt"
distribution "repo"
}
}
scm {
url projectUrl
}
}
publishing {
publications {
mavenPublication(MavenPublication) {
from components.java
artifact sourcesJar {
classifier "sources"
}
artifact javadocJar {
classifier "javadoc"
}
groupId = project.group
artifactId = project.archivesBaseName
version = project.version.toString()
pom.withXml {
def root = asNode()
root.appendNode('description', project.description)
root.appendNode('name', project.name)
root.appendNode('url', projectUrl)
root.children().last() + pomConfig
}
}
}
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url "$buildDir/repo"
}
}
}
bintray {
user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
key = project.hasProperty('bintrayKey') ? project.property('bintrayKey') : System.getenv('BINTRAY_KEY')
publications = ['mavenPublication']
pkg {
repo = "maven"
name = project.archivesBaseName
userOrg = groupName
licenses = ['Apache-2.0']
websiteUrl = projectUrl
vcsUrl = projectUrl + '.git'
issueTrackerUrl = projectUrl + '/issues'
version {
name = project.version.toString()
desc = project.description
vcsTag = project.version.toString()
released = new Date()
}
}
}
artifactory {
contextUrl = 'http://oss.jfrog.org'
publish {
repository {
repoKey = 'oss-snapshot-local'
username = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
password = project.hasProperty('bintrayKey') ? project.property('bintrayKey') : System.getenv('BINTRAY_KEY')
}
defaults {
publications('mavenPublication')
publishArtifacts = true
publishPom = true
}
}
resolve {
repoKey = 'jcenter'
}
clientConfig.info.setBuildNumber(project.hasProperty('buildNumber') ? project.property('buildNumber') : System.getenv('BUILD_NUMBER'))
}
This turned out to be a simple solution. When moving over to use the organisation from the personal account on CircleCI, the environment variables were lost.

New version of library synchronized with jcenter

I have a library in Bintray synchronized with JCenter. https://bintray.com/bmsolution/Android/RestManager/0.2
But when I uploaded new version by gradle plugin - it is on Bintray (link) but it isn't on jcenter (link)
Do you have to wait for synchronization so long or do I do something wrong?
Fragment of my gradle file responsible for upload:
def libVersion = "0.2"
ext {
PUBLISH_GROUP_ID = 'pl.bms'
PUBLISH_ARTIFACT_ID = 'network'
PUBLISH_VERSION = libVersion
}
apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
publishing {
publications {
MyPublication(MavenPublication) {
artifact sourcesJar
artifact("$buildDir/outputs/aar/network-release.aar")
groupId PUBLISH_GROUP_ID
artifactId PUBLISH_ARTIFACT_ID
version PUBLISH_VERSION
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
// Iterate over the implementation dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.implementation.allDependencies.each {
// Ensure dependencies such as fileTree are not included in the pom.
if (it.name != 'unspecified') {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}
bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_KEY')
publish = true
publications = ['MyPublication']
override = true
pkg {
repo = "Android"
name = "RestManager"
userOrg = 'bmsolution'
licenses = ['Apache-2.0']
vcsUrl = "https://bitbucket.org/libandroid/rest-manager"
issueTrackerUrl = "https://bitbucket.org/libandroid/rest-manager/issues"
version {
name = libVersion
released = new Date()
vcsTag = libVersion
}
}
}
If you didn't change any group/artifactId, then based on this post I'd recommend to wait ~24 hours for it to syncronize and then contact support#jfrog.com.
If you changed your group/artifactId then you to relink bintray and jcenter which could also be done with a mail to support or by republishing.

how to write script in gradle to update svn project

I am trying to learn Gradle and,
I try to build this code but it wasn't working.
can someone tell me where is it failing?
import org.tmatesoft.svn.core.wc2.*
import org.tmatesoft.svn.core.wc.*
import org.tmatesoft.svn.core.*
allprojects {
repositories {
jcenter { url "http://jcenter.bintray.com"}
mavenCentral()
}
dependencies {
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.7.11'
}
}
task svnCommitFile(){
description = "Commits a single file to an SVN repository"
doLast{
if (!project.hasProperty("commitMsg")){
ext.commitMsg = "None"
}
SvnOperationFactory svnOperationFactory = new SvnOperationFactory()
def authentication = SVNWCUtil.createDefaultAuthenticationManager(svnUser, svnPassword)
svnOperationFactory.setAuthenticationManager(authentication)
try {
SvnCommit commit = svnOperationFactory.createCommit()
commit.setSingleTarget(SvnTarget.fromFile(new File(fileToCommit)))
commit.setCommitMessage(commitMsg)
SVNCommitInfo commitInfo = commit.run()
println "Commit info: " + commitInfo
println "Commit message: " + commitMsg
} finally{
svnOperationFactory.dispose()
}
}
}

gradle uploadArchives for multiple subproject with different maven group and version

I have the following build.gradle and it works for the individual sub project (or all of them if I copy paste it):
def group = 'com.my.pgk'
def artifact = project.name
def version = '1.0.0'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
uploadArchives {
repositories {
mavenDeployer {
def finalPath = 'file://' + LocalMavenRepoPath.toString()
println group
println finalPath
repository url: finalPath
pom.groupId = group
pom.artifactId = artifact
pom.version = version
pom.packaging = 'jar'
}
}
}
jar {
archiveName = artifact + "-" + version + ".jar"
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
archiveName = artifact + "-v" + version + "-src.jar"
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
archiveName = artifact + "-v" + version + "-doc.jar"
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
It is doing exactly what I want it to...
Trying to move this to the root project build.gradle doesn't because the variables defined in the subprojects are not updated when uploadArchives is called.
How can I work around this?
You need the subprojects to be configured first so that gradle is aware of the subproject variables before the root build.gradle is configured. You can force a bottom-up configuration by using:
evaluationDependsOnChildren()
In a project with a single root and two subprojects, you can create a common gradle script in the root project and apply it to each of the subprojects.
Root Project:
build.gradle
//nothing to see here
Root Project: common.gradle
if (!project.hasProperty("commonVar")) {
ext.commonVar = "unset"
}
task printCommonVar() {
println commonVar
}
Root Project: settings.gradle
rootProject.name = 'GradleGroupProject'
// Find the directories containing a "build.gradle" file in the root directory
// of the project. That is, every directory containing a "build.gradle" will
// be automatically the subproject of this project.
def subDirs = rootDir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (!file.isDirectory()) {
return false
}
if (file.name == 'buildSrc') {
return false
}
return new File(file, 'build.gradle').isFile()
}
});
subDirs.each { File dir ->
include dir.name
}
Subproject 1: build.gradle
ext.commonVar = "subproject1"
apply from: rootProject.file('common.gradle')
Subproject 2: build.gradle
ext.commonVar = "subproject2"
apply from: rootProject.file('common.gradle')
The ordering between ext.commonVar = "subproject2" and apply from: rootProject.file('common.gradle') is important.

How to publish(deploy) multiple zip files to artifactory using gradle?

I would like to upload multiple zip files(from different directories) to artifactory using gradle script. I was able to upload single distribution using below code.
distributions {
main {
baseName = 'sample'
contents {
from{'src/main/dist/sample'}
}
}
}
publishing {
repositories {
maven {
url "${artifactoryURL}/myFiles"
credentials {
username artifactoryUser
password artifactoryPassword
}
}
}
publications {
distribution(MavenPublication) {
groupId 'com.test'
artifactId 'sample'
version version
artifact (distZip) {
}
}
}
}
Please help in uploading multiple zip files from different source ("src/main/dist/second").
This is full example of build.gragle. To publish zips you should run distZip artifactoryPublish
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
apply plugin: 'distribution'
//Defining list of our distributions, every value is map which contains value and version of every distribution
def dists = [
[name:'sample', version: 1],
[name:'second', version: 1]
]
distributions {
//Creating distribution from each value of the distributions list defined above
dists.each { dist ->
"$dist.name" {
baseName = "$dist.name"
contents {
from{"src/main/dist/${dist.name}"}
}
}
}
}
//Every new distribution creates task "${distributionName}DistZip" so we make posible to run them with base task distZip
distZip.dependsOn {
tasks.findAll{ task -> task.name.endsWith('DistZip') }
}
publishing {
publications {
//Again iterating list to make publication for every distribution
dists.each { dist ->
//Every publication has name of the distibution
"$dist.name"(MavenPublication) {
groupId "test"
version = dist.version
artifactId = dist.name
artifact("$buildDir/distributions/${dist.name}.zip")
}
}
}
}
artifactory {
contextUrl = "$URL"
publish {
repository {
repoKey = 'REPO_KEY'
username = USER_NAME
password = PASSWORD
}
defaults {
//Publish every distribution to Artifactory
dists.each { dist ->
publications(dist.name)
}
}
}
}

Resources