I'm new to gradle and I'm getting a build error that I don't really understand. My project is just an empty shell with the directory structure and no java source code. Here is my root build.gradle file
allprojects {
//Put instructions for all projects
task hello << { task -> println "I'm $task.project.name" }
}
subprojects {
//Put instructions for each sub project
apply plugin: "java"
repositories {
mavenCentral()
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
when I execute the gradle build command the build fails because it doesn't know the testCompile method with this message:
Could not find method testCompile() for arguments [{group=junit, name=junit, version=4.+}] on root project
I use Gradle 2.5.
I've understood that this method is a part of the java plugin which I've loaded. I don't see what went wrong, can you help?
In case anyone comes here based on the Could not find method testCompile() error, by now the more probable cause is that you need to replace the deprecated testCompile by testImplementation.
See What's the difference between implementation and compile in Gradle?
The java plugin is only applied to subprojects, so the testCompile configuration, added by the java plugin, can only be used in subprojects. The below works:
allprojects {
//Put instructions for all projects
task hello << { task -> println "I'm $task.project.name" }
}
subprojects {
//Put instructions for each sub project
apply plugin: "java"
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
}
It's saying that it can't find the method testCompile for the arguments check that you spelt it correctly making sure the name and group which you have as "junit" are all correct and also the version is correct another fix for this issue is adding the testCompile line in sub projects block.
Related
I'm creating a project which uses the Squash SQL Library for Kotlin. I've added the dependency to my build.gradle file. When running the update it just finishes without outputting any errors. But the library is not getting imported in my project and doesn't appear at all.
The dependencies shown in IntelliJ:
My build.gradle file:
//Kotlin Stuff, nothing changed here
repositories {
mavenCentral()
maven {
url "http://dl.bintray.com/kotlin/squash"
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.jetbrains.squash:squash:0.2.2'
}
//Kotlin Stuff
The dependency you've added is just the parent-pom which doesn't have any jar's in the repo. Here is the list of the squash projects (http://dl.bintray.com/kotlin/squash/org/jetbrains/squash/):
squash-core
squash-graph
squash-h2
squash-jdbc
squash-postgres
squash-sqlite
I guess you want to import the squash-core so change
compile 'org.jetbrains.squash:squash:0.2.2'
to
compile 'org.jetbrains.squash:squash-core:0.2.2'
how can I run my gradle project? Its a GUI application. I just want to run it so I can test it. Created a gradle project, copied my src file in and marked it as a source file (blue file). Now when I click run, the project runs all the gradle tasks but my application does not start. Here is my build.gradle file:
group '1'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = '1'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
jar {
baseName = 'JavaWinApp'
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
from { configurations.compile.collect { zipTree(it) } }
manifest {
attributes 'Implementation-Title': 'JavaWinApp'
attributes 'Implementation-Version': version
attributes 'Main-Class': 'Main'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.intellij:forms_rt:6.0.5'
}
Can you see anything blatantly obvious that I should change or can you give me any pointers?
Please let me know if you require any other information and I will happily supply it. I am an utter noob with this. My only exposure to gradle is through Android Studio, there all just runs fine. Looks like I'm missing something obvious, but cannot seem to find it.
Thank you in advance
The gradle application plugin should add a run task to your build, that will start your application. To execute this task you will need to create a run configuration in IntelliJ for this task. See this documentation on how to create a gradle run/debug configuration.
I'm trying to generalize a dependency to all subprojects in my gradle project. Reading this question: https://discuss.gradle.org/t/inheriting-common-dependencies-from-parent-project-in-child-projects/5493/2
I tried it out:
subprojects{
dependencies {
compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
}
}
I failed with the following error:
1. Error:(60, 0) Could not find method compile() for arguments
[{group=com.xetra11.toolbox, name=toolbox-commons, version=0.0.1}] on
object of type
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
<a
href="openFile:C:\Development\Testzone\toolbox-backend\build.gradle">Open
File</a>
Also using allprojects closure did not succeeded. Did I understood the answers of this above mentioned question wrong or what is the issue here?
That's because your subprojects do not have compile configuration yet. In other words, you have to first apply Java plugin and then declare the dependencies. Three ways how you can achieve that:
1) change the code to apply the plugin from root project
subprojects{
apply plugin: 'java'
dependencies {
compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
}
}
2) call the configuration after buildscript evaluation of each project is done
subprojects{
afterEvaluate {
dependencies {
compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
}
}
}
3) or, add the configuration as soon as the Java plugin is added in subprojects
subprojects{
plugins.whenPluginAdded { plugin ->
if (plugin instanceof JavaPlugin) {
dependencies {
compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
}
}
}
}
Context
I have started a personal project in java with Gradle as the build system and I want to use Dagger 2 as a DI. The main reason of doing that is to get used to that library and be able to use it easily in bigger projects.
What have I tried
I've managed to make the Google sample runs on IntelliJ IDEA
Problem
IntelliJ IDEA keeps telling me that it cannot resolve the generated class (in this case DaggerCoffeeApp_Coffee). It's a bit annoying not to know if the written code is correct (specially when you are learning to use Dagger 2).
All java classes are the same as the Google sample. Here is my build.gradle file:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
compile 'com.google.dagger:dagger-compiler:2.0.1'
}
Question
Is there any way to make IntelliJ IDEA recognize DaggerCoffeeApp_Coffee as a generated class (and so make it possible to go to its implementation by `ctrl + left click)?
Simplest way I found:
Add idea plugin and add Dagger2 dependency like below:
plugins {
id "net.ltgt.apt" version "0.10"
}
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
}
Turn on Annotation Processing for IntelliJ: Go to Settings and search for Annotation Processors, check Enable annotation processing like below image:
Finally I made it!
I had to add the apt and the idea plugin so right now my build.gradle file look like this:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.4"
}
}
apply plugin: "net.ltgt.apt"
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
apt 'com.google.dagger:dagger-compiler:2.0.1'
}
you must manually enable the annotation processing in IntelliJ.
From: Settings --> Build, Execution, Deployment --> Compiler --> Annotation Processors --> Enable annotation processing and Obtain processors from project classpath
then rebuild the project and you will find the generated classes in the project.
Please note that I have used this solution in a (java) android project.
I'm using version 2017.3.3 of IntelliJ IDEA, version 0.14 of the net.ltgt.apt plugin and version 2.14.1 of Dagger and as well as applying the idea plugin in the build.gradle file (as in Pelocho's answer) I found I also had to tell IntelliJ where it can find the sources generated by Dagger, as follows:
apply plugin: 'idea'
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
This is what I had to do in order to get Idea to work with Dagger2 and gradle.
Turn on annotation processing as shown in the answers above.
Add the following to the build.gradle file in order for Idea to see the generated classes as sources.
sourceDirs += file("$projectDir/out/production/classes/generated/")
Here's the full listing of my build.gradle
plugins {
id 'java'
id 'idea'
id "net.ltgt.apt" version "0.10"
}
idea {
module {
sourceDirs += file("$projectDir/out/production/classes/generated/")
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.dagger:dagger:2.16'
apt 'com.google.dagger:dagger-compiler:2.16'
}
sourceCompatibility = 1.8
Also, I had to add the following gradle task (to my build.gradle file) to clear out my out directory. When I moved some files around and Dagger2 regenerated the source files, the out directory wasn't being cleared out :(. I also included this task in my run configuration, so that it gets triggered before I rebuild my project.
task clearOutFolder(type: Delete) {
delete 'out'
}
Here's the solution that worked for me:
File -> Project Structure -> (select your project under list of modules) -> Open 'Dependencies' tab
Then, click on green '+' sign, select 'JARs or directory' and select 'build/classes/main' folder.
Another solution would be to link folder with build class files using 'dependencies' block inside build.gradle:
https://stackoverflow.com/a/22769015/5761849
Using IntelliJ IDEA 2019.1 and Gradle 5.4.1, this seems to be enough:
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'com.google.dagger:dagger:2.23.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.23.1'
}
I don't know the minimal versions for which this solution works, though.
I had a similar problem, I could not find out the cause for a long time.
Just launched and the result surprised me.
Intellij Idea 2018.3.6 -
build.gradle:
plugins {
id "java"
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
}
The following worked for me on IntelliJ 2021.3.3 (UE)
plugins {
id 'java'
id 'idea'
id("com.github.johnrengelman.shadow") version "7.1.2"
}
idea {
module {
sourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/main")
testSourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/test")
}
}
group 'com.codigomorsa'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'com.google.dagger:dagger-compiler:2.44'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.google.dagger:dagger:2.44'
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.44'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
test {
useJUnitPlatform()
}
I'm currently trying to include Project Lombok helper into my Gradle project, but while following their instructions for Gradle within my build.gradle, I'm getting the following error:
Error:(11, 0) Build script error, unsupported Gradle DSL method found: 'provided()'!
Possible causes could be:
you are using Gradle version where the method is absent
you didn't apply Gradle plugin which provides the method
or there is a mistake in a build script
My current build.gradle file:
apply plugin: 'java'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
provided "org.projectlombok:lombok:1.14.4"
testCompile group: 'junit', name: 'junit', version: '4.11'
}
As of release 2.12, provided scope is called compileOnly
Old answer:
Provided scope is available in 'war' plugin (http://www.gradle.org/docs/current/userguide/war_plugin.html , providedCompile ) If You don't want to use the 'war' plugin, there is also an opened JIRA issue regarding 'provided' scope http://issues.gradle.org/browse/GRADLE-784 , suggested workaround is to create Your own cofiguration:
configurations {
provided
}
and set it to be used with your compilation classpath:
sourceSets {
main {
compileClasspath += configurations.provided
}
}
Check your app level gradle file. If any line looks like this:
compile dependency.gson provided dependency.javaxAnnotation
Edit it like this:
compile dependency.gson
provided dependency.javaxAnnotation
It should work.