Running gradle commands inside a task - gradle

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.

Related

Gradle with multiple subproject running tests

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.

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.

gradle Lint plugin defined task for include or exclude custom files

I am using gradle lint plugin in my Android project on CI Server. But sometimes I want to run android lint only on custom files, just like gradle Copy Task include and exclude to defined task scope in my gradle task.
In Android Studio, inspection can defined these behavior. But in my case, it is gradle task run on CI Server without UI interaction.
I don't think the Gradle Plugin is capeable of that. However you can also run lint on it's own.
lint [flags] <project directory>
You can get all of the options with:
lint --help
The executable is located in $ANDROID_HOME/tools/
More information here.

gradle Difference between jar and assemble task

what is the difference between assemble and jar task of java plugin in gradle?
I tried executing them with a sample Hello world project they both seems to do the same job.
Since jar is a single task, which assembles a jar-archive for current project, assemble is, according to documentation:
assemble All archive tasks in the project, including jar. Some plugins add additional archive tasks to the project. Task Assembles all the archives in the project.
It is build-cycle task, which execute all the task of this build-cycle phase. Like a check task, which runs all test and verification task, assemble Runs all task, which essemble some artifacts. And the 'jar' could be not the only such a task in the project, some plugins could add them too.

Resources