IDEA cannot compile java file in gradle project - intellij-idea

I am build a gradle project and run compileJava task everything is ok. But i right click one java file and 'Compile xxxx.java', i get some error.
Error:(3, 25) java: package com.jpf.ms.entity not exists
...
IDEA version: 14.1.4

Make sure your settings.build references that package, and refresh your dependencies as shown here before running gradle tasks.

Related

IntelliJ Gradle "Unable to find Method"

The Error:
Failed to sync Gradle project '-'
Error:Unable to find method 'org.gradle.internal.logging.progress.DefaultProgressLoggerFactory.(Lorg/gradle/internal/logging/progress/ProgressListener;Lorg/gradle/internal/time/TimeProvider;)V'.
You can see, a constructor inside a class, which is found in the logging jar (gradle_home/lib/grade-logging-version.jar) is missing.
So, I think the Class-Path does not contain this jar file.
Gradle Version: 3.5
Running using the Command Line works.
I have already tried this:
Gradle sync failed: Unable to find method
to-find-method, it does not work for me.
I have tried to use different versions.
I can't see any difference when enabling
"Use default gradle wrapper"
"Use gradle wrapper task configuration"
"Use local gradle distribution".
I had the exact same problem today.
The only way I was able to get it to work was to remove my IntelliJ IDEA IDE, get the latest from the website (with JDK) and put it in a new folder (i.e. no overwriting existing files).
After that, I deleted the .idea folder from the project and reimported it cleanly into the "new" IntelliJ.
I suspect upgrading IntelliJ from Version <= 2016.x caused the issue, since a new installation / clean extraction from archive caused it to work on the fly.

Intellij IDEA gradle debug plugin

Using IDEA 2017.1(ue); I like to know how I can debug a gradle plugin. I've setup a very simple gradle project (https://github.com/tkvw/gradle-plugin-debug). The project references one plugin org.gradle.hello-world.
My questions are:
1) Where can I set a breakpoint on the helloworld plugin? Normally I lookup my classes/sources using search anywhere in IDEA, but it doesn't find the class/sources. Idea seems to ignore the buildscript dependencies of the gradle build in it's sources lookup path?. (I tried the "old" buildscript block + apply as well)
2) How do I setup -Dorg.gradle.debug=true on the run configuration? If I specify it like this: the task simply runs and doesn't wait for a debugger to be attached.
From the command prompt the command
gradlew -Dorg.gradle.debug=true helloworld
does run and wait for a debugger to connect.

IDEA 2016.1, Gradle's processResources expanding not working automatically

I have an IDEA 2016.1 Enterprise and a Gradle 2.12 multi-module project. In one of the modules, in src/main/resources, I have a file which I would like Gradle to 'expand', here is my configuration:
processResources {
filesMatching('my.properties') {
expand(project.properties)
}
}
(I would like to expand just this single file, and just copy the rest.)
It all works fine when built on the command line, but not by default in IDEA - when I clean and build the project, the file lands in build/resources/main but the placeholders are not replaced. I have to manually invoke the Gradle processResources task using the Gradle pane in IDEA and double clicking on the task.
Is this something I should report to Jetbrains (i.e. a bug) or has anybody have it working and I should change something in my configuration?
When you build from command line, you are using gradle. However, when you build the project from intellij, by default intellij doesn't use gradle to build, but use its internal build system which doesn't understand your gradle's processResources.
One way to solve it is to check "Delegate IDE build/run actions to gradle" as shown below:
If you don't want to use gradle build in intellij, there's another workaround - add processResources as a gradle task to run after build in your "Run/Debug Configurations":
Try adding the dependency in your build.gradle file, eg.
assemble.dependsOn processResources
This should work if you have java plugin applied.

How to get IntelliJ to associate Gradle sources with build.gradle?

When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply, dependencies configure appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi() to the build's dependencies block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
I solved this problem as follows:
As mention in already posted answers, configure gradle
update gradle/wrapper/gradle-wrapper.properties file
change bin to all in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
id "java-library"
id "idea"
}
From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
Import project to IntelliJ idea.
Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
You can see
Restart IntelliJ idea IDE.
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Before
After
Autocomplete functionality as mentioned in this answer.
I had similar frustrations with Grails 3, which defines and runs a wrapper task when an app is created. Changing to the "all" zip in the wrapper properties file did not work because this kept getting changed back to the "bin" zip.
This was solved when it was understood that the "gradle-wrapper.properties" file simply stores the values from the "wrapper" task, and if this task is run after the properties are changed, they get changed right back.
This is easily fixed by setting some properties on the wrapper task:
wrapper.gradleVersion='3.2.1'
wrapper.distributionType=Wrapper.DistributionType.ALL
Now importing the project into IDEA gives you smart editing of your build.gradle.
when I choose build.gradle in IDEA and open it, IDE prompts
You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation.
I choose Ok, apply suggestion!
after project refreshing I am able to use code completion
before you import your project, configure it to use the customizable gradle wrapper as per the instructions here :-
https://docs.gradle.org/current/userguide/gradle_wrapper.html
add a task to your top level project like this:-
task wrapper(type: Wrapper) {
println "Wrapper gradleVersion = '2.12'"
gradleVersion = '2.12'
}
or whatever the latest version is.
make sure you can build the project from the gradle command line before you try importing into intelliJ, using the ./gradlew command, which will download and install a gradle distribution for you the first time you build.
set your java home, intelliJ home and gradle home variables in your machine and in intelliJ (mine look like this, yours may be different depending on your setup and your history of hacking around your machine...:-
(from .bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
)
When you do import, choose the customisable gradle wrapper. if all is well, when you open the top level build.gradle for your project, you will be asked to configure sources for the gradle dsl, which will also update your gradle wrapper properties file to this:-
#Thu Mar 31 14:04:00 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
.. the change being from ... bin.zip to all.zip. and that's it. This had been giving me lots of grief for a long time, but that's the way to do it. (on IntelliJ IDEA 2016.1 CE at least...)
most of this was in
Dimitry's answer too, but I couldn't get it to work using the default wrapper , it had to be the customisable wrapper.

IntelliJ downloads Gradle dependencies but cannot find them during compile

IntelliJ downloaded the dependencies listed in my build.gradle file and they show up in the External Library, but IntelliJ fails to find them during compile. It just throws a bunch of "package x does not exist" and "cannot resolve symbol". Under the Project Structure -> Modules -> Dependencies they all show up and are listed as compile.
If I try to build it with Gradle it's successful, so it's just IntelliJ. Any ideas?
If it's important, I'm using this on a Mac.
Found the answer. IntelliJ was treating Main and Test as modules with no dependency related to the project itself and all of the library dependencies were stored at the project level.
I killed off Main and Test as modules and it sorted itself out.