Idea "Groovyc: unable to resolve class" - Works on command line - java

I have a gradle project in Intellij Idea that contains two modules:
foo-core
|--src
| |--main
| | |--java
|--build.gradle
foo-test
|--src
| |--test
| | |--groovy
|--build.gradle
build.gradle
settings.gradle
foo-test contains a spock test that should test a class from foo-core. When I try to run the tests in foo-test from IntelliJ Idea, I get the following error:
Groovyc: unable to resolve class ...
But when I run them from the command line with gradle :foo-test:test, everything works just fine.
Here is my toplevel settings.gradle:
include ':foo-core', ':foo-test'
And here is the build.gradle from foo-test:
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'groovy'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile project(':foo-core')
compile "org.codehaus.groovy:groovy-all:2.2.1"
compile "com.google.inject:guice:3.0"
testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
testCompile group: 'junit', name: 'junit', version: '4.11'
}
The project dependencies of foo-test are correct in Idea. When I "Refresh all Gradle Projects", any changed dependencies are imported correctly.
I have already tried cleaning all caches in Idea.
Why can I run the tests in gradle on the command line, but cannot compile them in IntelliJ Idea?
Update: Answers to questions from comments
Idea version 13
I created an Android Gradle project in Idea and added more modules by creating the directories+gradle files and adding them in the toplevel "settings.gradle". Then I did a "Refresh all Gradle Projects" in Idea. I do not use the gradle idea plugin.
The source and test directories are recognized by Idea. All project settings seem to be correctly imported in Idea (from gradle). I can use the java classes from foo-core in another java project (foo-ui), but can not use them in the groovy files.
Just compiling in IntelliJ does not work. A full rebuild does not work either. Even "Refresh all Gradle Projects" produces the Groovyc errors.
Also, the tests already worked when I had only two modules (foo-ui and foo-test), but stopped working after I moved some classes to a new module (foo-core).
Update 2: I have now updated to Idea 13.0.1. I have also tried to close the project, delete all .iml files and "build" directories and import it again. Both did not work.

This was, in fact, a bug of IntelliJ Idea: http://youtrack.jetbrains.com/issue/IDEA-117676
Everything works fine with IntelliJ Idea 13.0.2 and Gradle 1.9.

Related

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver despite the fact it is in dependencies of gradle.build file

I have read top 10-15 questions with answers by the following query https://stackoverflow.com/search?q=com.microsoft.sqlserver.jdbc.SQLServerDriver%22
However, I still don't understand why it doesn't work.
Usual steps to solve this issue:
Make sure that the jar is add to as a dependecy in your build/dependency management tool (Ant/Maven (pom.xml)/Gradle(gralde.build))
Yes, it is in my case:
dependencies {
compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '7.4.0.jre8'
testCompile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '7.4.0.jre8'
}
in build.gradle file
and gradle build command works without exceptions.
An alternative solution is to download jar file manually from https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
and then add it to a classpath. This solution is undesirable i don't want to do something manually that has to be done by a build tool.
So the question is why I am getting the error "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" and I cannot see this jar in the dependencies tab
despite the fact that the jar is mentioned in my build.gradle file in the dependency section as a compile-time dependency and as a Test time dependency :
FYI:
That is how it is called in my code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
It looks like IntelliJ hasn't imported the addition of these dependency. This usually means that the auto-import is disabled.
You can reimport your gradle file by clicking the reimport button in the gradle tab of IDEA. You can enable auto-import by clicking the Gradle Settings button in the gradle tab, and enabling "Automatically import this project on changes in build script files".
On a separate not, you don't need to declare testCompile if you also declare a dependency as compile.

Configuration with name 'default' not found gradle sync failed [duplicate]

