My project uses Gradle's multi-project system. Most of my projects include the "lemur-common" library like this
dependencies {
compile project(":lemur-common")
}
"lemur-common" also has a unit test directory, which has a somewhat complicated dependency statement.
dependencies {
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.9.RELEASE'
testCompile group: 'org.mockito', name: 'mockito-inline', version: '3.5.6'
constraints {
implementation('org.mockito:mockito-core:3.5.6') {
because 'Fixes illegal reflective access warning'
}
}
}
Now, all of my other projects need to have this same line, since they're all using spring-boot-starter-test. I've done quite a bit of fiddling, trying to express something like "project-a's testCompile should have the same dependencies as lemur-common" but I haven't gotten it to work.
Is it possible to express this in Gradle? Does anyone know how?
You'll want to create a custom Gradle plugin.
As a sibling of all your project dirs, create a buildSrc directory, which itself will have a Gradle file (it needs to be built like everything else). Make gradle file under src, which will be your plugin. Put all of the shared gradle code (the dependencies block you posted) in that file. For example:
- buildSrc
|── build.gradle
|── settings.gradle
|── src
|── main
|── groovy
|── unit-test-dependencies.gradle
Then, in the build.gradle of lemur-common, projectA, and etc. add this line
plugins {
id 'unit-test-dependencies'
}
That should do it. Gradle's documentation for this feature is here: https://docs.gradle.org/current/samples/sample_convention_plugins.html
Related
I'm new to IntelliJ and Gradle
I've got a Maven Project with a lot of dependencies which works on it's own.
Now I should use the libraries from that Project and create a Plugin for IntelliJ in Gradle.
I tried various ways to add the dependencies in the IntelliJ Module Settings, which allowed me to use the needed classes to write my code and build it. However, when I tried to start the plugin, it couldn't find the classes anymore.
I think I need to specify these in the build.gradle but I don't understand exactly how, as all the ways I tried didn't worked.
build.gradle:
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.6.5'
}
group 'com.ast.devmate.intellij'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
**compile 'com.ast.devmate.intellij:root:1.0.0-SNAPSHOT'**
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.1'
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
gets me this:
Could not find com.ast.devmate.intellij:root:1.0.0-SNAPSHOT.
without the line marked with ** I got a lot of
error: package foo does not exist
import foo;
It looks like you're trying to access a custom library using Gradle. You will probably need to use a file dependency: How to add local .jar file dependency to build.gradle file?
I am using gradle version 4.6:
in dependencies I add this:
testCompile "com.kuku:kuku:1.0:tests"
also tried this and same:
testCompile group: "com.kuku", name: "kuku", version: "1.0", classifier: "tests"
then gradle throw error:
Could not resolve all files for configuration ':testCompileClasspath'.
Could not find kuku-tests.jar (project :serializer_v9).
what I am doing wrong? needless to say I see the kuku tests jar in the build directory
So I found that the problem is Gradle includeBuild does not support yet publication that does not default: here
So the only way is to create separate jar with the test classes
I've multi-module gradle project. Let's say project parent has modules module1 and module2.
I have included both the modules in settings.gradle in the parent project as well.
When I declare the common dependencies in build.gradle of parent project, both the project is not compiling but when I add the dependencies in build.gradle of each module then the modules are compiling successfully.
Any Idea how can I do this.
You can declare dependencies for all modules in your project using the allprojects statement in your parent module. This is the essential way of doing so in your parent build.gradle file:
allprojects {
// Plugins can go here, example:
apply plugin: 'java'
dependencies {
// Dependencies go here, example:
compile group: 'org.apache.shiro', name: 'shiro-core', version: '1.4.0'
}
}
Check this and this answer for more insight on the configuration point of a multi project.
Also have a look at the Gradle documentation about this topic for a deep dive.
Inside my module build script (build.gradle) I can set dependencies:
dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
}
Gradle Example 8.2
I can also use buildscript() method inside the build script and set dependencies:
If your build script needs to use external libraries, you can add them
to the script's classpath in the build script itself. You do this
using the buildscript() method, passing in a closure which declares
the build script classpath.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'commons-codec', name: 'commons-codec', version: '1.2'
}
}
Gradle Example 59.6
My question, are these the same? Is there any difference between these two ways of setting dependencies for the build script?
There is a big difference. The former declares (compile) dependencies of your code; the latter declares dependencies of the build script itself (i.e. it allows to use commons-codec right in the build script).
I have a simple project where I'd like to unjar a subset of my dependencies and pack them into the output jar.
I have the two configurations:
configurations {
embed
all
}
dependencies {
embed group: 'commons-collections', name: 'commons-collections', version: '3.2'
...
all embed
all group: 'something-not-embeddable', name: 'dontembed'
compile all
}
According to http://www.gradle.org/docs/current/userguide/dependency_management.html 50.5 Working with Dependencies section's example it should work.
In a later section of my build, I want to unjar the embed jars and use them as source for jar.
My problem is that the gradle output says:
> Could not find method all() for arguments [configuration ':embed'] on root project 'myproject'.
Can you tell me why my approach is not working and how could I fix it?
Lol, looks like I chose a bad configuration name, works with alldeps instead of all