Deploy gradle project to Artifactory - gradle

for few days I was trying to deploy a gradle project to Artifactory, but any of my attempts were successful!
I am using Artifactory 6.1.0 and gradle on eclipe the oxygen release
I created an admin user and a gradle repository in Artifactory
I added the code [from generate setting of the repository] to the build.gradle file and here's the content
//In fact I am not sure if I need all of them!?
apply plugin: 'java-library'
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
buildscript {
repositories {
jcenter()
}
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"
}
artifactory {
contextUrl = "${artifactory_contextUrl}" //The base Artifactory URL if not overridden by the publisher/resolver
publish {
repository {
repoKey = 'xyz_reposit'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
}
resolve {
repository {
repoKey = 'gradle-dev'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'myGroup'
artifactId = 'myProject'
version = '1.0.0'
from components.java
}
}
}
and the gradle.properties is configured
artifactory_user=Admin
artifactory_password=password
artifactory_contextUrl=http://localhost:8081/artifactory
When I execute artifactoryDeploy from publishing here the result in the cmd
When I go back to Artifactory, nothing is published in the xyz repository!? would you please help me!? I am pretty sure that something is missing in the build.gradle code?!

What gradle of version are you using? I recommend updating to the latest 5.4.1
Second, you should also have to do a gradlew artifactoryPublish after doing the artifactoryDeploy.
To see the difference in commands run:
gradlew tasks

I had the similar issue, and what me helps most is a running example
https://github.com/jfrog/project-examples/tree/master/gradle-examples/gradle-android-example

Related

How to publish the Jar file into jfrog as a new version instead of deleting/overwriting the pervious one

I am trying to publish the jar file into jfrog. At first it works fine. But when I tried to deploy an artifact under the same path more than once, changing the application version as 2.0.0(before it was 1.0.0), it gives me this error.
"message" : "Not enough permissions to delete/overwrite artifact 'customer-service:customer-service/com/customer/service/core/1.0.0/customer_ervice_core-1.0.0.jar' (user: 'username' needs DELETE permission)."
This is how my build.gradle file looks like.
import org.springframework.boot.gradle.plugin.SpringBootPlugin
buildscript {
repositories {
jcenter()
}
dependencies {
//Check for the latest version here: http://plugins.gradle.org/plugin/com.jfrog.artifactory
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
}
}
plugins {
id 'org.springframework.boot' version '2.7.5' apply false
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'java'
id 'maven-publish'
id 'com.jfrog.artifactory' version '4.28.1'
}
allprojects {
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
}
publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'customer_service_core'
from components.java
pom {
name = 'Customer service core module'
description = 'Customer service core module'
}
}
}
}
artifactory {
contextUrl = "${artifactory_contextUrl}"
jar {
enabled = true
}
publish {
repository {
repoKey = 'customer-service'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
defaults {
// Reference to Gradle publications defined in the build script.
// This is how we tell the Artifactory Plugin which artifacts should be
// published to Artifactory.
publications('mavenJava')
publishArtifacts = true
// Properties to be attached to the published artifacts.
properties = ['qa.level': 'basic', 'dev.team': 'core']
// Publish generated POM files to Artifactory (true by default)
publishPom = true
}
}
}
group = 'com.customer.service'
version = '2.0.0'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// lombok dependencies
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}
I want to know is there any way to deploy our jar files as a new version without overwriting the pervious one? Do I always need to include a version number manually in the path to be deployed, if so how can I do it though this build.gradle file?
I think snapshot Repo maybe the solution and you can save many different versions as you like.
https://jfrog.com/knowledge-base/artifactory-how-does-the-max-unique-snapshots-parameter-work-in-artifactory/

Gradle will not publish artifact?

