Running gradle tasks from a subproject with unknown root - eclipse

I have a multi-project setup in gradle with a somewhat-flat structure like so:
RootProject/
build.gradle
settings.gradle
SubProjectA/
build.gradle
anotherDirectory/
SubProjectB/
build.gradle
SubProjectC/
build.gradle
The root project is really just a root build. It's there to tie all of the other projects together. SubProjectB depends on SubProjectC, and C depends on A.
Running tasks from the root project works as expected. I can run gradle :SubProjectB:build and it will build SubProjectB and its dependencies without error.
My problem is that if I try to run a task from a sub-project's directory, it fails if that sub-project depends on any of the other sub-projects because it doesn't know where any of the others are without the root project's settings file. So running A's build from its directory would work, but running B's or C's would fail.
Is there anything I can do within this structure to define what the root project actually is, so that if I run gradle build from B, it would behave the same as running gradle :SubProjectB:build from the root project's directory?
The end goals are to make it easier to use gradle from the command line, and to get functional default tasks on the sub-projects. That way I should be able to double-click a project Eclipse's Gradle tasks list, and have it run the default tasks as though it were running the tasks from root.

In this case, you can use composite builds. Let say you are at SubProjectA and want to build against SubProjectB. The you can execute the command below:
gradle --include-build ../anotherDirectory/SubProjectB run
SubProjectB will build first and then SubProjectA.

Related

Your project should have a settings.gradle

Hi I'm try to build a flutter app on git lab and every time its fails and show me the Error below:
Executing Gradle tasks as part of a build without a settings file
is not supported. Make sure that you are executing Gradle from a
directory within your Gradle project. Your project should have a
'settings.gradle(.kts)' file in the root directory.
I changed the location of the file setting.gradle and not work
any help??
the root of gradle
After searching and going through various trial and error methods, I finally tried the gradle init command and it fixed the issue. Alththough the settings.gradle file was already present but somehow its not seen by gradle itself, and initializing gradle fixed the error for me.

How do I set up my JavaFX project as a Gradle build?

I've created a new JavaFX project via the e(fx)clipse plugin, then staged and committed to Git. Following that I tried to run it and got a 'class not found' error - needed to add JavaFX to the --module-path in VM arguments. But it still won't run:
Error: Could not find or load main class Files\Java\AdoptOpenJDK\javafx-sdk-13.0.2\lib
Caused by: java.lang.ClassNotFoundException: Files\Java\AdoptOpenJDK\javafx-sdk-13.0.2\lib
Then I noticed that I have no build files in the project tree - no pom.xml or build.gradle, etc.
I decided to build with Gradle (previously only used Maven). After a lot of hunting on the net I did the following:
Project context menu -> Configure -> Add Gradle nature (this didn't visibly do anything, but the option is no longer available).
In the Gradle Tasks view, run init. (This ran fine until it got to Execute setupProjectLayout for :init. This got stuck and I let it sit for about 20 mins, but there was no activity (checked task manager to confirm)).
There must be something fundamental that I'm missing here, surely it shouldn't be this difficult. How can I get this project to build in Gradle?
The Gradle init task is an interactive task to generate a new Gradle project. If you run it from the IDE but have no way to provide answers to the prompts from Gradle, it will indeed wait forever on an input.
You do not need a CLI for input.
After you run the gradle task in the "Gradle Tasks" view you switch to the "Console" view and you are presented with the questions by gradle init. You answer those questions and finish the task.
Working Directory: /path/to/your/project/thenewgradleproject
Gradle user home: /home/user/.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 6.8
Java Home: /usr/lib/jvm/java-11-openjdk-amd64
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: init
> Task :wrapper UP-TO-DATE
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4]
> Task :init
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2]
Project name (default: thenewgradleproject):
> Task :init
Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/6.8/samples
BUILD SUCCESSFUL in 53s
2 actionable tasks: 1 executed, 1 up-to-date

Gradle scala project on intellij

