How can I include a Sibling Project in my Gradle Project? - java

I have two Gradle projects with the following directory structure:
/baseDir
/first
/first/build.gradle
/second
/second/build.gradle
Both firstand secondare on the same hierarchy. From the Gradle User Guide I found only a way to include sub projects. This is not the case here.
How can I include sibling Project second in project first?

Try to use a settings.gradle with the following content:
includeFlat "second"

Related

Gradle - one project, multiple packages, multiple jars

I'm asking you about a very basic question but I hope you can find the time to help me:
I'm trying to realise a java-project, that can spit out several different programs which partially have dependencies on other projects of mine.
In order to keep it simple, I want to have all the code in one project, run by Gradle, so if I make changes to a central library (the database connector for example) all the child-programs automatically recieve the changes.
An example could look like this:
project:
program_A
central_library
program_B
output:
program_A.jar (including central library)
program_B.jar (including central library)
Now I'm having serious troubles finding a correct buildscript for this and was wondering if someone here could help me out.
P.S. : Since I'm new to this, if I should realize this through different modules within the Gradleproject instead of different packages in the Gradleprojects sourcefile, feel free to tell me :)
One way to approach this is to have a root project, that holds the three other projects inside of it.
Specify the sub-projects inside its settings.gradle file:
rootProject.name = 'RootProject'
include 'program_A'
include 'central_library'
include 'program_B'
With this in place, program_a can depend on central_library by adding a dependency in its build.gradle:
dependencies {
compile project(':central_library')
}
I have a similar setup in one of my projects, although the "central library" is the root project and the submodules are test environments.
Create a root directory and put each library or program into its own sub-directory.
Create a gradle project in each subproject.
You can for example create a skeleton gradle project by running
gradle init --type=java-library
or
gradle init --type=java-application
Then in the root directory create a gradle multi-module project. Basically
run only
gradle init
and then create a settings.gradle and list all sub-projects there.
This is actually described very well in the gradle documentation:
https://guides.gradle.org/creating-multi-project-builds/
If I understand correctly, what you want to do is, when you change your local projects, you want other projects to see those details. For this you need to publish your projects to some kind of repo, like maven repo. You can do this from command line gradle publishToMavenLocal, or gradle build pTMl. You can also do this in build.gradle file with something like the following:
task sourceJar (type : Jar) {
classifier = constants.extSources
from sourceSets.main.allSource
}
publications {
mavenJava(MavenPublication) {
from components.java
artifact(sourceJar) {
classifier "sources" //classifier = constants.extSources
}
}
}

gradle include library from another project

I have this project setup (in Android AIDE):
|---Project1
| |---App
| |---Lib
|
|---Project2
|---App
I want add project 1 Lib as dependency (or linked source, because i only need to access 1 class from the library) to Project2.
Project2 settings.gradle:
include ':Lib'
project(':Lib').projectDir = new File(settingsDir, '../Project1/Lib')
Project2 build.gradle:
compile project(':Lib')
I am getting the following error:
Project dependency 'Lib' not found.
Is there anything wrong with the folder path? I didn't find any good solution for this problem and i am not very familiar with gradle.
If i would instead link the library source folder to Project2 i tried to add the following lines to the build.gradle (under android section):
sourceSets {
main.java.srcDirs += '/../Project1/Lib/src/main/'
}
I could access the source if i used the full path down to the folder with the class file but with the problem that when i instantiated the class from the Lib in Project2 it gave me the error that the class was not found inside the package.
Do both projects need to have the same package names? What would be the best solution to manage this without having the Library as an standalone library?

How to add root Project as classpath in Gradle

In JAVA eclipse, the main class of the java project is defined as one of the class in the JAR of the 'lib' folder, and the root project (fooBar) is added as a dependency in the classpath.
This can be acheived in eclipse by simply adding the root project in the classpath. However, how do we add the dependency of the root project in gradle?
dependencies {
compile project(':fooBar');
}
> Project with path ':fooBar' could not be found in root project 'fooBar'.
The following is the project structure:
fooBar
-src/main/java
-src/test/java
-JRE System Library
-gradle
--wrapper
--launch.gradle
-lib
-build.gradle
-settings.gradle
-gradle-apps.settings
-gradle.properties
-gradlew
-gradlew.bat
I guess I got your point. First of all I have a doubt. Why are you adding
dependencies {
compile project(':fooBar');
}
this entry in build.gradle of fooBar project? This can cause cyclic dependency and results in build failure.
So I guess remove the above entry from build.gradle of fooBar project.
And please add the below code in your settings.gradle file:
rootProject.name = 'fooBar'
include ':fooBar'
Hope this will work.
As far as the project method is concerned, the root project has no name. So this is the syntax in the subproject's build.gradle:
dependencies {
compile project(':')
}
However, it is rarely a good idea to do this. It is too easy to end up with circular dependencies. Most multi-module projects have a separate "main" projects (called something like "main", "core" or "base"), and other modules can easily depend on that using compile project(':core') or whatever.
(Originally answered over here.)

Depend on a project from another Gradle script

I have the following folder structure:
projects
build.gradle
core/
samples/
sample1/
build.gradle
core/
desktop/
ios/
html/
android/
The core project is a Java library while sample1 is a Libgdx application that wants to depend on core.
The outermost build file is responsible for the core project as well as another project (not listed).
The problem is that the Libgdx application also contains a core project, which hosts all the shared game logic. This is the template generated by the Libgdx project setup tool.
How could I work around this in Gradle without renaming any of the projects?
In the root projects level, you need to add 'settings.gradle' which have something like:
rootProject.name = 'projects'
include ':core'
project(':core').projectDir = "$rootDir/core" as File
include ':samples:sample1:core'
project(':samples:sample1:core').projectDir = "$rootDir/samples/sample1/core" as File
include ':samples:sample1:desktop'
project(':samples:sample1:desktop').projectDir = "$rootDir/samples/sample1/desktop" as File
...
in your project(desktop for example) build.gradle you can add dependencies like:
compile project(':core')
compile project(':samples:sample1:core')
ps: Libgdx is nice

Android Studio: add project as library

I want to add this project as library to my project in android studio.
this is what I tried,
I have my project directory as f:/my project/my app/src
and my library in f:/my project/my library/src
I import the module (the library) by going to file > import module > selecting the library
then I got to file > project structure > modules > dependencies tab > select my project > add module dependency apply ok and then done
however when I use the code from the library I get the usual syntax error (the class ... could not be found)
also I noticed this popup (see image)
I am new to android studio or intelliJ, how do I fix this.
Thanks!
Edit the settings.gradle file (in directory f:/my project), it must contains something like this:
include 'my app','my library'
If this file don't exists: create it manually. The settings.gradle contains the list of gradle modules in a multi-module project.
Then you must add the dependency to your library in app. To do so edit the my app/build.gradle and add this line :
dependencies {
compile project(':my library')
}
I also notice that you don't use default structure for your projects (i.e. you put the code in src/ instead of src/main/java) so you will have to overwrite some values of the default fileSet in the build.gradle of your projects. Be sure to have something like this in my app/build.gradle and my library/build.gradle :
android {
sourceSets {
main {
java.srcDirs = ['src']
}
}
}

Categories