How to resolve "Could not get unknown property 'shadow' for project of type org.gradle.api.Project." - spring

plugin used : id 'com.github.johnrengelman.shadow' version '4.0.4'
Gradle : https://services.gradle.org/distributions/gradle-7.4.2-bin.zip
getting error :
A problem occurred evaluating root project 'iot-bulk-slice-worker'.
> Could not get unknown property 'shadow' for project of type org.gradle.api.Project.
for :
plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4'
//id 'com.github.johnrengelman.shadow' version '7.1.1'
}
if (subproject.name in gradle.platforms) {
subproject.publishing {
publications {
code(MavenPublication) { publication ->
publication.artifactId = gradle.platforms ['artifactName']
from components.java
}
shadow(MavenPublication) { publication ->
publication.artifactId ="${subproject.name}"
subproject.shadow.component(publication)
}
}
}
}
}

It is still a little difficult to say without the complete build file. But I assume the subproject variable is exactly that - a subproject. In that case, you are configuring a publication on a subproject that uses the shadow extension from itself.
This means to shadow plugin has to be applied to the subproject. From the snippets, you are only applying it to the root project.
Could be something like this:
plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4' apply false // <-- Set to false if the root project does not itself need to be "shadowed". This still makes the plugin available on the build classpath for subprojects.
}
subprojects { subproject ->
if (...) {
subproject.apply(plugin: 'com.github.johnrengelman.shadow')
// publications here...
}
}

Related

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

new gradle format confusion and migrating causes Could not find method testCompile()

I have a build.gradle with
plugins {
id {some plugin for all projects}
id "com.diffplug.spotless" version "5.1.1"
}
AND THEN I have an allprojects {} section that defines ONE apply plugin: 'jacoco' and a subprojects {} section that declares apply plugin: 'java' with a few others
Immediately adding spotless messed with stuff and errors out that it cannot find the java plugin so then I modify ALL plugins to be in the plugins section like so
plugins {
id "java"
id "checkstyle"
id "eclipse"
id "idea"
id "jacoco"
id "com.diffplug.spotless" version "5.1.1"
id "com.dorongold.task-tree" version "1.5" //This prints out a task tree (pretty print)
}
This then results in this error
Could not find method testCompile() for arguments [junit:junit:4.11] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
So for some reason the java plugin is lost. I can't figure out the right combination here to migrate everything to this new plugins section format.
How do I do that? I randomly tried putting a plugins section in allprojects and subprojects but that results in this new error
Could not find method plugins() for arguments [build_d8c2jgy4ua1m0vkv9kmvgefmc$_run_closure2$_closure5#62337fda] on root project 'providersvc-all' of type org.gradle.api.Project
How does this new plugins section work? I can't seem to migrate without it breaking everything. I just want java plugin, testCompile, and spotless to play nicely together right now
EDIT(forgot to attach the full trimmed down file that does not work):
plugins {
id "java"
id "com.diffplug.spotless" version "5.1.1"
}
ext {
//dependency versions every project usees so we version in one location all jars(less jar hell this way)
deps = [
'junit': 'junit:junit:4.11'
]
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven {
//webpieces VERSIONED snapshots so you can lock on a snapshot
url "https://dl.bintray.com/deanhiller/maven"
}
//For testing locally
maven {
url uri('/tmp/myRepo/')
}
}
}
subprojects {
dependencies {
testCompile deps['junit']
}
}
thanks,
Dean
You are only applying the plugins to the root project - not the sub-projects. However, if you like to configure plugins through the subprojects configuration, you have to use the apply plugin syntax. But you don't have to use the old buildscript block for configuring the classpath and repositories if you a combination of the two.
Here is an example. I am assuming the root project is not a Java project. I have also removed your comments and inserted mine instead for the sole reason to make them easier to spot.
plugins {
id "com.diffplug.spotless" version "5.1.1" apply false // <-- Set "apply false" here
// This makes it configure which version to use on the classpath for the entire build, but without applying it.
// Notice that the Java plugin is not specified here anymore.
// This is because it is a core plugin so you can't set the version (and I am assuming you don't want it on in the root project).
}
ext {
deps = [
'junit': 'junit:junit:4.11'
]
}
allprojects {
repositories {
jcenter()
mavenCentral() // <-- You can remove this if you want as it is already present as a proxy in jcenter().
maven {
url "https://dl.bintray.com/deanhiller/maven"
}
maven {
url uri('/tmp/myRepo/')
}
}
}
subprojects {
// Here are the two plugins
apply plugin: "java"
apply plugin: "com.diffplug.spotless"
dependencies {
testImplementation deps['junit'] // <-- testCompile renamed to testImplementation as the former has been deprecated for a long time
}
}

Apply plugin configuration in init.gradle script

