Hi I have created simple build.bat file which is as below :
#echo off
ant -buildfile build-rj-projects.xml
pause
and my build-rj-projects.xml build file is as below :
<?xml version="1.0"?>
<project name="RJ Build Projects" default="info">
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
</project>
Now when I double click on build.bat file it executes target of build-rj-projects.xml file, but suddenly console disappears ! :( .I have tried putting another target
<sleep seconds="10"/>
in build-rj-projects.xml file but it sleeps for 10 seconds for every target in build-rj-projects.xml file.Any solution ?
ant is usually a batch file. When you invoke a batch file from another batch file, the execution flow is transfered to the called one and does not return to the caller. In your case, that means the pause command is not executed because your batch file ends when ant is invoked. You need to change your calling line to
call ant -buildfile build-rj-projects.xml
using the call command, when the called batch file ends, the execution flow returns to the caller.
Related
I have 3 batch files which consume a common variable-'target'. These batch files need to be executed one after the other. I am using one main batch file where I am trying to call these three batch files. But the execution stops right after first batch job is done. If I execute these batch jobs individually within that main batch file, they gets executed fine without any issue. Not sure what's missing here.
Main batch file (MainBatch.bat) contents:
set target=OHD121
CALL C:\Users\abc\x1.bat
pause
CALL C:\Users\abc\y1.bat
pause
CALL C:\Users\abc\z1.bat
pause
I figured the reason behind the issue. in my called bat files, I have another bat file for which I wasn't using call and that's why the control wasn't returning to original call action to perform subsequent steps.
In my demo project's build event, (a class library project), to copy the build result .dll to a specific folder, (auto-created if it doesn't exist), I added following command line in Post-build event command line section:
xcopy /Y "$(TargetDir)$(TargetFileName)" "$(SolutionDir)DemoApp\bin\$(ConfigurationName)\Packages\"
It works perfectly.
Then I tried to replace that command line with a call to a new batch file called CopyPackage.bat located in $(SolutionDir). The content of the batch file is exactly the command line above:
call $(SolutionDir)CopyPackage.bat
Then I rebuild the project and get following error:
Severity Code Description Project File Line Suppression State
Error The command "call C:\TestProjects\DemoApp\CopyPackage.bat" exited with code 4. DemoApp
Do I miss something?
The solution after getting some hints from you all:
In post-build event command line I put: (see the params)
$(SolutionDir)CopyPackage.bat "$(TargetDir)$(TargetFileName)" "$(SolutionDir)DemoApp\bin\$(ConfigurationName)\Packages\"
In batch file CopyPackage.bat :
set targetfile=%~1
set targetdir=%~2
echo %targetfile%
echo %targetdir%
xcopy /Y %targetfile% %targetdir%
No need to use call you can simply invoke the batch script directly.
I do have to caution you, since the post-build targets have no way of knowing the inputs and outputs of the task, it will always have to execute the script, even if nothing has changed.
Instead, if you convert this to a msbuild target and you implement the input/output signalling correctly, you'll gain a lot of time by being able to leverage the incremental build features of MsBuild.
For example:
<Target Name="CopyOutputs"
Inputs="#(BuiltAssemblies)"
Outputs="#(BuiltAssemblies -> '$(OutputPath)%(Filename)%(Extension)')">
<Copy
SourceFiles="#(BuiltAssemblies)"
DestinationFolder="$(OutputPath)"/>
</Target>
More information on incremental builds and input/output signalling can be found:
MsBuild: transforms
MsBuild How-To: build incrementally
Extend the build process
call is an internal command of cmd.exe you should use
cmd.exe /c "$(SolutionDir)CopyPackage.bat"
instead.
Edit:
The content of the batch file is exactly the command line above
VS variables will not be properly resolved inside of the .bat file. You should pass them as parameters to batch file.
Changing the path in your CopyPackage.bat to absolute path can help resolve this.
Properties like these: $(TargetDir),$(SolutionDir) are recognized by msbuild.exe tool since they are part of msbuild properties and are defined or imported into current environment.
When using xcopy /Y "$(TargetDir)$(TargetFileName)" "$(SolutionDir)DemoApp\bin\$(ConfigurationName)\Packages\" in post-build-event,the msbuild tool can recognize them.So for the first time, it succeeds.
However, for the second time. The msbuild engine can recognize properties in post-build-event, so it calls the .bat successfully. But since the .bat can't recognize the Msbuild property(These properties can only be recognized by MSbuild.exe, not .bat or cmd.exe), the build will fail for not finding the path.
Currently this is how I have written the code in the batch file:
C:\ cd C:\abc\xyz\build-scripts-master
call setEnv.cmd
cmd ant do-clean
cmd ant do-dist
This is not working. It just executes the setEnv and breaks out. It does not run the remaining commands
Manually this is how it works:
I first go to the folder C:\abc\xyz\build-scripts-master through the Command Prompt
Then I type in setEnv, which is a windows command script, and hit return.
Then I type in ant do-clean
And then ant do-dist
I want to automate this process and hence was trying to achieve this using batch file.
Try the following:
#CD /D "C:\abc\xyz\build-scripts-master"
#Call setEnv.cmd
#Call ant.bat do-clean
#Call ant.bat do-dist
The latter two lines assume that ant.bat is located somewhere in the current working directory or %PATH%
It is not imperative that the directory path is doublequoted in this case, just good practice.You could continue not to use the .bat extension with ant. I've included it just to make it clear that it is a batch file, and should be Called in the same way as the setEnv batch file.
it didn't run the bat files because you didn't specify the files' location in the code. As it stands right now, the script expects the .bats to exist in the working directory or it has been placed in the folder. The only way it will run the files arbitrarily is if you had placed your working location in the system variables or set the Path to the folders location. I don't know if the cmd and call are needed. I have never used them in my scripts.
I have a .proj file (build.proj) that I'm calling using a .bat (build.bat) file via the command window.
build.bat:
%SystemRoot%\Microsoft.NET\Framework\v3.5\MSBuild.exe projects\build.proj /fl1 /flp1:LogFile=build.log;Verbosity=Normal
From an elevated command window I run
> build.bat
It runs through build.proj all the way up until it tries to call another .bat file.
Here is the line in build.proj where it calls this .bat file (test.bat):
<Exec Command="call $(BatchDir)\test.bat />
$(BatchDir) is defined at the top of build.proj and equals "..\batch"
In the console output I can see that it is trying to call the batch file:
call ..\batch\test.bat
"The system cannot find the path specified"
test.bat definitely exists relative the the projects folder. What am I doing wrong here?
Directory structure:
/MyProject
build.bat
/projects
build.proj
/batch
test.bat
Try using the full path to test.bat in the $(BatchDir) variable.
It looks like it's taking .. relative to either %SystemRoot%\Microsoft.NET\Framework\v3.5\MSBuild.exe or /MyProject/build.bat, neither of which have a ..\batch directory.
I have installed Jenkins 1.55 exe in windows environment. I have a build script*.bat) which internally calls another (.bat). While executing manually it is working fine but while trying in jenkins only first batch script is executing without triggering the second one and it is giving as success.
Is there any solution for this?
D:\Jenkins\workspace\9.0_TP_Build>cd D:\PortalScripts\9000 portal\
D:\PortalScripts\9xxx portal>WL9.0-TPRefresh-doPortalNightlyBuild2.bat ---> THis is the file i am executing
D:\PortalScripts\9xxx portal>cd d:\PortalScripts\axxx portal
D:\PortalScripts\9xxx portal>call WL-9000-TPRefresh-BuildPortal.bat a.x.x.x axxx axxx --> THis is the second file needs to be executed...but the below echo are from the first batc files...
D:\PortalScripts\9xxx portal>REM #echo off
D:\PortalScripts\9xxx portal>SET HR= 5
If you call a batch file from another, the first one will exit after executing the 2nd one, unless it is called with "call". For example:
call WL9.0-TPRefresh-doPortalNightlyBuild2.bat
instead of just:
WL9.0-TPRefresh-doPortalNightlyBuild2.bat
There is a similar question/answer here, with an example: Batch Closing before end of file?