Gradle: Cannot configure artifactory from an external build script - gradle

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

Related

How do I suppress POM and IVY related warnings in Gradle 7?

After upgrading to Gradle 7 I have many warnings like:
Cannot publish Ivy descriptor if ivyDescriptor not set in task ':myProject:artifactoryPublish' and task 'uploadArchives' does not exist.
Cannot publish pom for project ':myProject' since it does not contain the Maven plugin install task and task ':myProject:artifactoryPublish' does not specify a custom pom path.
The artifactoryPublish task works fine.
My Gradle script:
buildscript {
repositories{
maven {
url = '...'
credentials {
username '...'
password '...'
}
metadataSources {
mavenPom()
artifact()
}
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.24.12"
}
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
suppressAllPomMetadataWarnings()
}
}
}
group = '...'
artifactory {
contextUrl = '...'
publish {
repository {
repoKey = '...'
username = '...'
password = '...'
}
defaults {
publishConfigs('archives')
publishIvy = false
publications("mavenJava")
}
}
}
How do I disable those warnings?
It looks like you mixed between the old Gradle publish-configuration method and the new Gradle publication method.
You applied the maven-publish plugin which allows creating publications. In artifactory.default, you added the "mavenJava" publication as expected.
However, the archives publish-configuration doesn't exist in your build.gradle file. Basically, publish-configurations are created by the legacy maven plugin. The configured mavenJava publication does the same as the archives publish-configuration and therefore all of the JARs are published as expected.
To remove the warning messages you see, remove the publishConfigs('archives') from artifactory.default clause:
artifactory {
publish {
defaults {
publishConfigs('archives') // <-- Remove this line
publishIvy = false
publications("mavenJava")
}
}
}
Read more:
Gradle Artifactory plugin documentation
Example

Multiple Gradle Files in the same project using apply from

I'm trying to segregate the gradle tasks to respective gradle files.
build.gradle
plugins {
id 'org.openapi.generator' version '4.3.1'
}
apply from: "$projectDir/gradle/script/openapi.gradle"
openapi.gradle
task buildSampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = "spring"
inputSpec = "$rootDir/src/main/resources/sample.yaml".toString()
outputDir = "$buildDir/generated".toString()
modelPackage = "com.sample"
}
When gradle build is run, getting this error
A problem occurred evaluating script.
Could not get unknown property 'org' for root project 'sample' of type org.gradle.api.Project.
But If I move the content of openapi.gradle into build.gradle it works fine.
Not sure what is the issue, could anyone help here please?
You should add plugin dependencies before your task definition to your openapi.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.openapitools:openapi-generator-gradle-plugin:${openapiPluginDependencyVersion}"
}
}
apply plugin: "org.openapi.generator"
// your task goes here
task buildSampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
...
}
gradle.properties:
openapiPluginDependencyVersion=4.3.0

ScalaStyle using grade return an error

I'm looking for scalaStyle using gradle. Can you explain how to do it?
I've tried this link, but I get an error:
Error:(110, 0) Could not find method scalaStyle() for arguments
[build_5ig236mubh10t6rxyt8apdkfi$_run_closure4#46b9e954] on root
project .....
Here's is a sample build.gradle that uses scalaStyle plugin:
buildscript {
repositories {
maven {
url 'http://jcenter.bintray.com/'
}
}
dependencies {
classpath 'org.github.ngbinh.scalastyle:gradle-scalastyle-plugin_2.11:0.9.0' //version 1.0.0 is not published yet.
}
}
apply plugin: 'scalaStyle'
scalaStyle {
configLocation = '/path/to/scalaStyle.xml'
includeTestSourceDirectory = true
source = 'src/main/scala'
testSource = 'src/test/scala'
}
You need to define buildscript block to declare dependencies for the script itself. When it's done a plugin needs to be applied. Finally you can use scalaStyle block to configure the plugin's behaviour.

Artifact is not published using gradle and artifactory plugin

I'm trying to upload a simple zip to artifactory using gradle. This is my script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
def user = "$System.env.ARTIFACTORY_USER"
def pass = "$System.env.ARTIFACTORY_PASSWORD"
task createArchive(type: Zip){
baseName 'myzip'
destinationDir(new File('target'))
from '.'
exclude 'target'
exclude '.gradle'
exclude 'build.gradle'
exclude 'README.md'
doFirst{
new File('target').mkdirs()
}
}
task clean(type: Delete){
delete 'target'
}
publishing {
publications {
customArtifact(MavenPublication) {
artifact createArchive
}
}
}
artifactory {
publish {
contextUrl = 'http://myrepo/artifactory'
repository {
repoKey = "myrepo-tools"
username = user
password = pass
}
publications ('customArtifact')
}
}
The code seems to work fine but actually only the build info are published. The zip is not:
Deploying build descriptor to: http://myrepo/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://myrepo/artifactory/webapp/builds/myproject/1503338121156
My code does not seem different from the one published here: How to build Groovy JAR w/ Gradle and publish it to in-house repo (which was pointed as reference in question nothing published to artifactory using gradle artifactory plugin) but I'm not sure that is still valid since is quite old.
The main difference is that almost all the examples are using java plugin so they have the from components.java line in the publications. In my case I cannot use it as I have a custom zip.. but I can't find what else can be changed.
Thanks,
Michele.

Unable to reference gradle.properties in script plugin

I am creating a script plugin to reference the ivy repository holding my orgs gradle plugins. My code right now is:
repository.gradle
repositories {
ivy {
credentials {
username = artifactory_user
password = artifactory_password
}
url 'https://ourUrl/artifactory/repoName'
layout "pattern", {
ivy '[organization]/[module]/[revision]/ivy-[revision].xml'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision].[ext]'
}
}
}
Then, in the build.gradle file,
build.gradle
buildscript {
apply from: https://ourUrl/assets/repository.gradle, to: buildscript
dependencies { classpath group: 'ourGrp', name: 'artifactName', version: '1.0.0' }
}
In my gradle.properties file:
gradle.properties
artifactory_user=username
artifactory_password=password
The error message I recieve is this:
What went wrong:
A problem occurred evaluating script.
Could not find property 'artifactory_user' on Credentials [username: null].
Any suggestions for how I can resolve this? I would like to avoid any further impact to the build.gradle file if possible.
This exact question was asked in the gradle forums. I'll paste the working workaround so it won't get lost during relinking or something:
repository.gradle:
repositories {
ivy {
credentials {
username = artifactory_user
password = artifactory_password
}
url 'https://ourUrl/artifactory/repoName'
layout "pattern", {
ivy '[organization]/[module]/[revision]/ivy-[revision].xml'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision].[ext]'
}
}
}
ext.extRepo = repositories
build.gradle:
buildscript {scriptHandler->
apply from: 'https://ourUrl/assets/repository.gradle'
repositories.addAll(extRepo)

Resources