I want to install some packages locally for all my projects, e.g. dependency-analyse. But I need to actually configure the plugin - also in the init script.
initscript {
repositories {
jcenter()
}
dependencies {
classpath "ca.cutterslade.gradle:gradle-dependency-analyze:1.3.0"
}
}
allprojects {
apply plugin: ca.cutterslade.gradle.analyze.AnalyzeDependenciesPlugin
}
This init script works fine and applies the plugin, but unfortunately, the default setting is that the plugin fails the build. I would like to just log a warning.
For that I need to add configs:
analyzeClassesDependencies {
justWarn = true
}
analyzeTestClassesDependencies {
justWarn = true
}
but when I try to add it in the init.gradle file:
initscript {
repositories {
jcenter()
}
dependencies {
classpath "ca.cutterslade.gradle:gradle-dependency-analyze:1.3.0"
}
}
allprojects {
apply plugin: ca.cutterslade.gradle.analyze.AnalyzeDependenciesPlugin
analyzeClassesDependencies {
justWarn = true
}
analyzeTestClassesDependencies {
justWarn = true
}
}
I get an error:
FAILURE: Build failed with an exception.
* Where:
Initialization script '/Users/<my-user>/.gradle/init.gradle' line: 13
* What went wrong:
Could not find method analyzeClassesDependencies() for arguments [init_2y9p9if69e8553k9fsvzz4a28$_run_closure1$_closure2#3e17c37a] on root project 'my-project' of type org.gradle.api.Project.
Anybody an idea of how I can apply plugin configuration?
I tried the gradle forum but didn't get any answer, so I hope to get some more help here :)
AnalyzeDependenciesPlugin will add different tasks depending on which plugin is applied to your project. For example analyzeClassesDependencies and analyzeTestClassesDependencies will be declared only when java plugin is applied (see how this plugin is implemented here : https://github.com/wfhartford/gradle-dependency-analyze/blob/master/src/main/groovy/ca/cutterslade/gradle/analyze/AnalyzeDependenciesPlugin.groovy )
So you just need to apply java plugin before you apply the AnalyzeDependenciesPlugin in your allprojects configuration closure:
allprojects {
apply plugin: "java" // <= apply Java plugin here
apply plugin: ca.cutterslade.gradle.analyze.AnalyzeDependenciesPlugin
analyzeClassesDependencies {
justWarn = true
}
analyzeTestClassesDependencies {
justWarn = true
}
}

Control the gradle task execute order

I have a strange problem about gradle task recently.
Assume I have a simple gradle config as follows
apply plugin: "java"
apply plugin: "maven"
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.diffplug.gradle.spotless:spotless:2.0.0"
}
}
apply plugin: "com.diffplug.gradle.spotless"
spotless {
java {
eclipseFormatFile 'format.xml' // XML file dumped out by the Eclipse formatter
}
}
spotlessJavaCheck.dependsOn(processResources)
version = '1.0-SNAPSHOT'
I just want to set the depends on relationship for the spotless check. After I run a build, the error looks like this
> Could not find property 'spotlessJavaCheck' on root project 'gradle-helloworld'.
I have done something similar with other plugins, it works well, but not for this spotless plugin.
Br,
Tim
Spotless Gradle plugin does magic at configuration time.
You need to set the dependency after evaluation time, once the magic is done:
afterEvaluate {
tasks['spotlessJavaCheck'].dependsOn processResources
}

Publish (rootproject) pom without (rootproject) publishing artifact / packaging = pom

I'm migrating one of our projects from maven to gradle: it's a gradle multi-project & all subprojects are publishing artifacts to artifactory. So far so good.
The legacy (maven-based) build environment however also expects the root project to publish a pom file with the "packaging" node equal to "pom" (common maven behaviour, so it seems)
So now, I'm trying to have this generated by Gradle, but only find ways to customize an automatically generated pom for each artifact, I can't find a way to generate/upload a pom without publishing an actual artifact.
Workaround for now is to have the root project use the java plugin, generate/install an empty jar and manipulate the generated pom to conform to maven expectations (packaging=pom), but that's a hack.
Is there a way to have this root pom file generated with gradle ?
Example project:
settings.gradle
rootProject.name = 'MultiProject'
include 'child01', 'child02'
rootProject.children.each { it.name = rootProject.name + "-" + it.name }
build.gradle
subprojects {
apply plugin: 'java'
}
allprojects {
apply plugin: 'maven'
group = 'my_group'
version = '0.0.1-SNAPSHOT'
}
EDIT (current workaround), addition to build.gradle
// workaround to generate pom
apply plugin: 'java'
configurations {
pomCreation
}
task createPom {
ext.newPomFile = "${buildDir}/blabla.pom"
doLast {
pom {
project {
packaging 'pom'
}
}.writeTo(newPomFile)
}
}
install.dependsOn(createPom)
artifacts {
pomCreation file(createPom.newPomFile)
}
I would use the gradle maven-publish plugin for that. With that plugin you can define your specific pom and don't have to upload other artifacts. Here an example:
publishing {
publications {
maven(MavenPublication) {
pom.withXml{
def xml = asNode()
xml.children().last() + {
delegate.dependencies {
delegate.dependency {
delegate.groupId 'org.springframework'
delegate.artifactId 'spring-context'
delegate.version( '3.2.8.RELEASE' )
}
}
}
}
}
}

Resources