How do we create a new gradle project with scala ? For a maven scala project we had the help of archtypes. What is the similar technique when I change my build script into gradle so that I can get a readymade project structure to go ahead and add my scala files into the project without adding src folders etc.
You can leverage the build init plugin for this: gradle init --type scala-library
This will create a project in the current directory, setup the Gradle wrapper and generate basic build files.

Gradle - configuration for sibling and interlinked projects

I'm try to configure sibling projects with several dependencies (I'm talking about project dependencies, not artifact dependencies)
So the structure is something like:
A (needs B)
B (needs C)
C
I need to set correctly these dependencies. In particular, I'm using the Eclipse integration, so the goal is to get Gradle -> Refresh all set the correct dependencies among the relative Eclipse projects (however, I believe this question is independent of Eclipse).
Notice that there is only a logical hierarchical layout, not physical (and it will stay that way).
I have read about the use of includeFlat, but probably I'm still missing something.
If we look only at B and C, then I can have them work correctly, by:
In B/settings.gradle : includeFlat 'C'
In B/build.gradle : compile project(':C')
This works fine for B.
Now, A needs B. So what I thought was: I just need to do the same thing between A and B:
In A/settings.gradle : includeFlat 'B'
In A/build.gradle : compile project(':B')
I did expect that when refreshing A, the transitive dependency A -> B -> C would work, but somewhere it breaks and I get:
Project with path ':C' could not be found in project ':B'
What I'm guessing is that B/settings.gradle is not being read. Why is that?
Could someone tell me what I am not understanding?
In a Gradle multi-project build, only one settings.gradle file can be used at a time. Since project B depends on C and project B is part of the multi-project build, you need to include project C in any settings.gradle file that uses project B (assuming you're using project dependencies instead of artifact dependencies).
Note that you can tell Gradle which settings.gradle file to use for each build with the -c command line option, but the build for A is never going to work unless B and all of its project dependencies are included.
Also note that multi-project builds always define a single "root" project, which is defined by where the settings.gradle file is. So if project A contains settings.gradle, it is the "root" project. However, in a flat layout, you can create a special directory named master to represent this and Gradle will automatically find your settings.gradle file there. Read about this here. Note that all of your build commands should typically be run from the "root" project, so if you use the Gradle wrapper, it should live there.
Assuming a flat layout, I would suggest this structure:
A/
build.gradle
src/
main/
java/
B/
build.gradle
src/
main/
java/
C/
build.gradle
src/
main/
java/
master/
build.gradle
gradle/
wrapper/
gradle-wrapper.jar
gradle-wrapper.properties
gradlew
gradlew.bat
settings.gradle
And the contents of settings.gradle would be something like:
["A", "B", "C"].each {
includeFlat it
}

Run "clean" all dependent SBT subprojects

I have an SBT project, specifically a Play Framework 2.1 project, that has a number of subprojects specified in the configuration. The dependencies seem to work fine when compiling, but "clean" only seems to clean the currently selected project, not including its dependencies. Is there any way to clean both the selected project and its dependent subprojects?
If your main project aggregates subjects, like this:
lazy val root = Project("name", file("."))
.aggregate(module1, module2, macros)
then any command called on this root project will be executed for all subprojects. If you call inspect clean command in your sbt session, you'll see, under Related section all subprojects which relates on this clean
On the side note in the comment
aggregate and dependsOn are different command for different purposes. The purpose of aggregation is in running commands called on the root project. In my example by calling test command on my root project, this command will be executed also for module1 module2 and macros. If you want to turn off such behaviour with the following setting:
aggregate in test := false
Aggregated project are independent on the code in them. It's usually used on the root project, nfor example not to call test on each project, but to call it on root. Remeber that in case of aggregation commands will be executed in parallel.
And dependsOn means that your project will depend on the code from other project. And in this case SBT will execute command sequentialy, in order to compile your root project, which dependsOn some modules, it should compile those modules at first step, the the root project.