Unable to reference gradle.properties in script plugin - gradle

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)

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

Gradle single-project pluginManagement block not working (Kotlin DSL)

I need to change a multi-project build to a single-project build, as there is and only ever will be one project in this repo. Currently, in settings.gradle, I have a custom plugin repo that currently uses a pluginManagement block with resolutionStrategy and my list of repo's:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.meanwhileinhell.plugin') {
useModule('com.meanwhileinhell:gradle-plugin:1.0.0-SNAPSHOT')
}
}
}
repositories {
mavenLocal()
maven { url "https://repo.spring.io/milestone" }
maven { url "https://plugins.gradle.org/m2/" }
// Meanwhileinhell repo
maven {
url "s3://mvn.meanwhileinhell.com/releases"
credentials(AwsCredentials) {
accessKey s3_access_key
secretKey s3_access_secret
}
}
}
plugins {
...
...
}
}
However, deleting settings.gradle and moving this block into my build.gradle.kts (Kotlin DSL) seems to do nothing. I've tried wrapping in a
configurations {
all {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
and also
settings {
pluginManagement {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
I found a SO answer that used settingsEvaluated in order to get the settings object, but again this was a no go.
Currently my build.gradle.kts looks like this, without pulling any plugin in from my repo:
val springBootVersion: String by project
group = "com.meanwhileinhell.myapp"
version = "$version"
repositories {
mavenCentral()
mavenLocal()
maven ("https://repo.spring.io/snapshot")
maven ("https://repo.spring.io/milestone")
maven ("https://plugins.gradle.org/m2/")
maven {
url = uri("s3://mvn.meanwhileinhell.com/releases")
credentials(AwsCredentials::class) {
accessKey = (project.property("s3_access_key") as String)
secretKey = (project.property("s3_access_secret") as String)
}
}
}
plugins {
base
eclipse
idea
java
id("io.spring.dependency-management") version "1.0.9.RELEASE"
// Load but don't apply to root project
id("org.springframework.boot") version "1.5.14.RELEASE" apply false
}
dependencies {
...
}
Whenever I try to add a plugin id like id("com.meanwhileinhell.plugin.hell2java") version "1.0.0-SNAPSHOT" I get an error that looks like it isn't even looking in my S3 location:
* What went wrong:
Plugin [id: 'com.meanwhileinhell.plugin.hell2java', version: '1.0.0-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.meanwhileinhell.plugin.hell2java:com.meanwhileinhell.plugin.hell2java.gradle.plugin:1.0.0-SNAPSHOT')
Searched in the following repositories:
Gradle Central Plugin Repository
Any help on this would be appreciated!
EDIT !!!! -----------------------
I've just found this in the Gradle docs:
https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_management
The pluginManagement {} block may only appear in either the settings.gradle file....
Looks like I'm going down the wrong way entirely, so will look into the initialisation script route.
I think you may have missed something about the file structure.
In the Groovy DSL, you have the following files:
build.gradle
settings.gradle
init.gradle
In the Kotlin DSL, you have the same files but with the .kts extension:
build.gradle.kts
settings.gradle.kts
init.gradle.kts
The Kotlin DSL doesn't differ to the Groovy DSL in where to put things. pluginManagement need to go in to the settings file, so for Kotlin that would be settings.gradle.kts. If you are in doubt, look at the documentation. For almost all code examples, you can switch between Groovy and Kotlin DSL to see how to do it (and which files they are supposed go to into).

Gradle Liquibase Integration

I am trying to integrate Liquibase in my project using Gradle.
For that I made below changes to build.gradle but it gives me error as follows:
Below is my build.gradle file:
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.liquibase:liquibase-gradle-plugin:2.0.1"
}
}
allprojects {'
apply plugin: 'liquibase'
}
dependencies {
liquibaseRuntime 'org.liquibase:liquibase-core:3.6.1'
liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1'
liquibaseRuntime 'mysql:mysql-connector-java:5.1.34'
}
liquibase {
activities {
doFirst {
if (!project.hasProperty('runList')) {
project.ext.runList = "main,test"
}
}
main {
defaultsFile "$projectDir/sql-migration/mysql/app_mysql.properties"
logFile "$projectDir/sql-migration/mysql/logs/liquibase-" + new Date().format("yyyy-MM-dd_HH-mm-ss")+".log"
}
test {
defaultsFile "$projectDir/sql-migration/mysql/app_test_mysql.properties"
logFile "$projectDir/sql-migration/logs/liquibase-test-" + new Date().format("yyyy-MM-dd_HH-mm-ss")+".log"
}
}
// To execute liquibase on single environment, gradlew app:update -PrunList=test
runList = project.ext.runList
}
I am not able to get why it is not able to find out 'liquibaseRuntime' repositories.
Any help in this would be really appreciated.
Assuming that you have a changelog file, make sure that the changelog file is properly referenced from build.gradle.
Also I would suggest defining the liquibase.url, username and password.
Refer to this post for detailed implementation:
https://dzone.com/articles/managing-your-database-with-liquibase-and-gradle
The error message
Cannot resolve external dependency [...] because no repositories are defined
indicates that you're missing a repositories block. You have one in your buildscript, but you probably need to move it to the top-level, as shown here.

Gradle: How can I configure repositories in buildscript block of external script

In external script common/buildversion.gradle I have:
buildscript {
// Copy repositories definitions from this buildscript to all projects
(allprojects*.repositories + [repositories]).each {
it.configure {
apply from: rootProject.file('../../common/repositories.gradle')
}
}
dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2" }
}
apply plugin: org.sonarqube.gradle.SonarQubePlugin
In common/repositories.gradle:
repositories{
maven { url "https://plugins.gradle.org/m2/" }
}
I am getting error:
Cannot resolve external dependency
sonarqube-gradle-plugin because no repositories are
defined.
Here is a way to have a single source for repositories definitions.
declare the repositories as an "ext" variables
// repositories.gradle
ext.repos = {
maven {
name "repo1"
url "repo1_url"
}
maven {
name "repo2"
url "repo2_url"
}
}
"apply" in your build.gradle
// build.gradle
apply from: "${project.projectDir}/repositories.gradle"
use variable in repositories declaration
// build.gradle
repositories repos
Externalizing sections of the buildScript block into other scripts is not supported. There is an open defect, you should vote on it.

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