I have an Android Studio app. It has a library dependency (Android-Bootstrap), when I try to sync gradle, it gives me an error:
Configuration with name 'default' not found.
My structure is:
-FTPBackup
-fotobackup
-build.gradle
-Libraries
-Android-Bootstrap
-Settings.gradle
-build.gradle
-Settings.gradle
-Build.gradle
The FTPBackup settings.gradle and build.gradle:
include ':fotobackup'
include ':libraries:Android-Bootstrap',':Android-Bootstrap'
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
And the build.gradle inside fotobackup is:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:+'
compile project (':libraries:Android-Bootstrap')
}
The library is downloaded from https://github.com/Bearded-Hen/Android-Bootstrap and it has build.gradle, settings etc.
whats wrong?
For one, it doesn't do good to have more than one settings.gradle file -- it only looks at the top-level one.
When you get this "Configuration with name 'default' not found" error, it's really confusing, but what it means is that Gradle is looking for a module (or a build.gradle) file someplace, and it's not finding it. In your case, you have this in your settings.gradle file:
include ':libraries:Android-Bootstrap',':Android-Bootstrap'
which is making Gradle look for a library at FTPBackup/libraries/Android-Bootstrap. If you're on a case-sensitive filesystem (and you haven't mistyped Libraries in your question when you meant libraries), it may not find FTPBackup/Libraries/Android-Bootstrap because of the case difference. It's also looking for another library at FTPBackup/Android-Bootstrap, and it's definitely not going to find one because that directory isn't there.
This should work:
include ':Libraries:Android-Bootstrap'
You need the same case-sensitive spec in your dependencies block:
compile project (':Libraries:Android-Bootstrap')
compile fileTree(dir: 'libraries', include: ['Android-Bootstrap'])
Use above line in your app's gradle file instead of
compile project (':libraries:Android-Bootstrap')
In my setting.gradle, I included a module that does not exist. Once I removed it, it started working. This could be another way to fix this issue
If you're getting this error with react native, it may be due to a link to an NPM package that you removed (as it was in my case). After removing references to it in the settings.gradle and build.gradle files, I cleaned and rebuilt and it's as good as new :)
Just a note on this question:
I had this exact error in my React Native app when trying to build to android. All you should have to do is $ npm i.
Case matters
I manually added a submodule :k3b-geohelper
to the
settings.gradle file
include ':app', ':k3b-geohelper'
and everthing works fine on my mswindows build system
When i pushed the update to github the fdroid build system failed with
Cannot evaluate module k3b-geohelper : Configuration with name 'default' not found
The final solution was that the submodule folder was named k3b-geoHelper not k3b-geohelper.
Under MSWindows case doesn-t matter but on linux system it does
I had this issue with Jenkins. The cause: I had renamed a module module to Module. I found out that git had gotten confused somehow and kept both module and Module directories, with the contents spread between both folders. The build.gradle was kept in module but the module's name was Module so it was unable to find the default configuration.
I fixed it by backing up the contents of Module, manually deleting module folder from the repo and restoring + pushing the lost files.
The message is a known Gradle bug. The reason of your error is that some of your gradle.build files has no apply plugin: 'java' in it. And due to the bug Gradle doesn't say you, where is the problem.
But you can easily overcome it. Simply put apply plugin: 'java' in every your 'gradle.build'
I also faced the same problem and the problem was that the libraries were missing in some of the following files.
settings.gradle, app/build.gradle, package.json, MainApplication.java
Suppose the library is react-native-vector-icons then it should be mentioned in following files;
In app/build.gradle file under dependencies section add:
compile project(':react-native-vector-icons')
In settings.gradle file under android folder, add the following:
include ':react-native-vector-icons' project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
In MainApplication.java, add the following:
Import the dependency: import com.oblador.vectoricons.VectorIconsPackage;
and then add: new VectorIconsPackage() in getPackages() method.
I am facing same problem, I was fixed it by generating gradle project and then adding lib project to android studio
First, See build.gradle file is present in project root directory
if not then, Create gradle project,
export your required lib project from eclipse then (File->Export->Android->generate Gradle build file
Click on Next->Next->Select your lib project from project listing->Next->Next->Finish
See build.gradle file present in your project root directory
Move this project to Android Studio
Your module name must be camelCase eg. pdfLib. I had same issue because I my module name was 'PdfLib' and after renaming it to 'pdfLib'. It worked.
The issue was not in my device but in jenkins server. So, check and see if you have such modulenames
Step.1
$ git submodule update
Step.2
To be commented out the dependences of classpass
You are better off running the command in the console to get a better idea on what is wrong with the settings. In my case, when I ran gradlew check it actually tells me which referenced project was missing.
* What went wrong:
Could not determine the dependencies of task ':test'.
Could not resolve all task dependencies for configuration ':testRuntimeClasspath'.
Could not resolve project :lib-blah.
Required by:
project :
> Unable to find a matching configuration of project :lib-blah: None of the consumable configurations have attributes.
The annoying thing was that, it would not show any meaningful error message during the import failure. And if I commented out all the project references, sure it let me import it, but then once I uncomment it out, it would only print that ambiguous message and not tell you what is wrong.

Cannot resolve symbol with AutoValue and IntelliJ

I have been trying to find the correct settings for IntelliJ's annotation processing in order for it to co-exist with Gradle's build process.
Whenever I build from IntelliJ I cannot get it to recognise the generated sources from the gradle-apt-plugin.
My requirements for my project are:
Building between IntelliJ and Gradle should be seamless and not interfere with the process of each other
I need to use IntelliJ's Create separate module per source set option
I need to use IntelliJ's folder based structure
IntelliJ needs to be able to recognise and autocomplete AutoValue classes
Here are the steps for a MCVE in order to reproduce the issue with IntelliJ 2017.2.4 and Gradle 3.5:
Create a new Gradle project from IntelliJ
Check the Create separate module per source set option
Open build.gradle file:
Add the following plugins block:
plugins {
id 'java'
id 'net.ltgt.apt' version '0.12'
}
Add the following dependencies block
dependencies {
compileOnly 'com.google.auto.value:auto-value:1.5'
apt 'com.google.auto.value:auto-value:1.5'
}
Go to Settings → Build, Execution, Deployment → Annotation Processors
Check the Enable Annotation Processing
Create a class:
#AutoValue
public abstract class GeneratedSourcesTest {
static GeneratedSourcesTest create(String field) {
return new AutoValue_GeneratedSourcesTest(field);
}
public abstract String field();
}
On IntelliJ run Build → Build Project
Open the GeneratedSourcesTest class, on the static factory method, everything compiles fine but I get the error:
cannot resolve symbol ‘AutoValue_GeneratedSourcesTest’
How can I make the AutoValue_GeneratedSourcesTest class accessible from IntelliJ?
After importing your Gradle project under IDEA do the following steps:
Set annotation processing configuration as follows:
Run menu: Build - Build Project
Right click on each new generated folder and select: Mark Directory as - Generated Sources Root so it was marked as follows:
Add /generated to project's .gitignore file
That's a minimal viable configuration which will provide full IDE support for generated classes.
The drawback is, whenever Gradle project gets re-imported the generated folders will need be marked as Generated Sources Root again.
Perhaps this can be improved with adding these paths as source sets under build.gradle.
Sometimes it happens that IDEA modules lose their compiler output path settings in result of the above. It's sufficient to just set it back to their default folders.
The answers are (should be) in the README for the gradle-apt-plugin: https://github.com/tbroyer/gradle-apt-plugin
Namely, also apply the net.ltgt.apt-idea plugin.
Btw, I recommend delegating build/run actions to Gradle in IntelliJ. Sure it's a bit slower, but requires zero setup in the IDE and works reliably. That said, it should also work OK if you don't.
Just have your build.gradle with these and it works fine, no need of touching intellij, source set etc..
plugins {
id 'java'
id "net.ltgt.apt" version "0.20"
}
apply plugin: 'idea'
apply plugin: 'net.ltgt.apt-idea'
group 'abc'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "com.google.auto.value:auto-value-annotations:1.6.2"
annotationProcessor "com.google.auto.value:auto-value:1.6.2"
}
I didn't have to do anything to intellij using maven by adding the optional true tag.
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.9</version>
<optional>true</optional>
</dependency>

