Multiple Gradle Files in the same project using apply from - gradle

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

Related

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.

adding errorprone to gradle build file

I am trying to add errorprone to my gradle build file as follows:
relevant parts of build.gradle
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
}
}
plugins {
id 'java'
id 'application'
id 'idea'
id 'com.github.johnrengelman.shadow' version '1.2.4'
id "net.ltgt.errorprone" version "0.0.9"
}
dependencies {
classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.9'
}
configurations.errorprone {
resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.19'
}
error from classpath line when I run gradle clean
Could not find method classpath() for arguments [net.ltgt.gradle:gradle-errorprone-plugin:0.0.9] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Any thoughts on what is causing this issue and what might help resolve this? Thanks
I got errorprone to work with gradle by making the following changes:
replaced this from dependencies:
classpath net.ltgt.gradle:gradle-errorprone-plugin:0.0.9'
with
errorprone "com.google.errorprone:error_prone_core:latest.release"
and removed the configurations line:
configurations.errorprone {
resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.19'
}
This worked.

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
}

Gradle war ignores transitive dependencies when using 'configurations.runtime.asPath' in custom task

I'm facing behavior that I can't explain, using gradle 1.10 I have:
settings.gradle:
include('lib1', 'lib2', 'web')
build.gradle:
subprojects {
apply plugin: 'java'
}
project(':web') {
apply plugin: 'war'
dependencies {
compile project(':lib1')
}
task myTask(type: JavaExec, dependsOn: 'compileJava') {
main = "some.thirdparty.Class"
args "--searchPath", configurations.runtime.asPath
}
}
project(':lib1') {
dependencies {
compile project(':lib2')
}
}
project(':lib2') {
}
When I run gradle clean war I only have lib1.jar in war/build/libs/web.war/WEB-INF/lib.
To make WEB-INF/lib contain both lib1.jar and lib2.jar I have to:
move project('web') block to the end of the file
update configurations.runtime.asPath to configurations.runtime (but I need to provide class path as a path, so it is not a solution)
I read the build lifecycle description, tried to compare --debug outputs but that didn't help.
Why is this happening? And what would be a good solution to provide the module runtime class path as a path in JavaExec task please?
asPath resolves the configuration, but resolution will only work correctly if it happens at execution time rather than configuration time (in particular in the presence of project dependencies). Try to wrap the args line with doFirst { ... }.

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