Do the dependencies in build.gradle generated automatically? - java

Does gradle work like go mod ? First importing some dependencies in java file such as import org.apache.hadoop.mapreduce.Mapper, then calling gradle build and gradle would generate dependencies in build.gradle automatically, also download all the jars needed to gradle cache.
Or, all the denpendencies we need must add to build.gradle manully? Which version should append to the denpendency, search in the maven repo one by one?
Thank~

Gradle uses the dependencies declared in the build file to create the compile classpath. Similarly it will use test dependencies for the test compile classpath and the test runtime classpath.
So you have to declare the dependencies first, including figuring out which version you want to use in your project, and then you will be able to compile and run code that leverages these libraries.

Related

Add jar dependency to gradle task without actually including its classes to the output

I currently use gradle build script that looks like this:
apply plugin: 'java'
dependencies {
compile files('libs/android.jar')
}
I need now to build a jar that doesn't include classes from android.jar
Any ideas?
You'll want to use feature variant to model "optional" dependencies: https://docs.gradle.org/current/userguide/feature_variants.html
So, the jar that is built this way contains only classes built from java files. To include them, use Shadow. If you want them just to be compile-time classpath, use compileClasspath property of java source set.

Dependencies of a repackaged jar not working

I have the following dependency in my gradle file.
compile 'org.A:A:1.0'
which automatically pulls in
'org.B:B:1.0'
and many other jars which it depends on.
But,my project requires repackaged A.jar (let's call it A*.jar which I installed in a local maven repository as custom version).
So now I change the dependency as below
compile 'org.A:A:custom'
which doesn't pull in any of the dependencies mentioned in the pom.xml file present inside the A.
jar file (which it would, had it been org.A:A:1.0)
My questions are:
1) Based on what does the statement compile org.A:A:1.0 pull other jars ? Is it pom.xml file present inside the jar?
2) What are the changes required if I want to automatically pull in both 'org.B:B:custom' and regular versions of other jars which are dependee of org.A:A:1.0
Maven will read the pom file for the artifact it resolves as well. In there the dependencies are found and resolved.
You simply need to also upload the pom of A*.jar and modify the version of it accordingly to A* - that should already do the trick.

Gradle transitive dependency to JAR inside of a library module

I'm migrating an Android multi-module project from Ant to Gradle.
We have some jars in the repo in the libs directory of a library module.
The dependency is as follows:
AppModule depends on LibModule.
The code in AppModule cannot access contents of the library jar that is in LibModule.
Although it can access from-source classes of LibModule (proving that the dependency in general is established).
Gradle documentation says, that all dependencies are transitive by default, but this experience seems to invalidate such claim. Is this a bug or is there some legitimate reason?
I've managed to hack it by adding a workaround dependency in AppModule:
compile fileTree(dir: '../LibModule/libs', include: ['*.jar']) // HACK!
But there should be a more DRY way to do this, right?
Gradle version: 2.1.
Interestingly, Android Studio appears to respect the transitiveness of the jar and does not signal an error.
The error occurs when I'm building using
./gradlew assembleDebug
Thy typical java error is signalled:
error: package net.jcip.annotations does not exist
import net.jcip.annotations.NotThreadSafe;
^
I know I can also specify deps in a maven-ish style, but we would like to be able to work with jars-in-the-repo as well, for our purposes.
TIA, Karol

Gradle specifying sources for dependencies specified with files

i specify my dependencies like the following:
dependencies {
compile files(
'v:\company\components\component1\v1\00\01\release\myComponent1.jar'
,'v:\company\components\component2\v1\00\01\release\myComponent2.jar'
,'v:\company\components\component3\v1\00\01\release\myComponent3.jar'
,'v:\company\components\component4\v1\00\01\release\myComponent4.jar'
,'v:\company\components\component5\v1\00\01\release\myComponent5.jar')
}
now in each of thse component versions, if you are in a release, you go up one directory and go into src you find jar with the sources in.
v:\company\components\component1\v1\00\01\src\myComponent1.jar
Is there anyway i can also reference those from my gradle build script for both intellij idea and eclipse?
Thanks.

How to include dependencies in android library gradle project?

I'm building an android library project with Gradle using Android Studio. It has some local dependencies:
compile project(':androidlibrary')
with nested, additional external dependencies:
compile group: 'com.google.guava', name: 'guava', version: '14.0.1'
I managed to build the aar file but no external or local dependencies are included. I expect external dependencies to be mentioned in the POM file when published to maven, but what about the local ones?
What is the right way to build such project?
I ended up using "download library from maven" feature of Intellj IDEA as described here. It downloads the library and all it's dependencies to the local directory.
Just a friendly warning: including dependencies is considered a very bad practice at SHOULD BE AVOIDED. It may cause conflicts.
There is no automated way to package the dependencies of the library inside the aar. This is contrary to proper dependency management and can lead to problems down the line.
If you really want to do this, you'd have to gather the dependencies manually (using code in build.gradle) and manually package the files in the aar.

Categories