Gradle v4.10.2
I’m building a Gradle Java plugin, and it builds. However when I run ./gradlew publish it does nothing, i.e., the artifact doesn’t get published. Here’s my build.gradle file (I have all the variables defined in my gradle.properties file). Also, if I just run ./gradlew publish w/o running ./gradlew build first, it doesn’t run the build phase. What am I missing in my build.gradle file? Thanks.
plugins {
id 'java'
id 'maven'
id 'maven-publish'
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
group=project.groupId
version = '0.0.1'
jar {
manifest {
attributes 'artifactId': project.artifactId,
'groupId': project.groupId,
'version': project.version
}
baseName artifactId
doLast {
println "artifactId: $project.artifactId\ngroupId: $project.groupId\nversion: $version"
}
}
dependencies {
compile gradleApi()
}
// For publishigh to S3 maven repo
publishing {
repositories {
maven {
url "s3://" + s3_bucket
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}
RTFM. I was missing the publications block inside the publishing block. Here’s the whole block
publishing {
publications {
myLibrary(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "s3://" + s3_bucket
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}

gradle maven-publish plugin only publishing first sub-modules artifacts

I have a multi-module project as follow:
top-level-module
sub-module-1
sub-module-2
sub-module-3
Top level module gradle config looks like this:
...
def javaProjects() {
subprojects.findAll { new File(it.projectDir, 'src/main/java').directory }
}
configure(javaProjects()) {
apply plugin: "io.franzbecker.gradle-lombok"
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: "jacoco"
apply plugin: 'maven'
apply plugin: 'maven-publish'
...
publishing {
repositories {
maven {
url = ...
credentials {
username = ...
password = ...
}
}
}
}
}
sub-module-1 and sub-module-2 have a gradle config like this:
plugins {
id 'java'
id 'groovy'
id 'application'
id 'maven'
id 'maven-publish'
}
mainClassName = 'com.mycompany.MyClass'
applicationName = 'xxx-cli' // xxx-cli is different for both modules
...
publishing {
publications {
XXXCliTar(MavenPublication) { // XXXCliTar is different in two modules
artifact(distTar)
artifactId "${applicationName}"
}
}
}
When I use the publish task as follows:
gradle -i build publish
I find that only the artifacts from sub-module1 is published.
What is really odd about this is that this only happens when run in Jenkis job (on a linux slave). It dos not happen when run on my windows dev machine!
I am wondering why artifacts from sub-module2 is not published.
I think there is a bug in the maven-publish plugin. The workaround was to not define the publishing.repositories.maven config in root module and instead duplicating it in sub-module1 and sub-modules2 like this:
publishing {
publishing {
repositories {
maven {
url = ...
credentials {
username = ...
password = ...
}
}
}
}
publications {
XXXCliTar(MavenPublication) { // XXXCliTar is different in two modules
artifact(distTar)
artifactId "${applicationName}"
}
}
}
Make sure to not apply the maven-publish plugin in rootProject's common config for sub-modules.

Publish Java artifact to Maven Local with Gradle

I am facing a problem when trying to install a generated jar into my local Maven Repository. The message error just show me 'task 'publish' is not found'
I am using this Gradle Script:
buildscript {
ext {
springBootVersion = '1.3.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'maven-publish'
jar {
baseName = 'mongofoundry'
version = '1.0.0'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
Do you have some idea Why I am reading that error message?
Thanks.
UPDATED
Running the command as #RaGe mentioned, solved the problem:
gradle publishToMavenLocal
The correct task to publish artifacts to local maven is
gradle publishToMavenLocal
Check Maven locally
For developing and testing it is useful to check library locally
gradle settings for apply plugin: 'com.android.library' not apply plugin: 'java-library'(where you can use it by default)
apply plugin: 'maven-publish'
//simple settings
project.afterEvaluate {
publishing {
publications {
library(MavenPublication) {
//setGroupId groupId
setGroupId "com.company"
//setArtifactId artifactId
setArtifactId "HelloWorld"
version "1.1"
artifact bundleDebugAar
/* add a dependency into generated .pom file
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', 'com.company')
dependencyNode.appendNode('artifactId', 'HelloWorld-core')
dependencyNode.appendNode('version', '1.1')
}
*/
}
}
}
}
to run it using command line or find this command in Gradle tab
./gradlew publishToMavenLocal
Location
artefact will be added into .m2 folder
//Unix
~/.m2
//Windows
C:\Users\<username>\.m2
//For example
/Users/alex/.m2/repository/<library_path>/<version>/<name>.<extension>
build folder
<project_path>/build/outputs/<extension>
other repositories location
~/.gradle/caches/modules-2/files-2.1/<group_id>/<artifact_id>/<version>/<id>
//For example
/Users/alex/.gradle/caches/modules-2/files-2.1/com.company/HelloWorld/1.1/c84ac8bc425dcae087c8abbc9ecdc27fafbb664a
To use it add mavenLocal(). It is important to place it as a first item for prioritise it, which is useful for internal dependencies
buildscript {
repositories {
mavenLocal()
}
allprojects {
repositories {
mavenLocal()
}
}
and
dependencies {
implementation 'com.company:HelloWorld:+'
}
*Also remember if you use a kind of shared.gradle file (via apply from) you should set path which is relevant to project.gradle (not shared.gradle)
[iOS CocoaPod local]
This is how I did it with Kotlin DSL (build.gradle.kts) for my Android library:
plugins {
id("maven-publish")
// OR simply
// `maven-publish`
// ...
}
publishing {
repositories {
// Local repository which we can first publish in it to check artifacts
maven {
name = "LocalTestRepo"
url = uri("file://${buildDir}/local-repository")
}
}
publications {
// ...
}
}
You can create all the publications with the following command:
./gradlew publishAllPublicationsToLocalTestRepoRepository
Or just a single publication with this command:
./gradlew publishReleasePublicationToLocalTestRepoRepository
See Gradle documentations: Maven Publish Plugin for more information.
Add maven plugin to your project and then:
gradle clean install
Here is an alternative skeleton for Gradle 7.5.1 with Java 17
build.gradle
plugins {
id 'org.gradle.java'
id 'org.gradle.maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
withJavadocJar()
withSourcesJar()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'your-group'
artifactId = 'your-artifact'
version = "0.0.1"
from components.java
}
}
repositories {
mavenLocal()
}
}
Publishing
You can see more details on the publishing steps with --info
./gradlew --info publishToMavenLocal
Output Directory
Linux/macOS
/Users/<username>/.m2/repository/your-group/your-artifact/0.0.1
Windows
C:\Users\<username>\.m2\repository\your-group\your-artifact\0.0.1

Gradle: Cannot configure artifactory from an external build script

I am new to gradle and would like to access my artifactory repository from it. If I put all configurations into one build script, the build succeeds. Here are the relevant parts of my build.gradle:
allprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'artifactory'
}
// ...
buildscript {
repositories {
maven {
url 'http://repo.jfrog.org/artifactory/gradle-plugins'
}
maven {
url artifactory_contextUrl + 'plugins-release'
credentials {
username = artifactory_user
password = artifactory_password
}
}
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.16')
}
}
artifactory {
contextUrl = artifactory_contextUrl
publish {
repository {
repoKey = 'libs-release-local'
username = artifactory_user
password = artifactory_password
maven = true
}
}
resolve {
repository {
repoKey = 'libs-release'
username = artifactory_user
password = artifactory_password
maven = true
}
}
}
dependencies {
// My dependencies ...
}
// Rest of the build script ...
Now, I would like to pull out the artifactory part into a separate gradle script for better organization. This is where the build goes wrong. Quite surprisingly, I get the following error even if I copy my build.gradle to foo.gradle, and change build.gradle to just contain the single line
apply from: 'foo.gradle'
The error is
FAILURE: Build failed with an exception.
* Where:
Script '/path/to/my/project/foo.gradle' line: 5
* What went wrong:
A problem occurred evaluating script.
> Plugin with id 'artifactory' not found.
In case this is not a bug, can anyone please explain this behavior of gradle's apply from and propose a solution?
Thank you
The apply from part is parsed once the build script is already configured, so telling Gradle where to find the plugins with specific ID is too late. You'll have to keep the buildscript part in the script, or put it in the init script:
apply from : 'http://link.to/my/gradle.script'
You can also use the fully qualified class name to apply the plugins in your helper script:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "com.adaptc.gradle:nexus-workflow:0.5"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:2.2.4"
}
}
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
apply plugin: com.adaptc.gradle.nexusworkflow.NexusWorkflowPlugin
Note that Gradle won't find the plugins if you put quotes around the class name, as you would do normally with plugin names.
This is how I found the class name for the Artifactory plugin:
I downloaded the plugin which was thankfully open source.
I searched for the name of the plugin among the files and found
artifactory-puplish.properties.
It contained the following
property: implementation-class=org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
The source of nexus-workflow has no such properties file so I looked around until I found
plugins-gradle-master/nexus-workflow/src/main/groovy/com/adaptc/gradle/nexusworkflow/NexusWorkflowPlugin.groovy

Resources