NITISH DASTANE
NITISH DASTANE

Reputation: 101

Run gradle commands from other directory

I have folder "A" and folder "B"
Folder "B" is having gradle code I want to run gradle clean and gradle build command from folder "A" of folder "B"
How do I do this?

Upvotes: 7

Views: 9604

Answers (4)

Chuss
Chuss

Reputation: 142

for me this works

gradle clean build --console=plain -p ${projectPath}

Where: clean: Is to "clean" build/libs folder. Console plain: To show only build results in console

// Compilando
sh(label: 'Compilando', script: "gradle clean build --console=plain -p ${rutaTemp}")
sh(label: 'Listando archivos', script: "ls -lart ${rutaTemp}")

Upvotes: 1

KANJICODER
KANJICODER

Reputation: 3885

This is harder than it looks. Dispite setting different values for --project-dir , --gradle-user-home , --build-file No matter what you do, when you "println project.projectDir" from your build.gradle script it will ALWAYS report back the directory in which "build.gradle" lives.

I wanted to re-arrange things in gradle because gradle pollutes your root directory with a lot of junk! Uncle Bob of "Clean Code" (Robert C. Martin) would probably refer to this behavior as "rude code".

I finally figured it out after searching around all day. Here is my project structure:

<root_folder>
 |
 +--[ .git       ]
 +--[ .gitignore ]
 |
 +--[-]src/main/java
 |   |
 |   +--Main.java
 |
 +--[-]RUN_ON_CMD
     |
     +--[-]Gradle
         +--[ build.gradle ]
         +--[ RUN.sh ]
         |
         +--[-]GENERATED

.gitignore :

GENERATED/

build.gradle :

apply plugin: 'java'                  
apply plugin: 'application'    

println "[project.projectDir]:"
println project.projectDir
mainClassName = 'Main'

sourceSets {
    main {
        java {
            //:Because "build.gradle" lives in:
            //:<root>\RUN_ON_CMD\Gradle\GENERATED\
            srcDir '../../../src/main/java'
        }
    }
}

RUN.sh

build_gradle=$( realpath build.gradle )
echo $build_gradle

current_directory=$( realpath "." )
echo $current_directory

generated=${current_directory}/"GENERATED"
echo $generated

cp $build_gradle $generated/"build.gradle"
gradle run -b $generated/"build.gradle" -g $generated --no-daemon

main.java

public class 
Main{
    public static void 
    main( 
        String[] args
    ){

        System.out.println("[MAIN]");

    }
}

To Run:

Do a "git bash here" inside the "Gradle" folder. Then type:

./RUN.sh

And hit ENTER


My output: (TDD_JAVA == root_folder )

JMIM@DESKTOP-JUDCNDL MINGW64 /c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle (master)
$ ./RUN.sh
/c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle/build.gradle
/c/DEV/REPO/GIT/TDD_JAVA
/c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle
/c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle/GENERATED
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/5.4.1/userguide/gradle_daemon.html.
Daemon will be stopped at the end of the build stopping after processing

> Configure project :
[project.projectDir]:
C:\DEV\REPO\GIT\TDD_JAVA\RUN_ON_CMD\Gradle\GENERATED

> Task :run
[MAIN]

BUILD SUCCESSFUL in 8s
2 actionable tasks: 1 executed, 1 up-to-date

All the junk generated by gradle is put into the "GENERATED" folder. Then my .gitignore makes sure not to commit any of that junk.

Upvotes: 0

M.Ricciuti
M.Ricciuti

Reputation: 12116

You should use the "start directory" parameter (-p, --project-dir : see Environment options)

I think the other available parameter -b --build-file could work as well, but its main usage is when your build script filename differs from default build.gradle.

Upvotes: 14

Yu Jiaao
Yu Jiaao

Reputation: 4714

Use the -b parameter(i.e. --build-file)

cd A
gradle -b ../B/build.gradle 

Upvotes: 1

Related Questions