IntelliJ with gradle gives inconsistency in compilation - java

I'm writing Minecraft Plugin using IntelliJ IDEA Ultimate with gradle. I have added dependency org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT as compileOnly. During development, I noticed that gradle compiles my code in different way than IntelliJ does. For example, IntelliJ was unable to accept addPassenger on Boat, but gradle compiled it. In the opposite way, if I changed it into setPassenger, IntelliJ didn't mark it as error, but gradle failed to compile. I tried to invalidate caches, reimport, clean, even remove %userprofile%\.gradle directory, nothing helped. As a POC I changed compileOnly to compile and it worked well, IntelliJ and gradle compilation results were consistent. What's the reason?

Ok, I found the solution (and forgot about this question).
I had been using multiple dependencies, and one load another with older version that I loaded implicitly in my build.gradle. However, they weren't exactly the same dependencies, but parallel ones. So gradle could not choose higher version of one dependency. Solution was to exclude this one explicitly loaded dependency and everything worked well.
Before:
dependencies {
compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.0.1'
compileOnly group: 'org.spigotmc', name:'spigot-api', version: '1.15.1-R0.1-SNAPSHOT'
}
After:
dependencies {
compileOnly('com.sk89q.worldedit:worldedit-bukkit:7.0.1') {
exclude `org.bukkit:bukkit:1.15.1-R0.1-SNAPSHOT`
}
compileOnly group: 'org.spigotmc', name:'spigot-api', version: '1.15.1-R0.1-SNAPSHOT'
}

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.

Android Gradle ResolutionStrategy force stil downloads previous version of lib

I have forced in my gradle to download this version of jsr305 as follows :
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.1'
I see that when I try to compile the gradle is resolving the version :
Inspite of that I see that during gradle sync the older versions (2.0.1 & 1.3.9) are still getting downloaded :
I am getting compile errors as follows :
com.android.build.api.transform.TransformException:
Error while generating the main dex list.
com.android.tools.r8.errors.CompilationError: Program type already present: javax.annotation.CheckForNull
Program type already present: javax.annotation.CheckForNull
I did a module level search and found that the CheckForNull.java is present at multiple places in
jsr305/2.0.1
jsr305/3.0.1
jsr305/1.3.9
I have tried deleting ./gradle folder and resync the project. I see that gradle still downloads the previous jsr305 version.
These are my dependencies in gradle :
implementation "com.facebook.react:react-native:${versions.reactNative}"
implementation ("com.google.code.findbugs:annotations:3.0.1") {
exclude group: 'net.jcip', module: 'jcip-annotations'
}
My questions :
Why is Gradle still downloading the older version of jsr305 ?
And in spite of the jsr305 version getting resolved why is multidex throwing that error ?
Created a test project that shows the behavior where the old lib versions are downloaded even after the forced resolution:
https://github.com/vineyugave/scratchpad
Also you can see the gradle scan here :
https://scans.gradle.com/s/tzrobr2zuar3c/dependencies?dependencies=jsr&expandAll
module :firstlib references implementation "com.google.code.findbugs:jsr305:2.0.0",
which should possibly be implementation "com.google.code.findbugs:jsr305:3.0.2" ...but the other one build.gradle does not really match the question, because it lacks react-native.
the dependencies of module :app should look alike (only the changes):
dependencies {
implementation "com.android.support:appcompat-v7:28.0.0"
implementation "com.android.support:recyclerview-v7:28.0.0"
implementation "com.android.support:support-v4:28.0.0"
implementation ("com.facebook.react:react-native:0.20.1") {
exclude group: "com.android.support", module: "recyclerview-v7"
exclude group: "com.android.support", module: "support-v4"
}
//noinspection GradleDependency
implementation "com.google.guava:guava:24.1-android"
}
configurations.all {
resolutionStrategy.force "com.google.code.findbugs:jsr305:3.0.2"
resolutionStrategy.force "com.google.guava:guava:24.1-android"
}
it's downloading elder versions, because they would need to be explicitly excluded from the dependencies, which demand them (as demonstrated above). one can list them all with ./gradlew app:dependencies and then exclude them accordingly.
task :app:transformClassesWithMultidexlistForDebug failed, because of support-library version conflicts caused by react-native (already fixed in the above example).
when moving those jniLibs from armeabi into armeabi-v7a, it wouldn't complain about a missing stripping tool anymore. however, then they wouldn't be loaded on arm64-v8a anymore.

Same interface in 2 different dependency issue

