I am building an API using spring boot. I'm using gradle and a multi-project build set up where I have a services-lib project and an api project that depends on the services-lib.
Running the api:bootRun tasks in the api project works perfectly fine, but now I'm trying to add the ability to trigger an spring-boot-devtools automatic restart, which requires the bootRun task to have the service-lib classdir in it's classpath(not the jar that is added by the multi-project dependency).
Adding this to my api's build.gradle does trigger the automatic restart when I run the api:build task (where "C:/foo/bar" is the absolute path to my multi-project root directory).
bootRun {
classpath += files('C:/foo/bar/services-lib/build/classes/java/main')
}
My question is, instead of having to hard code that path, can I set it using something like project(':services-lib')?
Well I did figure this out thanks to Kidus' suggestion.
bootRun {
bootRun.systemProperty 'spring.profiles.active', 'dev'
classpath += files('../services-lib/build/classes/java/main')
}
It still means if I change anything about the build output in the services-lib project I have to change it hear but at least now when others check out the project now it will work for them.
Related
I am a Java newbie here. I am thinking of a way to remove all the project dependencies.
For example in nodejs, we can simply remove the node_module folder and then do an npm install.
I am using Gradle and IntelliJ IDEA
How can I do the following:
Remove the dependencies from the project
Re-add the dependency again
Lastly where do I see all the dependencies along with its version number in IntelliJ
To remove dependencies from the project, simply go to your
build.gradle file and remove all the lines containing
"implementation", "complie" or anything similar.
However if you just want to remove them from cache and redownload
them simply enter this command in the terminal:
gradle clean build
or in the gradle menu which you can find in the IntelliJ Sidebar
press "Reload all gradle projects"
To see the version of the dependencies you can either use
"Dependencies" new feature of IntelliJ, or find them in the
build.gradle file.
more info here
You can Remove the already installed dependencies by using clean command
gradle clean build
The clean task is defined by the java plugin and it simply removes the buildDir folder, thus cleaning everything including leftovers from previous builds which are no longer relevant. Not doing so may result in an unclean build which may be broken due to build artifacts produced by previous builds.
As an example assume that your build contains several tests that were failed and you decided that these are obsolete thus needs to be removed. Without cleaning the test results (using cleanTest task) or the build entirely (by running the clean task) you'll get stuck with the failed tests results which will cause your build to fail. Similar side effects can happen also with resources/classes removed from the sources but remained in the build folder that was not cleaned.
This will remove the old dependencies and build them back together once again .
and about the dependencies , to check Without modules:
gradle dependencies
For android :
gradle app:dependencies
Note: Replace app with the project module name
Here is a possible solution , Another one would be to create a gradle task inside build.gradle:
subprojects {
task listAllDependencies(type: DependencyReportTask) {}
}
Then call the task with
gradle listAllDependencies
and one final simple solution is using Project report plugin
Add this to your build.gradle
apply plugin: 'project-report'
And Then generate a HTML report using:
gradle htmlDependencyReport
And here is IntelliJ IDEA specific way .
Hope i helped .
Every time I run this command: gradle build it produces a my-program-1.0.0-SNAPSHOT.jar. Where do I control this? In the gradle.properties I have only the version nothing else. Even if I run gradle -Dversion=1.0.0 build it still creates a jar with SNAPSHOT in the name. How do I create a jar like this: my-program-1.0.0.jar?
I'm using spring boot.
Basically we have to change the project version in the gradle, To do so we have to use project api in gradle. you can refer https://docs.gradle.org/current/dsl/org.gradle.api.Project.html
One Solution I can suggest is:
Add version '1.0' in your build.gradle
I found gradle tasks shown below in my Eclipse Java Spring project:
application
bootRun
build
assemble
bootBuildImage
...
build setup
documentation
help
ide
verification
What these tasks are coming from? Is it part of Gradle or Spring?
(1) You ask
What these tasks are coming from?
Gradle task come from 2 area: by Gradle itself and by Gradle plug-ins.
For example, at folder where has file build.gradle , (1a) when you run command
gradle bootRun
Spring Boot application when run, it usually run and return result at http://localhost:8080 (default). gradle bootRun run success by an plugin called Spring-boot-gradle-plugin declared inside build.gradle .
(1b) When you run
gradle wrapper
your project will add some folder and files, make your source code become portable, no need install Gradle at local PC when you share for your colleagues. gradle wrapper come from Gradle itself, not from third-party one.
(2) You ask
Is it part of Gradle or Spring?
(1b) from Gradle itself, (1a) is a plug-in made by Spring team.
Reference: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin
They look to be gradle as assemble, build, buildDependencies are all standard irrespective of what you are building.
I'm a new Gradle user coming from Maven and I've hit a bit of a roadblock in our CI builds due to the issue with the working directory in a multi-module build. Specifically it's the unit tests, as I have a few unit tests that are loading resources that are relative to the sub-project directory.
If I build the projects individually, everything works as expected. If I build them using the master build.gradle file, then I run into issues with files not being found, etc.
So the question is,can I change the working directory when gradle forks a new Java process to build the sub-module?
TIA
I've the similar problem: shared config directory in root for Gradle muli-module, migrated from Maven. Manipulation with projectDir (https://discuss.gradle.org/t/how-do-i-set-a-common-build-directory-for-multi-module-builds-in-the-root-project-directory/5570, https://docs.gradle.org/current/userguide/fine_tuning_project_layout.html#sub:modifying_element_of_the_project_tree) is restricted in last Gradle version, so I've moved to defining root directory location environment variable or fixed place (e.g. ~ or \..\..) or JVM parameter with this location.
I've got a module of an android app with all the logic and pojos so it can be tested quickly, the thing is that when I test that module Gradle spends most of the time configuring the 5 projects i have in the root application and are not related to the code i'm going to test.
Can I avoid that ?
I'm using: "gradle :core:test" command
You can try setting the "Configure on demand" flag to true in your gradle.properties file
org.gradle.configureondemand=true
This will ensure that when you build your module, only it's dependencies and itself are configured during the configuration phase.
Additionally you can also use the -a switch to execute only the module's tasks ignoring building its dependencies, if this is relevant.
gradle -a :core:test