I need to set some 10 environment variables before my CMAKE can run. This is what I am doing
Top of my build.xml has the following
<property environment="env" />
Creating a batch file with all the environment variables. I have something like this in my ANT Build.xml
Calling runCompile
I then try to call CMAKE
Problems i am facing are:
I am unable to get any of the environment variables ex: when i try to print ${env.CMAKE_COMMAND} it prints ${env.CMAKE_COMMAND} and not value
When i try to goto working directory and execute the batch file, this also does not work.
Any idea how I can achieve the above?
Sorry I am unable to paste the XML code here. Hence attached as image.
Looking at the exec task documentation you will find a hint about the nested element env. With this it should be possible to specify environment variables to pass to the system command, like shown in the example:
<exec executable="emacs">
<env key="DISPLAY" value=":1.0"/>
</exec>
Related
I am invoking a windows batch command from Jenkins, after i get the latest version of my project from SVN. the windows batch command just performs certain file copying, after the all the files are retrieved from SVN and runs an ANT build. In the ANT build process, i am generating a JSP file where i have tried to capture the in the following fashion.
%BUILD_TAG%-%BUILD_NUMBER%-%BUILD_ID%-%SVN_REVISION%
Unfortunately none of this information is understood by the build process and it just writes %BUILD_TAG%-%BUILD_NUMBER%-%BUILD_ID%-%SVN_REVISION% into the file.
Could you please let me know if there is a way to capture these information into a file in the way i am trying to do? if not, could you direct me on how these information could be captured into a JSP file during the process that we are following?
BUILD_TAG, SVN_REVISION, etc are all environment variables that are present during a Jenkins build, and to use them in Ant, you would use them as any other environment variable from Ant
First, add a line:
<property environment="env"/>
Then you can reference any environment variable with this prefix, like:
${env.VAR_NAME}
So in your case, you'd do:
${env.BUILD_TAG}-${env.BUILD_NUMBER}-${env.BUILD_ID}-${env.SVN_REVISION}
I am trying to run a ear task with ant on Jenkins. The ant task builds successfully on my local machine.
However, when I try to build using Jenkins, I get the following error: "specified workspace is not valid". I tried hardcoding the workspace value, I tried getting it from the environment variables and I also tried to define it in a custom workspace and then accessing it.
But I keep getting the following error:
[subProjEAR] $ cmd.exe /C '"F:\Build\Ant\apache-ant-1.8.4\bin\ant.bat && exit %%ERRORLEVEL%%"'
Buildfile: E:\Jenkins\jobs\ProjectDev\workspace\subProjEAR\build.xml
init.env:
init.typedefs:
Trying to override old definition of task apt
init:
BUILD FAILED
E:\Jenkins\jobs\ProjDev\workspace\subProjEAR\build.xml:81: Specified workspace directory "E:\Jenkins\jobs\ProjDev\workspace" is not valid.
Total time: 0 seconds
Build step 'Invoke Ant' marked build as failure`
The code snipped around line 81 is as below
<target name="init" depends="init.env,init.typedefs" unless="init.executed">
<property name="init.executed" value="true" />
<fail unless="workspace" message="The workspace property needs to be set!" />
<dirname property="project.dir" file="${ant.file}" />
<property name="echo.metadata" value="false" />
<!-- Line below is no 81 -->
<mdimport workspace="${workspace}" pjdir="${project.dir}" echo="${echo.metadata}">
</mdimport>
<property name="archive.name" value="${project.name}.ear"/>
<property name="uri" value=""/>
</target>
I have created an environment variable named as WORKSPACE in my "local machine". Echoing prints it correctly ( C:\bea\user_projects\w4WP_workspaces\myProjWS). I have also set the WORKSPACE environment variable on the Jenkins machine. This is the same workspace as Jenkins. Echo on Jenkins prints the below:
Buildfile: E:\Jenkins\jobs\ProjectDev\workspace\subProjEAR\build.xml
init.env:
init.typedefs:
Trying to override old definition of task apt
init:
[echo] workspace : E:\Jenkins\jobs\ProjectDev\workspace
[echo] weblogic workspace : ${env.WORKSHOP_WORKSPACE}
BUILD FAILED
E:\Jenkins\jobs\ProjectDev\workspace\subProjEAR\build.xml:85: Specified workspace directory "E:\Jenkins\jobs\ProjectDev\workspace" is not valid.
Please help.
Best way to access WORKSPACE from ANT is through:
<property environment="env" />
<echo message="${env.WORKSPACE}" />
It doesn't seem like a Jenkins error, as Jenkins is able to successfully load the build file from that workspace. What is on line: 81 of that build.xml? The cause of the problem is there. Paste a snippet of your build file around line: 81
Edit after author comments
So, as I guessed, it wasn't anything to do with Jenkins. The issue is with <mdimport> task. So far, I've been able to find zero documentation about it, other that it comes from Weblogic
It is this task that is throwing the error. And considering that Weblogic and Jenkins have little to do with each other, I doubt you should be passing Jenkins's workspace for this task. It is probably looking for some Weblogic workspace.
Do you have a Weblogic workspace checked out under a subfolder in Jenkins, like my_checkout_folder? If so, maybe you should use ${workspace}/my_checkout_folder for that task?
In this issue from someone, they are reading ${workspace}/workspace.xml file
In this example (which looks very similar to yours), the value of workspace comes from environment variable WORKSHOP_WORKSPACE. Do you have that environment variable set on your local machine (the one where it works)?
Probable cause
It just came to me, while reviewing your question.
You said it works on local machine, but not on Jenkins.
If you are using the same ant file on both, means you are expecting an environment variable workspace to be present on your local machine, right? (Otherwise the same ant script wouldn't have worked on local)
Jenkins, when executing a build, has its own environment variable workspace which has nothing to do with the one you might be having on your local machine. Jenkins's workspace variable defines its job's working directory.
Even if you manually set a global workspace environment variable on Jenkins machine, it will be overwritten by the job's workspace everytime it runs.
Do the following test:
echo workspace environment variable on your local machine
in Jenkins job, use "execute shell" (or "execute windows batch command") to echo workspace from within the job.
Paste the result here. But you can also compare them yourself.
i am trying to use the %teamcity.build.checkoutDir% variable in my ant script to build a custom path to a file which gets passed as parameter to another program called via ant exec job.
<exec executable="${props.tools.vbpUpdater}" failonerror="true" >
<arg line="-c${vbpConfigLocal} ${teamcity.agent.checkoutDir}/${vbpFile}" />
</exec>
The format of the path seems to be off.
What i get is :
C:\TeamCity\buildAgent
What i expected is something like this
C:\\TeamCity\\buildAgent or C:/TeamCity/buildAgent
If i add a custom property to my build agent with the expected format (i did this for testing purposes only) everything works fine in my ant script.
best regards
You can use this instructions and create a build step (it should be the first one in queue) which would replace '/' with the values you need. And in further build step use this modified value.
I've done some similar things when integrating mercurial build number into build version
I've inherited some code from a previous developer, which gets built using Ant into an executable jar file that runs by double clicking. The application runs, but under some conditions crashes with an OutOfMemoryError. To investigated this, I'd like to add the -XX:+HeapDumpOnOutOfMemory jvm arg to the Ant buildfile, and as I understand it, the <jvmarg value="-XX:+HeapDumpOnOutOfMemory" /> element needs to go under a <java ...> task. However, there is no <java ...> task to be found in this or any other Ant buildfiles in this code base.
How is this even possible? How can the jar file be executable without a <java ...> task?
I'm asking primarily to find out what in fact makes my jar file executable so that I can figure out where to put that <jvmarg /> element to debug the OOME.
Thanks!
A <java> task doesn't create an executable jar file. It executes a Java program.
I don't think it's possible to specify VM parameters when starting an executable jar file by double-clicking on it. If you want to pass VM parameters, open a command prompt and execute the jar this way:
java -XX:+HeapDumpOnOutOfMemory -jar nameOfTheJar.jar
Your jar is executable because it has a Main-Class defined in the META-INF/MANIFEST.MF file. Double clicking it to run doesn't do anything with Ant. Ant is simply used to package the jar.
In order to add the parameter and still launch via a double click you could create a shortcut that runs the command in JB Nizet's answer
I'm testing a Zend Framework application using PHPUnit and Jenkins. I need to override the APPLICATION_ENV environment variable which is access using PHP's getenv in the PHPUnit bootstrap.php file:
<?php
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
... use APPLICATION_ENV to pick database, configuration, etc. ...
I have two environments: testing (for local machines) and testing-ci (for Jenkins machine). How can I set the variable to testing-ci when it runs in Jenkins? Is there any way to set it in build.xml for Ant or Phing?
Step 1: Add the environment variables to Jenkins.
Open either the global or project-specific configuration page depending on your needs and scan down for the Environment variables section. Check the checkbox and use the Add button to add key/value pairs.
These will be passed by Jenkins to your Ant build script.
Step 2: Load them into Ant.
Near the top of your Ant build.xml script, load all environment variables with an env prefix so they don't interfere with other properties.
<property environment="env"/>
Now all imported variables will be available using the env prefix, e.g. ${env.HOME}.
Step 3: Pass them to PHPUnit.
Assuming you're using the <exec> task to run PHPUnit, you can pass each needed variable to it using the <env> child element.
<exec taskname="test" executable="phpunit">
<env key="APPLICATION_ENV" value="${env.APPLICATION_ENV}"/>
...
</exec>
Note: You might want to try just the first step to see if Ant will pass the environment variables along to executed child processes, but I think the other two steps are good for making it clear what is required to other developers.
OK.
Here's what you do...
First, create a new file called bootstrap.php.
Next, in boostrap.php, put the following code:
if (!empty($argv) &&
($key = array_search('--environment', $argv)) !== FALSE)
{
$env = $argv[$key + 1];
putenv('APPLICATION_ENV=' . $env);
}
Load the bootstrap.php into your testsuite or (even better) phpunit.xml.
Finally, via your CI build config, or via the console or wherever, execute your unit tests like phpunit UnitTest.php --environment dev.
You're good to go.