Gradle with multiple subproject running tests - gradle

Let's assume that I have such structure of a gradle project:
:app:api
:app:domain
:app:infra
:app:libs:lib1
:app:libs:lib2
Infra depends on lib1 and lib2.
As far as I know, if you run task test for infra, gradle by default runs all test tasks for all the dependent projects, which are:
:app:libs:lib1
:app:libs:lib2
How can I disable this functionality? I want to run gradlew app:infra:test, I want to run tests just in infra and not to run tests in lib1 and lib2?
If I run gradlew test, then I want to run all the tests.

Related

Difference between gradle build and gradle bootJar?

What is the difference between "gradle build" and "gradle bootJar"? Why would I use bootJar if I can still create the artifact using build?
build is a lifecycle task contributed by the Base Plugin. It is
Intended to build everything, including running all tests, producing the production artifacts and generating documentation. You will probably rarely attach concrete tasks directly to build as assemble and check are typically more appropriate.
bootJar on the other hand is a specific task added by Spring Boot Gradle plugin that, when the java plugin is present, attaches itself to the assemble lifecycle task.
The assemble task is automatically configured to depend upon the bootJar task so running assemble (or build) will also run the bootJar task.
(Packaging Executable Jars)
You want to use bootJar if you're only interested in building the executable jar and not interested in executing tests, code coverage, static code analysis or whatever is attached to the check lifecycle task.

How to skip test task when running sonarqube task?

When I execute sonarqube task, by default test task runs. How can I configure Gradle to not run tests when I specifically run sonarqube.
I want to configure this build.gradle.kts.
When I run
./gradlew sonarqube -x test
then tests are not executed.
I want to achieve the same by configuring in the build.gradle.kts file.

How to configure build step for maven goal in teamcity to run unit test

I use junit in my java project (developed using intellij idea) for unit test, and I want to configure build step in team city to run my unit tests only. I also use maven to build my project. It works when I set goals for maven to "clean compile" but I dont know how to configure build step to run unit tests.
Also in command line. when i run "maven test" it runs unit tests correctly and shows the failures.
I can't comment on TeamCity, but I'll try to help anyway out of my maven knowledge.
So first of all mvn clean compile will never run your unit tests, because maven has a concept of lifecycle and testing phase is coming after compile phase.
When you run mvn test it will run all the face up to (including) the test phase so the unit tests will run as a part of maven default lifecycle.
Now you're asking about a "build step for running unit tests" from which I conclude that you need a separate step. In maven phase is nothing more than running a series of plugins. In maven plugin has goals, so you can run a plugin responsible for running unit tests directly.
In maven this plugin is called "surefire" and a goal is called "test", so you can run:
mvn surefire:test
Given the classes (production code and tests) are compiled, you will see that this only runs your unit tests. So this probably has to be configured in Team City.

Running gradle commands inside a task

Is there any way by which i can run gradle clean, gradle compileand gradle publish commands inside a gradle task in build.gradle on a windows pc
You can use taskX.dependsOn taskY where taskX is the task you will be running (in this case, build) and where taskY is the task that must be run before the current task. You could also use taskY.shouldRunAfter taskX for a task that should run after the current task. Note that these statement shouldn't be called from within the task.
build.dependsOn publish will run the publish task before running the build task.
I suggest you read through Ordering Tasks in the gradle doc.
If you really need to call a task from another task, you can, but there are better options - such as those above. Using tasks.taskX.execute() will do what you're looking for. Where taskX is the task you want to execute. This can be called from within a task.
To address your comment.
This code will run the tasks: clean, compile and publish before build is executed:
build.dependsOn clean
build.dependsOn compile
build.dependsOn publish
This code will run the tasks: clean, compile and publish after build has been executed:
build.shouldRunAfter clean
build.shouldRunAfter compile
build.shouldRunAfter publish
This code will run the task: clean before build is executed and run: compile and publish after build has been executed:
build.dependsOn clean
build.shouldRunAfter compile
build.shouldRunAfter publish
This is probably the code you're looking for. Add the configuration which best suits your needs before the build task has been declared. Then when executing build.gradle, execute the task build from the command line.

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).

Resources