Could not get unknown property 'mavenUser' for Credentials [username: null] - gradle

I've got this error : Could not get unknown property 'mavenUser' for Credentials [username: null] of type org.gradle.internal.credentials.DefaultPasswordCredentials_Decorated during the gradle build, although I've already created a gradle properties file with the right credentials in the gradle home directory
buildscript {
ext {
nexusBaseUrl = 'http://....'
// Dependencies version
springBootVersion = '2.7.2'
sonarqubeVersion = '3.3'
mybatisSpringBootStarterTestVersion = '2.2.2'
mybatisSpringBootStarterVersion = '2.2.2'
}
repositories {
maven {
url "${nexusBaseUrl}/mha-maven-public/"
allowInsecureProtocol = true
credentials {
username "${mavenUser}"
password "${mavenPassword}"
}
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:$sonarqubeVersion"
}
}

Related

Unable to publish a gradle Jar artifact to a sonatype repository

I am following instructions in the Sonatype documentation for publishing a gradle project.
I have the following gradle version.
$ ./gradlew --version
------------------------------------------------------------
Gradle 7.6
------------------------------------------------------------
Build time: 2022-11-25 13:35:10 UTC
Revision: daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8
Kotlin: 1.7.10
Groovy: 3.0.13
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 1.8.0_181 (Oracle Corporation 25.181-b13)
OS: Mac OS X 10.16 x86_64
When I try to use the uploadArchives task as given in their example, I get the following error:
$ ./gradlew uploadArchives
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/ferozed/stash/libs/mask-json-field-transform/build.gradle' line: 167
* What went wrong:
A problem occurred evaluating root project 'mask-json-field-transform'.
> Could not find method uploadArchives() for arguments [build_6yphclnk6m8p3rtmq5h7m56li$_run_closure12#19fbeecd] on root project 'mask-json-field-transform' of type org.gradle.api.Project.
This is my build.gradle file
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'gradle.plugin.com.github.johnrengelman:shadow:7.1.2'
}
}
plugins {
id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies
id 'maven-publish'
id 'signing'
}
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
jcenter()
mavenCentral()
maven {
url = "https://packages.confluent.io/maven"
}
maven {
url = "https://jitpack.io"
}
}
group = 'io.github.ferozed.kafka.connect'
version = '0.1'
dependencies {
// Kafka
implementation group: 'org.apache.kafka', name: 'connect-api', version: '3.3.1'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.1'
implementation 'com.github.jcustenborder.kafka.connect:connect-utils:0.7.173'
implementation 'com.github.jcustenborder.kafka.connect:kafka-connect-transform-common:0.1.0.14'
//test
testImplementation(platform('org.junit:junit-bom:5.9.0'))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation("io.mockk:mockk:1.9.2")
}
sourceSets {
main {
java {
srcDirs = ["src/main/java"]
}
resources {
srcDirs = ["src/main/avro", "src/main/resources"]
}
}
test {
java {
srcDirs = ["src/test/java"]
}
}
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
tasks.withType(AbstractArchiveTask).configureEach {
preserveFileTimestamps = false
reproducibleFileOrder = true
}
plugins.withId("com.github.johnrengelman.shadow"){
//this block requires the java plugin to be applied first.
plugins.withId("java"){
shadowJar {
//We are overriding the default jar to be the shadow jar
classifier = null
exclude 'META-INF'
exclude 'META-INF/*.INF'
exclude 'META-INF/license/*'
}
jar {
manifest {
attributes(
'Built-By' : System.properties['user.name'],
'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
'Created-By' : "Gradle ${gradle.gradleVersion}",
'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
)
}
}
tasks.build.dependsOn tasks.shadowJar
tasks.shadowJar.mustRunAfter tasks.jar
tasks.shadowJar.mustRunAfter tasks.javadocJar
tasks.shadowJar.mustRunAfter tasks.sourcesJar
}
}
publishing {
publications {
mavenJava(MavenPublication) {
pom {
name = 'Mask Json Field Transform'
description = 'A kafka connect transform to remove the value of a sensitive field in a json document.'
url = 'https://github.com/ferozed/mask-json-field-transform'
properties = [
myProp: "value",
"prop.with.dots": "anotherValue"
]
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'ferozes'
name = 'Feroze Daud'
email = 'ferozed.oss#gmail.com'
}
}
scm {
connection = 'scm:git:git#github.com:ferozed/mask-json-field-transform.git'
developerConnection = 'scm:git:git#github.com:ferozed/mask-json-field-transform.git'
url = 'https://github.com/ferozed/mask-json-field-transform'
}
}
}
}
}
signing {
sign configurations.archives
sign publishing.publications.mavenJava
}
tasks.signArchives.dependsOn tasks.shadowJar
artifacts {
archives javadocJar, sourcesJar
}
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { deployment -> signing.signPom(deployment) }
repository(url: "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
snapshotRepository(url: "https://s01.oss.sonatype.org/content/repositories/snapshots/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
pom.project {
name 'Example Application'
packaging 'jar'
// optionally artifactId can be defined here
description 'A kafka connect transform to remove the value of a sensitive field in a json document.'
url 'https://github.com/ferozed/mask-json-field-transform'
scm {
connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/'
url 'https://github.com/ferozed/mask-json-field-transform'
}
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'ferozed'
name 'Feroze Daud'
email 'feroz#gmail.com'
}
}
}
}
}
}
What am I doing wrong?
First and foremost, you may not need to 'fix the error' so to speak, in that it appears the problematic code MAY be able to be outright deleted. However...
To fix the error...
Could not find method uploadArchives() for arguments [build_6yphclnk6m8p3rtmq5h7m56li$_run_closure12#19fbeecd] on root project 'mask-json-field-transform' of type org.gradle.api.Project.
This is telling us that uploadArchives is not valid. See immediately below.
I believe you may need to use the maven plugin to access this functionality.
https://github.com/siddeshbg/gradle-uploadArchives/blob/master/README.md
to do this, add:
id 'maven'
to your plugins{}
block.
The above example explains that the maven plugin is responsible for doing this
The Maven plugin adds support for deploying artifacts to Maven repositories
It is worth considering also that the author of the above repository claims:
This is the legacy publishing mechanism, should not be used in newer
builds
Now... on to the better solution, forgetting the maven plugin, in favor of the maven-publish plugin you're already using...
I do not personally work with Maven often enough to know what other mechanisms one should use, but Google suggests that perhaps the maven-publish plugin you're using might do this WITHOUT the 'uploadArchives{}' portion of your code that is causing the error.
See:
https://docs.gradle.org/current/userguide/publishing_maven.html
Per this page, it would seem the entire uploadArchives block is unnecessary

Maven publish plugin complains no username provided but its there

I am trying to push a jar to artifactory, i get the following error from the gradle script:
But the username is there, is hardcoded to "aws":
publishing {
publications {
publicMyArtifact(MavenPublication) {
groupId "com.company.project"
artifactId "artifact"
version = "1.0"
afterEvaluate {
artifact "repo/path/*.jar"
}
}
}
repositories {
maven {
name "reponame"
url "https://accountnumber.d.codeartifact.eu-central-1.amazonaws.com/maven"
credentials {
username = "aws"
password = System.getenv("CODEARTIFACT_AUTH_TOKEN")
}
}
}
}
I invoke this task with the following line in jenkins: sh "./gradlew publish"
Any idea what could be possibly the reason?

Configure description for package published to the GitHub Packages Gradle registry

When publishing a package to the GitHub Packages Gradle registry, the resulting package's description is empty.
Is it possible to set the package description via config?
I have the following (abridged) configuration in build.gradle.kts, I was assuming it would use pom.description or the repo's README.md as the description but that doesn't seem to be the case.
plugins {
`maven-publish`
}
publishing {
publications {
create<MavenPublication>("mavenJava") {
groupId = project.group.toString()
artifactId = rootProject.name
version = project.version.toString()
from(components["java"])
pom {
name.set("steam-webapi-kt")
description.set("Steam WebAPI wrapper in Kotlin and Ktor")
url.set("https://github.com/j4ckofalltrades/steam-webapi-kt")
}
}
}
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/j4ckofalltrades/steam-webapi-kt")
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
Sample package: https://github.com/j4ckofalltrades/steam-webapi-kt/packages/899640

Gradle Artifactory deploy fails when using generated gradle.properties

I have the following build.gradle file:
plugins {
id 'java-gradle-plugin'
id 'com.gradle.plugin-publish' version '0.10.1'
id 'groovy'
id 'maven-publish'
id "com.jfrog.artifactory" version "4.9.1"
}
group = 'de.gafertp'
version = '2.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'net.sourceforge.plantuml:plantuml:1.2019.1'
testCompile 'org.junit.jupiter:junit-jupiter-api:5.4.0'
testCompile 'org.junit-pioneer:junit-pioneer:0.3.0'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.4.0'
}
gradlePlugin {
plugins {
plantUmlPlugin {
id = 'de.gafertp.plantuml'
displayName = 'Gradle PlantUML Plugin'
description = 'A very simple plugin to render PlantUML files. ' +
'Takes a set of diagram files together with desired output files / formats ' +
'and renders them with PlantUML (http://plantuml.com/).'
implementationClass = 'de.gafertp.PlantUmlPlugin'
}
}
}
pluginBundle {
website = 'https://github.com/codecholeric/gradle-plantuml-plugin'
vcsUrl = 'https://github.com/codecholeric/gradle-plantuml-plugin'
tags = ['plantuml']
}
publishing {
publications {
plantUmlPluginJar(MavenPublication) {
from components.java
}
}
}
artifactory {
contextUrl = "${artifactory_contextUrl}"
publish {
repository {
repoKey = 'gradle-dev-local'
username = "${artifactory_user}"
password = "${artifactory_password}"
println "username=${artifactory_user}"
println "password=${artifactory_password}"
maven = true
}
defaults {
publications('plantUmlPluginJar')
}
}
resolve {
repository {
repoKey = 'gradle-dev'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
}
}
test {
useJUnitPlatform()
}
And this gradle.properties file:
artifactory_user=${security.getCurrentUsername()}
artifactory_password=${security.getEncryptedPassword()!"blahblah"}
artifactory_contextUrl=http://localhost:8081/artifactory
The build fails with the following error:
Execution failed for task ':artifactoryDeploy'.
> java.io.IOException: Failed to deploy file. Status code: 401 Response message: Artifactory returned the following errors:
Unauthorized Status code: 401
If we take a look at the printed username and password, we see this:
username=${security.getCurrentUsername()}
password=${security.getEncryptedPassword()!"blahblah"}
Gradle takes the properties values as literal strings instead of evaluating them. So the issue seems to be that the security object cannot be found, because if I replace the properties in build.gradle file with their values directly, the following error occurs:
A problem occurred evaluating root project 'gradle-plantuml-plugin'.
> Could not get unknown property 'security' for object of type org.jfrog.gradle.plugin.artifactory.dsl.DoubleDelegateWrapper.
What am I doing wrong? The deploy works fine when using plaintext username and password, but I would like to use the auto-generated way of logging in (using an encrypted password).
It seems that gradle.properties should look like this:
username=your_username
password=blahblah
We suppose that blahblah is your encrypted password.

Publish source jar when reading external .gradle file

I have a git submodule (of which I have no control of) in my project which contains file named publish.gradle. The contents of this file are:
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
version = System.getenv()['VERSION'] ?: version
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
artifactory {
contextUrl = repoBaseUrl
publish {
String publishRepoKey = version.endsWith('SNAPSHOT') ? repoKeySnapshot : repoKeyRelease
repository {
repoKey = publishRepoKey // The Artifactory repository key to publish to
username = System.getenv()['ARTIFACTORY_USERNAME'] ?: artifactoryUser // The publisher user name
password = System.getenv()['ARTIFACTORY_PASSWORD'] ?: artifactoryPassword // The publisher password
}
defaults {
publications('mavenJava')
publishArtifacts = true
publishPom = true
}
}
resolve {
repoKey = repoKey
}
}
In my build.gradle file I have the following line:
apply from: "${rootDir}/submodule/gradle/publish.gradle"
When I try to add the following line after this line:
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing.publications.mavenJava.artifact sourceJar {
classifier 'sources'
}
Then the correct things get published, except that the pom file in the repository only contains a <dependencyManagement/> section and all the dependencies are missing. When I remove the above lines from build.gradle the pom is correct.
Try to override the all publishing block with :
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}

Resources