Command are ignored after ant command in batch file - ant

Following is the bat script code block on which i am working on:
ECHO off
IF NOT EXIST "%ANT_HOME%" (
SET ANT_HOME=%~dp0%build\apache-ant-1.8.2
SET ANT_BIN=%~dp0%build\apache-ant-1.8.2\bin
SET PATH | FIND "%ANT_HOME%;%ANT_BIN%"
)
cd "build\Run"
ant -q
cd ../..
echo "Program Terminated!"
exit
Now, my build.xml file is inside this build\Run folder so that i am first navigating to build/Run before running ant -q command (NOTE: I don't want to change this method of navigating).
The moment ant -q command is executed following things happen:
Set the environment variables as the condition.
Change directory to build\Run.
As my build.xml is inside the Run directory the ant -q command run correctly.
Ant executed correctly and not ant script terminates.
Now my current path will be build\Run ! correct <= Here i don't want this after ant is terminated, instead i want to come out from build\Run that's why i used cd../..
But the problem is I am not able to execute the commands after ant -q. This happens be the program control goes from BATCH => ANT.
Is there any way to execute my command after ant command from bat script itself ?

You may find that the ant being run is a batch file itself, in which case it simply chains to it (no return).
You should try this instead:
call ant -q
Calling a batch file (as opposed to chaining) will correctly return to the point after which you called it.

Related

Run action/script AFTER the binary executed by "bazel run" returns

I'm wondering if there's a way to run an action or script or program after a bazel run command finishes. Running such a script/program before the bazel run is (in a way) possible via --run_under.
Here's an example use-case:
bazel run //path/to/my:target
Running target generates an output file out in it's cwd (somewhere in the bazel .cache directory)
Run a script that uses the output file out
If it's not possible to get run that script afterwards, is there a way to get the cwd of the binary that was just run? Then it would also be possible to access out from "outside" without bazel.
--run_under is the correct approach to this. You can simply run the command in the script after running what is passed as args to your script that you pass to --run_under e.g.
#!/bin/bash
set -euo pipefail
./prefix_command
bash "$#"
./postfix_command

What's the working directory in Jenkins after executing a shell command box?

I'm looking at a Jenkins job and trying to understand it.
I have an Execute shell command box in my Build section:
> mkdir mydir
> cd mydir
>
> svn export --force https://example.com/repo/mydir .
When Jenkins is done executing that command, and moves on to the next build step, what is its working directory?
workspece-root/ or workspace-root/mydir ?
As the next step, I have Invoke top-level Maven targets (still in the Build section).
What I really want to know is: why does that execute successfully?
Is it because Jenkins automatically moves back to the workspace-root/ folder after executing a shell command box, or is it because the next job is a "top-level" job, and Jenkins therefore changes back to the workspace-root/?
Each build step is a separate process that Jenkins spawns off. They don't share anything, neither current directory, nor environment variables set/changed within the build step. Each new build step starts by spawning a new process off the parent process (the one running Jenkins)
It's not that Jenkins "move back" to $WORKSPACE. It's that Jenkins discards the previous session.
I lately saw that if you print the CWD , I would get the Project_NAME.
E.g
D:\jenkins\workspace\My_Project
Any script you might be running wont be found. Hence we can do a "CD path" before we start out scripts.
Slav's explanation is very good and I thought of complementing it by providing a real world example that shows how multiple Windows batch commands look like even if they work in the same directory:
Command 1
REM #ensures that all npm packages are downloaded
cd "%WORKSPACE%"
npm install
Command 2
REM #performs a prod-mode build of the project
cd "%WORKSPACE%"
ng build --prod --aot=true -environment=pp
So, each one ensure that current working directory points to the current project directory.

Executing custom shell script to build in jenkins

I have setup a jenkins job to build my project. I have a jake.sh file in my project and the code is pulled from github. I want "npm install" command to be executed and then jake.sh to be executed once the the code is checked out.
How can I configure this in jenkins? I have tried givin ./jake.sh and jake.sh in Build->Execute Shell section
According what you tell I think the problem can be
The script is not marked as a executable. In this case add in Build -> Execute Shell (in case you have linux) sudo chmod 777 path_to_script/jake.sh.
The script is not in the base directory. Remembeber that when you execute a bash script, the current directory is /path_to_job/workspace. So you have first to move to the script folder (cd path_to_script) or specify the path when running it: ./path_to_script/jake.sh.
I hope this solves your problem.
A workaround for shell scripts can be to run the script as
bash ./jake.sh
instead of
./jake.sh
Then you don't have to do chmod. Useful when you wipe the workspace before every build.
In the same manner, if you have a nodejs shell script or python script, you can run node myscript.js / python myscript.py.

How can I run ANT from a batch file without having an Environment Variable set?

I want to include ANT, the JavaSDK and FlexSDK into my project directory. I need people in my company to be able to compile from source code.
I have a build.bat file which starts with :
ant blah/blah/blah
But what if I want to run ANT directly without the system environment variable? If I remove these from the system ant is not recognised.
I am a real newbie here, but I want to do something like this:
start "${basedir}\libs\ant\bin" ant -lib ${basedir}/libs/ant/lib/flexTasks.jar
pause
This is so that other people dont need to install a whole load of software... IT would all be contained in the folder.
Just set those environment variables in the same batchfile, i.e. something like :
set ANT_HOME=C:\ant182
set ANT_ARGS=-lib C:\antxtralibs
set JAVA_HOME=C:\jdk160_30
set PATH=%PATH%;%JAVA_HOME%\bin;%ANT_HOME%\bin
:: default
ant -f %1
:: debug
:: ant -debug -f %1

Is executing a sequence of Commands possible using ANT Exec task

In ANT Exec task. I need to cd into a folder (Windows OS) (eg: D:\Testrun) and execute a set of commands in a sequence which will be passed as parameters to the ant script. Is this possible ? Could anyone quote a sample example ? Could the results of the command execution be logged in a file ?
Could you wrap up the sequence of commands into a script (e.g. a .bat script)? You could pass parameters to the script from your ant task. You could direct the output of the script to a file using the output attribute of the exec task.

Resources