gradle multiple jars from multiple folders - gradle

i have a project layout like
sample/
A/
src/
main/
java/
A.java
res/
A.jpg
B/
src/
main/
java/
B.java
res/
B.jpg
build.gradle
settings.gradle
how to create two separate jar files(A.jar/B.jar)
A.jar includes A.java, A.jpg
B.jar includes B.java, B.jpg

Here you can find a sample project. Basically all configuration is put in build.gradle and settings.gradle files:
build.gradle
subprojects {
apply plugin: 'java'
}
settings.gradle
include 'A', 'B'

Related

Gradle: exclude build.gradle.kts in a subdirectory

Is there a way to tell Gradle
"Hey, there's a build.gradle.kts in the directory some/subdir, but please act like it's not there and don't try to mess with it at all"?
I have to keep a non-Android Kotlin project in an Android repository with a build.gradle.kts-file in the repository root for ... reasons ... and it keeps breaking my build due to plugin version conflicts.
The hierarchy layout is as follows:
|-- build.gradle.kts
|-- settings.gradle.kts
|-- src/
|-- android_project_a/
|-- build.gradle.kts
|-- android_project_b/
|-- build.gradle.kts
|-- kotlin_project/
|-- build.gradle.kts // This is what I need to exclude
If you are using command line to build, You can execute subdir as following
gradle build -x kotlin_project:build
But this is not a nice way to have it done, I wanted to check your build.gradle and setting.gradle, As you can change that once and for all, If you change the sub-projects and implementation of your sub-projects.
This kotlin_project should be a part of your setting.gradle as its being build automatically when you trigger root dir build.
You can exclude it if you want, But you have to remove it as well from dependencies at your build.gradle

How to include another root project in settings.gradle

I have a 2 root projects in gradle one main root project1 from where I invoke gradle build and has settings.gradle which includes subprojects to build
Another root project2 where I have settings.gradle which includes subprojects to build
Rootproject1
build.gradle
settings.gradle
Rootproject2
build.gradle
settings.gradle
I want to inlcude Rootproject2 in my Rootproject1 settings.gradle file so that from Rootproject1 (gradle build) should build Rootproject1 as well as Rootproject2
include/inlcudeFlat -- it is relative to rootproject
There should be one settings.gradle which will be placed inside the primary root folder and you will need to write:
include ':Rootproject1',
':Rootproject2'
Then, in the build.gradle file of Rootproject1, you will need to write:
dependencies {
implementation project(path: ':Rootproject2')
}
[Updated on Jan 23, 2019] Something like following:
Root
|------ Rootproject1
|
|------ Rootproject2
|
|------ settings.gradle

Gradle parent and sub-projects as siblings

In a gradle multi-module project, can the parent and sub-projects be siblings?
-parent
--build.gradle
--settings.gradle
-sub-project-1
--build.gradle
-sub-directory
--sub-module-2-1
---build.gradle
--sub-module-2-2
---build.gradle
Yes, it's possible to configure it this way, by declaring subprojects within settings.gradle as includeFlat instead of simple include. You can read about it here
But in that case, you have to run Gradle from the directory. where settings.gradle file is located, otherwise it won't find any subprojects.

submodule of module not working in gradle

I have a root project gradletinker with sub-module xt-domain-layer. The settings.gradle of gradletinker is:
rootProject.name = 'gradletinker'
include 'xt-domain-layer'
The xt-domain-layer also contains a submodule called xt-web. The settings.gradle of xt-domain-layer is:
rootProject.name = 'xt-domain-layer'
include 'xt-web'
Now from the root project graldetinker when I run \gradletinker>gradlew projects I do not getting the submodule xt-web listed
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'gradletinker'
\--- Project ':xt-domain-layer'
\gradletinker>gradlew -q :xt-domain-layer:projects
------------------------------------------------------------
Project :xt-domain-layer
------------------------------------------------------------
Project ':xt-domain-layer'
No sub-projects
Any idea where I am going wrong?
You use settings.gradle to add sub-projects to a gradle projects. The mechanism to add it is:
include 'subproject-folder-name'
or alternatively, if the subproject is located in some arbitrary location:
include ":subProj"
project(":subProj").projectDir = file("path/to/subproject")
In your case, add to xt-domain-layer settings.gradle:
include 'xt-web'
As per gradle docs. I only need a single settings.gradle file at the root level.
rootProject.name = 'gradletinker'
include 'xt-domain-layer:xt-web
So removed the settings.gradle files and added the following in settings.gradle at the root level.
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'gradletinker'
\--- Project ':xt-domain-layer'
\--- Project ':xt-domain-layer:xt-web'

How to add other projects in current project in Gradle?

I have two projects ProjA and ProjB, here ProjB is depends on ProjA. So, how to include ProjA in ProjB?
And please let me know in case change in the ProjA build.gradle file?
Okay, so you have two projects, whereas ProjA depends on ProjB.
If you have already set up a gradle build for those, both of them will have a file settings.gradle, which at least defines the project name and build.gradle, where you can specify the dependencies.
Example:
settings.gradle
rootProject.name = 'ProjA'
build.gradle
group = 'mygroup'
version = '1.0'
By further adding one of Gradle's publishing plugins (see Maven publishing or Ivy publishing), you can configure a publication. A publication in turn contains artifacts, that is the files you want to upload (and usually some repositories).
Example for a minimal Maven publication:
apply plugin: 'maven-publish'
publishing {
publications {
core(MavenPublication) {
from components.java
}
}
}
The maven-publish plugin will add the task publishToMavenLocal (among others) to your project which installs the jar somewhere to ~/.gradle/caches/.
In build.gradle of ProjB you can then define a compile time dependency on ProjA like so:
dependencies {
compile(group: 'mygroup', name: 'ProjA', version: '1.0')
}
If your projects are co-located, you could have a multi build project.
Say you have a root folder with these contents:
some root directory
project-a
project-a.gradle
src
project-b
project-b.gradle
src
build.gradle
settings.gradle
settings.gradle contains:
include 'project-a', 'project-b'
project-b.gradle then depends on project A:
dependencies {
compile project(':project-a')
}
After this, project a will always compile and assemble before project-b and the jar file for project-a jar will be on the classpath when compiling project-b.

Resources