How does Gradle find a setings.gradle file - gradle

In a normal project, I can have a structure like this:
myProject/
build.gradle
gradle.properties
**settings.gradle**
but if I have another project
myProject2/
build.gradle
gradle.properties
it works fine.
Things go strange when myProject2 is a subfolder (but not sub-project) of myProject.
myProject/
myProject2/
build.gradle
gradle.properties
build.gradle
gradle.properties
settings.gradle
How does gradle find what settings file to use?

The settings.gradle file has the next 2 main purposes:
Add libraries to your build script classpath
Define which projects are taking part in the multi-project build
Therefore it is optional for a single-project build but due to #2 it is mandatory for multi-project builds.
By default it is assumed that the location of the settings file is also the location of the root project but you can redefine the location of the root project in the settings file.
Now, gradle logic for locating settings.gradle file as documented in gradle user guide here:
If you trigger a multiproject build from a directory with a settings
file, things are easy [Amnon - you just found it]...
If you execute Gradle from within a project with no settings.gradle file, Gradle looks for a settings.gradle file in the following way:
It looks in a directory called master which has the same nesting level
as the current dir.
If not found yet, it searches parent directories.
If not found yet, the build is executed as a single project build. If
a settings.gradle file is found, Gradle checks if the current project
is part of the multiproject hierarchy defined in the found
settings.gradle file.
If not, the build is executed as a single
project build. Otherwise a multiproject build is executed. Otherwise
(you executed Gradle from
(Gradle user guide also provides the purpose of this behavior here).
Now, back to your case, the first two project layouts you provided are for a single project build so settings.gradle is optional. For your third project layout if you'll run gradle from the root project (myProject) then it will find settings.gradle next to it but if you'll run it from myProject2 folder then since this project parent path contains a settings.gradle file, gradle will find it and use it.

Related

How do I remove gradle support from an intellij project?

I have a scala/sbt project, and there is a build.gradle file from when I was experimenting one time. Intellij saw the build.gradle and has enabled gradle for the project. Can I turn off gradle for the project?
I have deleted my build.gradle and any gradle directories in my project but I keep getting messsages like the below, so I'm guessing there is a setting in intellij I need to turn off.
7:31 pm The IDE modules below were removed by the Gradle import:
knot-alpha
You can open dialog to select the ones you need to restore.
Close the project
Remove all Gradle related files and directories:
build.gradle
settings.gradle
gradle.properties
gradle/
Delete the hidden .idea/ directory
Reimport project
In addition to Fracisco Mateos answer:
It should be enough to remove the gradle.xml from the hidden .idea directory. This will remove the Gradle "nature" of the project in IntelliJ without having to delete all your workspace metadata.
Removing the build.gradle, settings.gradle, gradle.properties and gradle directory probably isn't necessary, but, of course, makes sense in order to have a clean project.

Gradle - Use copy task to copy a file from outside project

I have two separate gradle projects in the following structure. I want to write a task to move the build jar from projectA to projectB's lib folder.
projectA -> c:/User/someUser/dev/projectA
projectB -> c:/User/someUser/dev/projectB
In projectA's build script, I have the following to move the generated jar from projectA's build/lib folder to projectB's lib folder.
task deployJar(type: Copy) {
from('build/libs/')
into('C:/Users/someUser/dev/projectB/libs')
include('*.jar')
}
I'm using absolute path but it is not working. I was hoping if someone can point out what I am doing wrong.
Thanks
Have you considered setting up the projects as a multi-project? You could hook them together with includeFlat. What you would usually do is have projectA deploy to an artifact repository and resolve the jar in projectB through dependencies {} or set projectA/projectB up as a multi-project and have projectB depend on the output of projectA (also through dependencies {}). If you go the second route, you get nice things like projectA automatically rebuilding when necessary when you build projectB.
I don't see anything exactly wrong with what you have already. Does it fail with an error?
Keep in mind that setting up your build with absolute paths will make it non-portable/fragile.

Gradle build not invoking JUnit

My gradle script for building an EAR is not running any JUnit tests I have. The command I used for invoking the script is
gradle build
I also tried
gradle test
and that also did not work.
My build structure is as follows
settings.gradle
gradle.properties
EARProject
build.gradle
META-INF
application.xml
lib
JARProject
build.gradle
src/main/java
....
src/test/java
....
WebProject
build.gradle
src/test/java
src/main/java
WebContent
.....
I am invoking the gradle command from EARPRoject directory.
What changes I need to do in order to run the test cases. Please note that if I run the gradle build command from individual projects it is working as expected.
Regards
-Albin
Apparently, EARProject doesn't have any tests. Depending on what you want, run gradle build from the top directory, or from a subproject/subdirectory containing tests. Alternatively, you can run gradle buildNeeded from EARProject, which will perform a full build of all projects that EARProject depends upon (which presumably includes JARProject and WebProject).

How to convert a Maven build to Gradle?

I know I should be working with my build.gradle and init.gradle files but I don't know what to write or how to point to my project folder with the pom.xml file.
The first step is to run gradle init in the directory containing the (master) POM. This will convert the Maven build to a Gradle build, generating a settings.gradle file and one or more build.gradle files. For simpler Maven builds, this is all you need to do. For more complex Maven builds, it may be necessary to manually add functionality on the Gradle side that couldn't be converted automatically.
https://guides.gradle.org/migrating-from-maven/
as Peter Niederwieser said:
For more complex Maven builds, it may be necessary to manually add
functionality on the Gradle side that couldn't be converted
automatically.
although you have to write some parts manually by your self. there is an online service that may be an useful tool For complex Maven builds. maven2gradle is a project on github which can convert online dependencies element automatically from maven to gradle scripts.
for using it,
get to maven2gradle . URL
open and select contains of your maven file.
Paste your maven dependencies on the text box in that web page (with
or without the dependencies root element).
click Convert button.
for more information http://sagioto.github.io/maven2gradle/

Specify project version

I have found 3 ways to define my project version.
In the documentation http://www.gradle.org/docs/current/userguide/dependency_management.html they talk to specify it in the manifest or the foldername (which already contains the project name).
Coming from a maven project I'm used to defining my version in my pom.xml and I have found project that also define their version in the gradle.build file in the version property
I'm looking for the correct way to handle my project version, so I can also depend on a certain version of my project.
The link that you have shared talks more about dependency management, and about good practises for versioning your artifacts.
There is a one-to-one relationship between a Project and a build.gradle file.
Also your build.gradle gives you a property:
version - The version of this project. Gradle always uses the toString() value of the version. The version defaults to unspecified.
This fits in for the project version.
You can set it directly in build.gradle, but depending on your use case you could pass it externally - using gradle.properties for example in multi-project builds.
You can also directly add properties to your project objects using properties files. You can place a gradle.properties file in the Gradle user home directory (defaults to USER_HOME/.gradle) or in your project directory. For multi-project builds you can place gradle.properties files in any subproject directory. The properties of the gradle.properties can be accessed via the project object. The properties file in the user's home directory has precedence over property files in the project directories.
Check for more details: http://www.gradle.org/docs/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:configurations%28groovy.lang.Closure%29
Add a gradle.properties file in the root directory of your project (same level as build.gradle) with this content:
version=0.0.1-SNAPSHOT

Resources