I have a gradle project like this:
root
|
|---- projectA
|
|---- projectB
...
My root build.gradle contains dependencies which are needed for projectA and projectB. I have defined them like this:
subprojects {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies {
compile 'com.google.guava:guava:23.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
Now I am trying to add a dependency on projectA from projectB so my projectB build.gradle looks like this:
dependencies {
implementation project(':projectA')
}
and projectB settings.gradle:
include ':projectA'
project(':projectA').projectDir = new File(settingsDir, '../projectA')
This is currently failing, as projectA and B do not depend on root to get their needed dependencies.
Can I add another dependency from projectA on root or what is the default gradle approach to share same dependencies from one root project?
Specifying dependencies with the "compile" keyword is being deprecated. The new keyword to use is "implementation". (See this SO question for an example of explanation.) The difference between "compile" and "implementation" as it relates to your case, is that "compile" propagates the dependency to all connected modules, while "implementation" is single-level. So, if you have
root
module A
module B
and module B has a depandency brought in with "compile", then root has access to that dependency.
But if you now change B to bring the dependency in with "implementation", then you'll still need to add the dependency to the root project's build.gradle.
Why is this relevant? Because a child module isn't allowed to know who its parent is. So, while A and B both see the dependencies you brought into root, they really shouldn't -- seeing those dependencies is a quirk of how Studio works. This is also why the answer to "Can I add another dependency from projectA on root" is "no, you're out of luck there, it'd create a circular dependency and that's not allowed".
The dependencies I'd try are:
In both module A and module B:
dependencies {
implementation 'com.google.guava:guava:23.0'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
Then in Module B, you do:
implementation project(':projectA')
Then in root, you depend on both A and B.
Your modules (A and B) are not correctly setup as child modules of the root project.
In your base folder, create if necessary the settings.gradle file, and add the child modules definition:
root/settings.gradle
include 'projectA', 'projectB'
The build.gradle file of projectB is ok as it is, then you need to remove the root/projectB/settings.gradle file; child-modules and their locations are specified in the parent build file.
Related
I am having two project(ProjectA and projectB). ProjectA have 2 tasks of creating jarA1.jar and jarA2.jar. This is configured with ProjectA's build.gradle. Now I want to add these two created jars as dependency to ProjectB , by just mentioning the project and not specifying the jar's file path separate.
Suppose in future if I add multiple jars at ProjectA , all the jars must be added as dependency to ProjectB without any changes in projectB's build.gradle.
ProjectA => build.gradle
task jarA1(type: Jar, dependsOn: compileGrpcJava) {
...
}
task jarA2(type: Jar, dependsOn: compileGrpcJava) {
...
}
jar {
dependsOn jarA1
dependsOn jarA2
}
ProjectB => build.gradle
Approach1:
dependencies {
implementation project (:ProjectA)
}
is not importing the Jars. But this works.
Approach2:
dependencies {
implementation project (:ProjectA)
implementation files('<path_to_jarA1.jar')
implementation files('<path_to_jarA2.jar')
}
Can anyone say why approach 1 is not working. Why adding the complete project as dependency not taking up all the jars of that project by default?
I got 2 java projects and 1 server all three having build.gradle file defining and configuring the dependencies I need in my project.
Code snippet of Liberty server build.gradle is as follows:
configuration{
project1
project2
oracle
extrasecuritystuff
}
dependencies{
project1: "fskfksd"
project2: "sdfd"
extrasecuritystuff 1."fsfd"
2."ewrwer"
}
doTask{....
My question is how do I exclude a transitive dependency present in 2."ewrwer". What is the groovy/gradle syntax for doing so?
Based on the gradle docs I tried something like this but did not work out, once I triggered the Jenkins build It was still pulling in the transitive dependency jars.
configurations {
project1
project2
extrasecuritystuff {
exclude group: 'javax.jms', module: 'jms'
exclude group: 'com.sun.jdmk', module: 'jmxtools'
}
}
dependencies {
project1 'org.javax.etc'
project2 'org.blah.blah'
extrasecuritystuff 'log4j:log4j:1.2.15'
}
I'm assuming here that you are using some plugin like the 'application' plugin to generate a .zip file of your project, and that your goal is to exclude the transitive dependency from the .zip file. The reason could be that the transitive dependency has an undesirable license, or is too large, and your code is never actually calling functions that need the transitive dependency. (Make absolutely 100% double sure of this, or your application will crash.) Or, the target system already has that dependency installed. (Likewise.)
You had a few syntax issues in your example code, but I've interpreted the question according to my understanding. In the example, my dependency ewrwer is pulling in lib-foo which I don't want.
configuration{
project1
project2
extrasecuritystuff
}
dependencies{
project1 ( [...] )
project2 ( [...] )
extrasecuritystuff ('org.blah:fsfd:1.0.1')
extrasecuritystuff ('org.bloop:ewrwer:2.12') {
exclude group: 'org.bleep', module: 'lib-foo'
}
}
For more about managing transitive dependencies, as always refer to the official Gradle docs.
Say I have a project ProjectA with a compile dependency on core. And core depends on deepcore. Thus, ProjectA has a transitive dependency on deepcore.
So, build script for ProjectA has this
dependencies {
compile "com.something:core:1.0.0"
}
And build script for core has this
dependencies {
compile "com.something:deep-core:1.0.0"
}
Now, there is a class CoreService defined in both core and deepcore with the same package structure. And I am using that class from my ProjectA, which implementation will it use? How do configure my dependency so that I am using the version from core?
This should do what you are looking for.
dependencies {
compile "com.something:deep-core:1.0.0" {
exclude group: 'com.unwanted', module: 'unwanted'
}
}
When different versions of dependencies are declared in the top-level build.gradle and sub-module build.gradle, which one takes precedence?
For example if in my top level build.gradle I had junit:junit:4.8.2 but in a sub-module had junit:junit:4.10 ?
Also, what does declaring a dependency in the top level build.gradle do? Should all dependencies just be declared in sub-modules?
Having
// root build.gradle
subprojects {
dependencies {
compile "junit:junit:4.10"
}
}
and
// submodule build.gradle
dependencies {
compile "junit:junit:4.8.2"
}
is essentially the same as just having
// submodule build.gradle
dependencies {
compile "junit:junit:4.8.2"
compile "junit:junit:4.10"
}
This means you have two version of junit declared in your submodule. Now Gradle conflict resolution kicks in here. The default resolution for
dependency conflicts is to choose the newer version. So junit 4.10 will be picked. To change this behaviour you can configure the resolution strategy.
I have two different java projects, projectA and projectB. I'm at a stage where projectB depends on projectA. How do I pull in the jars created by building projectA and all of its dependencies?
In essence, I would like to mirror the effect of adding a projectA to the build path of projectB in eclipse,effectively pulling in jars on the classpath projectA.
If the two projects are closely related you can make Project A and Project B part of the same Multi-project build, then:
//ProjectB build.gradle
dependencies {
compile project(':ProjectA')
}
If they're not closely related you can publish the output of Project A to a repository e.g. your local Maven, Artificatory, Bintray etc. Then in Project B you'll get it like any other dependency. E.g.
//ProjectB build.gradle
repositories {
mavenLocal()
//Or something like this:
//maven { url "http://urltoyourartifactory.com/repo/" }
}
dependencies {
compile 'com.yourname:project-a:1.0.0'
}