Gradle project dependency error in Jenkins

I have a project structure similar to
//Directory structure
Root Folder/
projectA/
build.gradle
projectB/
build.gradle
properties.gradle
Now, project B is dependent on project A
The settings.gradle and build.gradle for Project B is as follows
settings.gradle
include ':ProjectA'
project(':ProjectA').projectDir = new File(settingsDir, '../ProjectA')
build.gradle
dependencies{
compile project(':ProjectA')
}
When I try to build project B on my local machine (Gradle version 3.2) it builds successfully and everything looks good.
When i try to build the same project in jenkins (same gradle version as my local), i am getting the error
Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'default' not found.
It looks like in jenkins, it is not able figure out the relative path.
How do I solve this?
Is there a way in jenkins I can ignore the dependency from gradle and use the pre build to compile ProjectA and put that in the classpath? If so how can we do it?
I was able to solve this by adding a jenkins property which I check in the build file
if (project.hasProperty('jenkins')) {
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
} else {
dependencies {
compile project(':ProjectA')
}
}
So when we build it locally, since we are not passing the property it is able to compile ProjectA. In Jenkins, the build job, in the gradle command I am passing the following -Pjenkins clean build. This goes to the if condition and pulls the jar from projectA and puts that in the lib directory.
This has solved the problem for me, let me know if there are better solutions for it

Intellij Idea 13 UI Designer and automatic Gradle building