I have conflict problem with dependencies.
My project has two dependencies like this:
dependencies {
provided group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile files('path/to/ABC.jar')
}
ABC.jar has its own dependency to javax.servlet:servlet-api:
dependencies {
provided group: 'javax.servlet', name: 'servlet-api', version: '2.5'
}
Here is the problem I am dealing with; I need to use ServletContext interface which is provided by both servlet-api libraries and the compiler uses wrong one.
Gradle auto-resolves version conflicts as described here.
But in my case it doesn't help, because it only works when a dependency has two different versions. In this case; although it's a newer version issue, the name has changed from javax.servlet:servlet-api to javax.servlet:javax.servlet-api. So gradle doesn't auto-resolve this conflict, because it doesn't seem to be a version issue.
The thing I tried was using excluding transitive dependency as described here.
compile files('path/to/ABC.jar') {
exclude group: 'javax.servlet'
}
But it didn't work, it seems exclude doesn't work on local 'jar' files.
Now, I don't know what else to do.
How can I exclude a dependency of a dependency which is added as a local file?
(If the first question doesn't have any answer yet) How can I say to the compiler to use the correct ServletContext interface?
compile files('path/to/ABC.jar') is a file dependency, a file dependency does not have any dependency information, so it does not introduce transitive dependencies. If this ABC.jar is a "fat" jar that has the dependency-classes included in the JAR, it is not suited for usage in something like Gradle, Maven or Ant/Ivy that is supposed to handle the dependencies. You would have to use a proper "thin" version of the dependency with the dependencies properly declared, or you need to "repackage" that JAR in your build script to exclude the dependency classes you don't want to pull in. No dependency management can do this for you.
You can execute gradlew dependencyInsight --configuration runtime --dependency javax.servlet:servlet-api or gradlew dependencies --configuration runtime to find out where the dependency really comes from.
Actually your example should not even compile if I see it correctly, because it should most probably be
compile files('path/to/ABC.jar'), {
exclude group: 'javax.servlet'
}
or
compile(files('path/to/ABC.jar')) {
exclude group: 'javax.servlet'
}
But as I said, with a local file dependency there are no transitive dependencies, so an exclude does not make sense at all anyway.
To make the Gradle version conflict magic work, you can simply tell Gradle that those libraries are actually the same library just with different coordinates by using a module replacement like
dependencies {
modules {
module('javax.servlet:servlet-api') {
replacedBy 'javax.servlet:javax.servlet-api'
}
}
}
Then Gradle sees them as the same library and can do its version conflict resolution magic. Whether the library that needs the old version still works with the new version is a different topic that you have to check and / or try yourself. This like always depends on whether the new version is backwards compatible to the old version.
I have no way to test this now, but I believe your syntax is wrong. I have some examples here that look different, in your case it would be:
compile(files('path/to/ABC.jar')) {
exclude group: 'javax.servlet'
}
As I say, I cannot test it now, check if it helps and give a comment.

Intellij not including gradle dependency on classpath when running an app

IntelliJ is not including by logback-classic dependency on classpath when running the application from IDE (it is kotlin application, but it shouldn't matter). My gradle build contains a line:
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
Dependency is correctly listed under "External Libraries" in IntelliJ.
When I try to manually reference a class that the dependency provides in the source code, compiler doesn't complain and it compiles the code successfully.
However, when I run the application I get ClassNotFoundException.
//This import is provided by logback-classic library
import org.slf4j.impl.StaticLoggerBinder
...
// This compiles successfully, but will trigger ClassNotFoundException when run
val singleton = StaticLoggerBinder.getSingleton()
I included the code to list out the runtime classpath:
val cl = ClassLoader.getSystemClassLoader()
val urls = (cl as URLClassLoader).urLs
for (url in urls) {
System.out.println(url.file)
}
And the output, when run from IDE, doesn't include logback-classic. Sample output:
...
/Users/knesek/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.8.8/bf88c7b27e95cbadce4e7c316a56c3efffda8026/jackson-databind-2.8.8.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.module/jackson-module-jaxb-annotations/2.8.8/e2e95efc46d45be4b429b704efbb1d4b89721d3a/jackson-module-jaxb-annotations-2.8.8.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/com.sun.mail/javax.mail/1.5.6/ab5daef2f881c42c8e280cbe918ec4d7fdfd7efe/javax.mail-1.5.6.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-core/1.1.11/88b8df40340eed549fb07e2613879bf6b006704d/logback-core-1.1.11.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.1.1/98e484e67f913e934559f7f55f0c94be5593f03c/kotlin-stdlib-1.1.1.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.3.9.RELEASE/daa5abf3779c8cad1a2910e1ea08e4272489d8ae/spring-beans-4.3.9.RELEASE.jar
...
Fascinatingly, logback-core is in there while logback-classic is not (and logback-core is a transitive dependency of logback-classic).
I tried invalidating InteliJ cahces, rebuilding and restarting InteliJ. Compling from gradle and running the jar works fine. Any suggestions?
It seems this a bug in InteliJ for which I'll file a bug report. Including this answer with a workaround here for anyone who might encounter similar issue.
It seems that when there is a certain combinations of dependencies present on the classpath, then when you use gradle dependency like this
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
it will not get included when run from the IDE. But if you include it like this:
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.+'
then it works. First example did work for a while, until I included more dependencies.

Could not find method compile() for arguments Gradle

Looked around for this solution for much too long now, and I'm not sure if I missed it or just misstyped something, but my Gradle script will not compile. I am migrating to Gradle, and am very new with it. I am very used to using Maven for dependency management, but Gradle seems best me for now. From running this snippet of code:
dependencies {
compile group: 'org.bukkit', name: 'bukkit', version: '1.7.9-R0.1-SNAPSHOT'
compile('io.ibj:MattLib:1.1-SNAPSHOT') {
exclude group: 'de.bananaco'
exclude 'net.milkbowl:vault:1.2.27'
}
compile group: 'net.citizensnpcs', name: 'citizens', version: '2.0.12'
compile group: 'com.sk89q', name: 'worldedit', version: '5.6.1'
compile group: 'com.sk89q', name: 'worldguard', version: '5.9'
compile group: 'net.milkbowl', name: 'vault', version: '1.2.12'
compile fileTree(dir: 'libs', includes: ['*.jar'])
}
NOTE: I do have the java, maven, nexus, shadow, and rebel plugins applied.
When I run my Gradle task, I encounter this error:
Could not find method compile() for arguments [[io.ibj:MattLib:1.1-SNAPSHOT], build_1b5iofu9r9krp7o8mme0dqo9l$_run_closure2_closure8#66fb45e5] on root project 'project'
If I remove the MattLib dependency from my project and reinsert it as
compile 'io.ibj:MattLib:1.1-SNAPSHOT'
The script completes, but I have dependency issues. I read up here:
dependencies {
compile("org.gradle.test.excludes:api:1.0") {
exclude module: 'shared'
}
}
(From Chapter 50 From the Gradle Manual)
that what I have SHOULD work, but I am confused why it doesn't.
gradle --version output:
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
Ivy: 2.2.0
JVM: 1.8.0_05 (Oracle Corporation 25.5-b02)
OS: Windows 7 6.1 amd64
Note that the compile, runtime, testCompile, and testRuntime configurations introduced by the Java plugin have been deprecated since Gradle 4.10 (Aug 27, 2018), and were finally removed in Gradle 7.0 (Apr 9, 2021).
The aforementioned configurations should be replaced by implementation, runtimeOnly, testImplementation, and testRuntimeOnly, respectively.
Make sure that you are editing the correct build.gradle file. I received this error when editing android/build.gradle rather than android/app/build.gradle.
compile is a configuration that is usually introduced by a plugin (most likely the java plugin) Have a look at the gradle userguide for details about configurations. For now adding the java plugin on top of your build script should do the trick:
apply plugin:'java'
It should be exclude module: 'net.milkbowl:vault:1.2.27'(add module:) as explained in documentation for DependencyHandler linked from here because ModuleDependency.exclude(java.util.Map) method is used.
In my case, all the compile statements has somehow arranged in a single line. separating them in individual lines has fixed the issue.
In my case the problem was mismatch in the gradle version. I have installed gradle on mac using
brew install gradle
and got the latest gradle which was 7.0
However when I cloned by project repo and executed the gradle taks it failed with below error
* What went wrong:
A problem occurred evaluating root project 'digital-engineering-course'.
> Could not find method compile() for arguments [org.springframework.boot:spring-boot-starter-web, build_bzpgd6h32w4m8umtmgs76ewog$_run_closure3$_closure8#b55ca3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
build.gradle file looked pretty normal to me as it has regular dependencies
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
It took me a while to understand the problem is mismatch of version. Gradle is not able to find the method compile() because I was using gradle 7.0 in my bash.
And the project was supposed to be ran with gradle 4.8 (Actually gradle wrapper was to be used, but that was breaking for another interesting issue Could not find or load main class org.gradle.wrapper.GradleWrapperMain
(If interested please follow this for details)
The reason for failure is compile is that the compile, runtime, testCompile, and testRuntime configurations introduced by the Java plugin have been deprecated since Gradle 4.10, and were finally removed in Gradle 7.0.
So, to solve the problem I had to install the lower version of gradle. If you want to manage multiple version of gradle use sdkman (earlier known as gvm)
Installation on macOs / linux is as simple as executing below
curl -s "https://get.sdkman.io" | bash
Once done use
sdk list gradle
It will list out all the available versions of the gradle. As per your need install and use. for e.g
sdk install gradle 4.8 (this will choose the 4.8 by default in current shell)
sdk use gradle 4.8 (if already installed, this is suffice to switch between gradle version)
And now the build.gradle was able to compile and execute the task.
Add the dependency to your project-level build.gradle:
classpath 'com.google.gms:google-services:3.0.0'
Add the plugin to your app-level build.gradle:
apply plugin: 'com.google.gms.google-services'
app-level build.gradle:
dependencies {
compile 'com.google.android.gms:play-services-auth:9.8.0'
}
In my case I had to remove some files that were created by gradle at some point in my study to make things work. So, cleaning up after messing up and then it ran fine ...
If you experienced this issue in a git project, do git status and remove the unrevisioned files. (For me elasticsearch had a problem with plugins/analysis-icu).
Gradle Version : 5.1.1
Just for the record: I accidentally enabled Offline work under Preferences -> Build,Execution,Deployment -> Gradle -> uncheck Offline Work, but the error message was misleading

Categories

Resources