Remove dependency in Gradle - java

In my project I have to libraries that uses com.google.javascript:closure-compiler. Due to some jars problem I want to exclude one. To do this I wrote this code:
compile('com.bertramlabs.plugins:asset-pipeline-core:2.8.1') {
exclude group: 'com.google.javascript', module: 'closure-compiler'
}
When I run ./gradlew dependecies old jar seems to be removed.
My problem occurs when I run ./gradlew install and import my lib in another project, because in another project I have both jars. How can I remove it?

You could try adding a provided configuration as described here.
Once that is in place, try changing your code to something like:
provided('com.bertramlabs.plugins:asset-pipeline-core:2.8.1') {
exclude group: 'com.google.javascript', module: 'closure-compiler'
}

Related

Gradle dependencies: package does not exist

I know this has been asked multiple times but the questions have multiple answers.
I'm trying to use a Java package that's a dependency of my dependency. Let's say I've built this gradle project called "ee_tools". And that has a dependency called "my_models", which has a package called "com.mycompany.my_models.db". So in the ee_tools project build.gradle, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "my_models", version: "1.0.0"
}
Then in my current project, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "ee_tools", version: "1.0.0"
Shouldn't this mean that the public classes in my_models are accessible through ee_tools to my current project? Gradle was able to find both in my artifactory instance. And the gradle dependencies command shows ee_tools under the compileClasspath, implementation, and testCompileClasspath trees, but not with its children dependencies. It also shows up in the runtimeClasspath and testRuntimeClasspath trees with its children dependencies, including my_models.
I am also able to see that package inside the jar on the left side of IntelliJ, under the "External Libraries" tree, along with some classes.
But when I try to use the my_models package in my current project, IntelliJ can't find it and it fails a gradle build with the error
error: package com.company.my_models.db does not exist
It can't find any package in that dependency. What gives? Am I declaring the dependencies incorrectly? Or is this a gap between the gradle binary in my command line vs IntellJ and gradlew?
If ee_tools depends on my_models, then your gradle file in ee_tools should be like
implementation project(path: ":path:to:my_models", configuration: 'default')
:path:to:my_models is defined in settings.gradle in project root path like this:
include ':path:to:my_models'

Do an exclusion in build.gradle?

I want to add an exclusion to my build.gradle because my jar doesnt run after I compile it. The problem can be fixed if some modules are excluded.
How do I do that correctly? The following code should fix the problem:
compile ("com.badlogicgames.gdx:gdx-tools:$gdxVersion") {
exclude group: 'com.badlogicgames.gdx', module: 'gdx-backend-lwjgl'
}
For the most recent gradle versions (7+), you need to use the new syntax implementation or compileOnly. This should also be in a dependencies block as shown below:
dependencies {
implementation("com.badlogicgames.gdx:gdx-tools:$gdxVersion") {
exclude group: 'com.badlogicgames.gdx', module: 'gdx-backend-lwjgl'
}
}
See for more info
However for gradle 6 and below, your compile block should work fine inside a dependencies block.

Replace android.support.v4.util.ArraySet with androidx.collection.ArraySet in Maven library

I have a 3rd party library that depends on:
import android.support.v4.util.ArraySet;
And I want to use the latest Android libraries in the rest of my project:
import androidx.collection.ArraySet;
However, since I'm getting the library via Maven, I can't seem to force it to replace the v4 dependency for the androidx one and have it refer to the right class.
I've already tried:
implementation 'androidx.collection:collection:1.1.0'
implementation('some-library:v1') {
exclude group: 'com.android.support', module: 'support-v4'
}
And:
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module('com.android.support:support-v4') with module('androidx.collection:collection:1.1.0')
}
}
I've even tried a few more things including the force=true for the dependencies I want.
However I'm still getting:
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArraySet;
How can I tell Gradle to resolve the imports for android.support.v4.util.ArraySet to androidx.collection.ArraySet when I don't have access to the source code?
As per the Migrate to AndroidX guide:
android.enableJetifier=true
The Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries.
So this rewriting of Maven dependencies is exactly what Jetifier does for you. Make sure you've added the android.enableJetifier=true to your gradle.properties file to enable Jetifier.

Gradle add library to classpath

I am using Gradle for a project. My build.gradle has got JUnit as a dependency, but every time I import the project again it does not recognize the JUnit library and asks me to add it to classpath. This problem does not only apply to JUnit, but also to other dependencies. Did I add JUnit to the wrong place?
PS: The project builds just fine, it's just the IDE (IntelliJ) that's marking everything red.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
The dependency declaration is correct. Probably you're opening the project in IntelliJ IDEA the wrong way.
I suggest you to open a Gradle project this way:
Click the File | Open menu
Select the build.gradle file
Here is further information about importing Gradle projects (see also the side note in that page).
download the external jar file from mvn repository and add it to libs folder.
Gradle-project(your project)->libs(inside libs folder paste jar file). if you don't have libs folder just create it.
go to build.gradle
dependencies { compile files('libs/your_jar.jar') }
reload the gradle project.
it's worked for me. i hope it's work for you guys also...

Gradle not downloading test dependencies

I am very new with Gradle and I would like to download all my test dependencies using Gradle. I used gradle init to generate my build file, and copied some dependencis from my previous scripts. However upon using gradle --refresh-dependencies in the project root, the test dependencies still do not download.
I tried searching for answers as to why this happens, but they don't seem to fix my issue. Is there something wrong with my build file?
Particularly, I am after downloading mockito and hamcrest below.
build file:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile "junit:junit:4.11"
testCompile "org.mockito:mockito-core:1.9.5"
testCompile "org.hamcrest:hamcrest-library:1.3"
}
test {
testLogging {
events 'started', 'passed'
}
}
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
I am also using Eclipse if that helps.
EDIT: After adding the Gradle nature to my project, it seemed to work. Can anyone explain why?
As far as I understand, --refresh-dependencies makes sure that the already cached dependencies are ignored and that dependencies are re-downloaded when they're needed.
Just use gradlew build, or gradlew test, and gradle will compile your tests, and thus download the missing dependencies.
It seemed to work now, after I added the Gradle nature to my project, then cleaning and regenerating eclipse files.

Categories