Intellij can't find commons.dbcp2 - java

I have a problem which drives me insane, since it should work.
I have compiled dbcp dependency in my gradle file
compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
still, it can't resolve if i refer to it like this:
import org.apache.commons.dbcp2.BasicDataSource;
Any idea? (using intellij)

The problem was that the gradle project was unlinked, so the dependencies - even thought the dependencies was there in the external libraries, i had to re-link the gradle.

Related

Problem with gradle and external jar - jar not found

I have a problem with Gradle. I want to use an externally provided Jar: itext-2.1.7.js6.jar, it is a patched version used by JasperReports.
In the project structure, I have a /libs directory, containing 2 files: fonts.jar and itext-2.1.7.js6.jar
Part of my gradle.build file looks like this:
compile fileTree(dir: 'libs', include: ['*.jar'])
implementation group: 'net.sf.jasperreports', name: 'jasperreports', version: '6.7.1'
implementation group: 'net.sf.jasperreports', name: 'jasperreports-functions', version: '6.8.0'
implementation group: 'joda-time', name: 'joda-time', version: '2.9.9'
//implementation group: 'hu.blackbelt.bundles.itext', name: 'com.lowagie.itext', version: '2.1.7-1'
The last dependency is commented out cause I want to use the external JAR instead of the original iText.
When I am trying to run my app I get:
BUILD FAILED in 0s
1 actionable task: 1 executed
Could not find com.lowagie:itext:2.1.7.js6.
Required by:
project : > net.sf.jasperreports:jasperreports:6.7.1
What am I doing wrong? Everything is fine with my fonts.jar, so I think that it should be the same with iText, but it isn't...
As can be seen in the pom file of jasperreports, there is an explicit dependency on com.lowagie:itext:2.1.7.js6 which can also be found in a specific Jasper Reports repository
The reason this works transparently in Maven is that Maven will use the repositories defined in a pom file, while Gradle will not use them, as they could be a source of insecurity.
You have two options:
Add that repository, potentially using repository filtering to only serve these artifacts from there and then remove the file from libs
Add an exclude to jasperreportsso that it no longer tries to fetch that dependency. And instead the runtime code will use the version found in libs. You just forget about transitive dependency management in this case, for example when upgrading jasperreports and getting (or not) an updated version from their dependency declaration.

gradle dependency unusable after refresh in intellij

as in the title, gradle dependency unusable after refreshing. I can see twitter4j is added in external libraries along with other gradle dependencies.
Gradle file:
compile group: 'org.twitter4j', name: 'twitter4j', version: '4.0.6', ext: 'pom'
java:
import twitter4j.*;
error:
cannot resolve symbol "twitter4j"
changing the gradle line to
compile 'org.twitter4j:twitter4j-core:4.0.6'
resolves this problem.

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.

Gradle not downloading test dependency

I am looking to add a test dependency to my project which uses gradle. What I simply need is for gradle to download the dependency jar so I can import part of the package. What I need is the following package:
import org.springframework.security.test.*
So I edited my gradle file to contain
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
runtime('org.hsqldb:hsqldb')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test') // This is what I added to try and download the new dependency
}
I tried building the project after entering this. Unfortunately this didn't work and I cannot import org.springframework.security.test.* I then ran gradlew test which actually had some success and showed:
Download https://repo1.maven.org/maven2/org/springframework/security/spring-security-test/4.0.3.RELEASE/spring-security-test-4.0.3.RELEASE.pom
Download https://repo1.maven.org/maven2/org/springframework/security/spring-security-test/4.0.3.RELEASE/spring-security-test-4.0.3.RELEASE.jar
Though I have no idea where it downloaded to. Navigating to $HOME/.gradle/caches/modules-2/files-2.1/org.springframework.security I can see no sign of the downloaded package (and Idea doesn't recognise it in its external libraries). Running gradle build --refresh-dependencies didn't seem to work.
Any help would be very much appreciated.
Closes SO question I could find
I had the same issue, it works if a specific version is included in the dependency, such as:
testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '5.1.6.RELEASE'

Gradle subproject jar and add to Eclipse referenced libraries

I have a repo at http://www.github.com/kourbou/HyperQuest.
I have a git submodule to a project called SpongeAPI. I would like to add the compiled jar of the subproject to the Eclipse project automatically but I have failed at doing so. I have added this to my gradle.build :
dependencies
{
jar project('SpongeAPI')
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
But the build fails and this happens:
A problem occurred evaluating root project 'HyperQuest'.
> Could not find method jar() for arguments [project ':SpongeAPI'] on root project 'HyperQuest'.
Could someone give me an example or write a gist of a build.gradle that works? Thanks.
Either set up multi-project build or publish the output of SpongeAPI into some repository and consume it from that place.
Multi-project build is described in http://www.gradle.org/docs/current/userguide/tutorial_java_projects.html#sec:examples - you will add settings.gradle that will include SpongeAPI and then the dependency is like compile project(':SpongeAPI').
The publish approach means that you will upload the artifact from SpongeAPI build into a repository and your build will add this repository (like you add mavenCentral() or jcenter()) and refer to your artifact using common notation. Again there is a documentation to help you with that - http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html#N10669
I know you posted this early on in SpongeAPI's development, but I'll add this here for future readers.
By including SpongeAPI as a git-submodule, you are potentially tying your plugin to a specific, in development version of the API.
Instead, we provide a maven repository that you should use at https://repo.spongepowered.org/maven/
When using a version of SpongeAPI you MUST stick to released API versions, and not snapshots.
Otherwise your plugin has the potential to break on dev releases of the API.
You can read up to date information on how to set up a Sponge Plugin project here: https://docs.spongepowered.org/stable/en/plugin/project/index.html

Categories

Resources