I've used the Intellij UI Designer to create forms for a project. Everything works fine when I'm building with idea as it handles compiling the forms for me, but as we recently switched to using Gradle for building it hasn't been possible to produce an executable jar file yet.
My google-fu has led me to several posts that explains that an ant script is needed to compile (eg link, link2, link3 ,and the one i ended on following: link4)
My project is a multi-module setup.
root build.gradle
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
}
}
supproject build.gradle
apply plugin:'application'
mainClassName = "dk.OfferFileEditor.OfferFileEditorProgram"
configurations {
antTask
}
dependencies {
compile 'org.json:json:20140107'
compile project(":Shared:HasOffers Api")
//dependencies for java2c
antTask files('../../lib/javac2-13.1.1.jar', '../../lib/asm4-all-13.1.1-idea.jar', '../../lib/forms_rt-13.1.1.jar')
antTask group: 'org.jdom', name: 'jdom', version: '1.1'
}
task compileJava(overwrite: true, dependsOn: configurations.compile.getTaskDependencyFromProjectDependency(true, 'jar')) {
doLast {
println 'using java2c to compile'
project.sourceSets.main.output.classesDir.mkdirs()
ant.taskdef name: 'javac2', classname: 'com.intellij.ant.Javac2', classpath: configurations.antTask.asPath
ant.javac2 srcdir: project.sourceSets.main.java.srcDirs.join(':'),
classpath: project.sourceSets.main.compileClasspath.asPath,
destdir: project.sourceSets.main.output.classesDir,
source: sourceCompatibility,
target: targetCompatibility,
includeAntRuntime: false
}
}
But even though the compilation is successfull, a Nullpointer exception is thrown the first time I try to access one of the fields the UI Designer created. So something is not being compiled correctly.
I'm probably missing some setting, but after unsuccesfully pouring several hours into forums and google I still haven't found any solution.
So I made this a lot more complicated than needs be.
To make it work you need to change two things in your project.
A setting in IDEA 13.1.5
Settings -> GUI Designer -> Generate GUI into: Java source code
This makes IntelliJ IDEA add 3 methods into the bottom of your forms:
$$$setupUI$$$()
$$$setupUI$$$()
$$$getRootComponent$$$()
If they are missing try recompiling your project after you change the setting.
Add the missing classes
Intellij has a jar called forms_rt.jar, and I found mine in {IntelliJ IDEA Root}\lib. And renamed it to "forms_rt-13.1.1.jar"
This needs to be included during compile time to your project. If you are using Gradle as I did you could copy it to {project root}/lib and add a flatfile repository like so:
repositories {
mavenCentral()
flatDir dirs: "${rootDir}/lib"
}
After that you need to include it in your project gradle file:
dependencies {
compile name: 'forms_rt', version: '13.1.1'
}
After that it should be possible to build it both in IntelliJ IDEA and Gradle.
IntelliJ IDEA 2019.1
I found this issue still exists. It's at least somehow documented now:
If your build actions are delegated to Gradle, GUI Designer will not generate Java source code.
So by disabling the according setting
Build, Execution, Deployment | Build Tools | Gradle | Runner | Delegate IDE build/run actions to gradle
I was able to build and run the project successfully. Note that I didn't need any other settings or additional libraries from the answers above. I let Generate GUI into be set to Binary class files.
The forms_rt library is in mavenCentral.
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22forms_rt%22
Once you have configured IntelliJ to update the SourceCode it is sufficient to just add the library to the dependencies in your build.gradle.
dependencies {
compile 'com.intellij:forms_rt:7.0.3'
}
Idea 2019.2
It seems like IntelliJ changed the settings UI when updating from 2019.1 to 2019.2, as the menu entry mentioned by Tom isn't there anymore.
I got it fixed by setting Build and run using: to IntelliJ Idea. I also changed Run tests using: to IntelliJ Idea to avoid problems while testing.
Both settings are located under File | Settings | Build, Execution, Deployment | Build Tools | Gradle.
I figured out an updated version of the gradle build workaround for a new project - https://github.com/edward3h/systray-mpd/blob/master/build.gradle
Probably won't use the form designer again though.
These are the relevant parts:
repositories {
mavenCentral()
maven { url "https://www.jetbrains.com/intellij-repository/releases" }
maven { url "https://jetbrains.bintray.com/intellij-third-party-dependencies" }
}
configurations {
antTask
}
dependencies {
implementation 'com.jetbrains.intellij.java:java-gui-forms-rt:203.7148.30'
antTask 'com.jetbrains.intellij.java:java-compiler-ant-tasks:203.7148.30'
}
task compileJava(type: JavaCompile, overwrite: true, dependsOn: configurations.compile.getTaskDependencyFromProjectDependency(true, 'jar')) {
doLast {
project.sourceSets.main.output.classesDirs.each { project.mkdir(it) }
ant.taskdef name: 'javac2', classname: 'com.intellij.ant.Javac2', classpath: configurations.antTask.asPath
ant.javac2 srcdir: project.sourceSets.main.java.srcDirs.join(':'),
classpath: project.sourceSets.main.compileClasspath.asPath,
destdir: project.sourceSets.main.output.classesDirs[0],
source: sourceCompatibility,
target: targetCompatibility,
includeAntRuntime: false
}
}
The dependency versions for jetbrains libraries are found via https://plugins.jetbrains.com/docs/intellij/intellij-artifacts.html?from=jetbrains.org#using-intellij-platform-module-artifacts and https://www.jetbrains.com/intellij-repository/releases/

Categories

Resources