i want to run few targets of ANT build in parallel. here is the code i tried
<project name="cis" default="release">
<property name="Run_excecuted" value="false"/>
<target name="run_main">
<sequential>
<parallel>
<antcall target="dashboard" />
<antcall target="remTraces" />
<param name="Run_excecuted" value="true"/>
</parallel>
</sequential>
</target>
I have set a property Run_excecuted in the build and have added the condition unless="Run_excecuted" in the required targets
<target name="dashboard" depends="prepare" unless="Run_excecuted">
<target name="remTraces" depends="prepare" unless="Run_excecuted">
what is happening is the antcalls are getting invoked in separate instances parallely and the dependencies are getting calculated again. Because of this target "prepare" is running multiple times. i dont want this to happen.How can i do this? property Run_excecuted being set is not helping.
Related
I have junit tests that use selenium to test web server.
When i run the tests using ant from command line, everything is working fine, browser gets opened and tests are going as planed.Browser gets open and i can see tests running.
Recently ive tried to add automatic tests as part of Ci cycle running on jenkins.
I run it as ant build command.
I can see that ant is executing properly (test classes are built i can see output from tests to console) but browser window never gets opened and test fails because of it. here is my ant file
<?xml version="1.0"?>
<project name="JUNIT" default="main" basedir="../../project" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="api.dir" location="src/java" />
<property name="build.api.dir" location="target/classes" />
<property name="test.dir" location="src/test/java" />
<property name="build.test.dir" location="target" />
<!-- Variables used for JUnit testin -->
<property name="test.report.dir" location="testreport" />
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="api.class.path">
<pathelement location="${build.api.dir}" />
</path>
<artifact:dependencies cacheDependencyRefs="true" pathId="pomdeps.path">
<pom file="pom.xml"/>
</artifact:dependencies>
<target name="clean">
<delete dir="${test.report.dir}" />
<delete dir="${build.api.dir}" />
<delete dir="${build.test.dir}" />
</target> <!-- Creates the build, docs and dist directory-->
<target name="makedir">
<echo message="Make dir"/>
<mkdir dir="${build.test.dir}" />
<mkdir dir="${build.api.dir}" />
<mkdir dir="${test.report.dir}" />
</target> <!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<echo message="Compile"/>
<javac srcdir="${api.dir}" destdir="${build.api.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" />
<classpath refid="libs.class.path" /-->
<classpath refid="pomdeps.path" />
</javac>
<javac srcdir="${test.dir}" destdir="${build.test.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" /-->
<classpath refid="api.class.path" />
<classpath refid="pomdeps.path" />
</javac>
</target>
<!-- Run the JUnit Tests --> <!-- Output is XML, could also be plain-->
<echo message="Classes folder ${build.test.dir}"/>
<target name="junit" depends="compile" >
<echo message="junit"/>
<junit printsummary="on" fork="false" haltonfailure="no" showoutput="true">
<classpath refid="pomdeps.path" />
<classpath>
<pathelement location="${build.test.dir}"/>
<pathelement location="${build.api.dir}"/>
</classpath>
<formatter usefile="false" type="plain"/>
<batchtest fork="no" todir="${test.report.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="main" depends="compile, junit">
<description>Main target</description>
</target>
</project>
Jenkins is 1.591 i installed it with default parameters as windows installation downloaded from their site.
Can it be something wrong with jenkins? Do i miss something?
As i mentioned earlier the problem was lack of UI permissions for Jenkins server.
1.configure Jenkins to run as service and make it login with real user name
2.Make sure that windows host that runs Jenkins logs on automaticaly after restart.
I have created test project in MVC for unit testing without using NUnit.
Can I use NAnt to run this project?
If no what is the suitable method to automatically run test cases (if possible sequentially)?
You don't have to use NUnit. If you already created a Unit test project in MVC you can use use command line to run this through nant.
From my understanding, you rather just execute the unit test poject you have in your solution which provide you with your test results. If that's the case then all you need to do it run that project using the msbuild command line. If you can run anything on command line chances are you can do the same in NAnt.
Here is how that translate to NAnt.
Example:
<target name="run.unittest.project" >
<property name="msbuild" value="C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"/>
<property name="configuration" value="Debug" />
<exec program="${msbuild}" commandline="YourSolution.sln /p:Configuration:${configuration};Platform=OurPlatform /t:Project_Name"/>
</target>
But....If you wanted to run a NUint test on a specific assembly in NANT you can try this...
<target name="nunit.target" description="" >
<property name="UnitTests" value="Path\project\bin\${configuration}" />
<echo message="Starting the UnitTests ....." />
<nunit2>
<formatter type="Plain" />
<test assemblyname="UnitTests.dll" appconfig="UnitTests.dll.config" />
</nunit2>
</target>
I've seen lots of info on the web about this, but nothing clear & specific which seems to address the problem of simply publishing a web service or web site to a specific folder that I specify at build time.
I'm using Nant and Nant Contrib:
<target name="build" description="builds the service">
<msbuild project="${buildoutput}\${service.source}\wsMyService.sln" >
<property name="Configuration" value="Release" />
<property name="PublishDir" value="${buildoutput}\${service.target}\" />
<property name="Targets" value="Publish" />
</msbuild>
</target>
Can anyone show me how this is supposed to be done. I can change the output folder in the property pages of the project, but I want this to be configurable from Nant so I can specify the path at build time.
When you call msbuild from the command line, you can pass in strings to assign to msbuild properties. I don't know anything about NAnt, so I assume it has to resort to a calling msbuild.exe. So you can override msbuild properties like this:
MsBuild /property:buildoutput=C:\arbitrary\folder\bin\
These properties specified from the command line override anything you specify in your build files.
This is what im currently using to build webservices using msbuild:
<Target Name="BuildWebService">
<ConvertToAbsolutePath Paths="$(Root)">
<Output TaskParameter="AbsolutePaths" PropertyName="Root" />
</ConvertToAbsolutePath>
<ItemGroup>
<WebServices Include="$(Root)\services\Solutions\**\*.host.csproj"/>
</ItemGroup>
<MSBuild Projects="%(WebServices.FullPath)"
Targets="Build"
Properties="WebProjectOutputDir=$(Root)\services\build\WebService\%(RecursiveDir);OutDir=$(Root)\services\build\WebService\%(RecursiveDir)\bin\" />
</Target>
Hope you can translate to nant easy enough.
I have script that dosn't work becouse property once set became unwritable
<target name="test" >
<fileset id="dir1" dir="./dir1"/>
<fileset id="dir2" dir="./dir2"/>
<pathconvert property="path.converted" refid="dir1"/>
<echo message="${path.converted}"/>
<property name="path.converted" value="set this property manually"/>
<echo>${path.converted}</echo>
<pathconvert property="path.converted" refid="dir2"/>
<echo message="${path.converted}"/>
</target>
always echoed the same result, but I want that echoes was different
I read in Apache Ant 1.8.0 release, that
Lexically scoped local properties,
i.e. properties that are only defined
inside a target, sequential block or
similar environment. This is very
useful inside of s where a
macro can now define a temporary
property that will disappear once the
task has finished.
How to use them?
I found solution. Use local task
<target name="direct" depends="">
<fileset id="dir1" dir="./dir1"/>
<fileset id="dir2" dir="./dir2"/>
<!--<property name="path.converted" value="0"/>-->
<local name="path.converted"/>
<pathconvert property="path.converted" refid="dir1"/>
<echo message="${path.converted}"/>
<local name="path.converted"/>
<property name="path.converted" value="0"/>
<echo>${path.converted}</echo>
<local name="path.converted"/>
<pathconvert property="path.converted" refid="dir2"/>
<echo message="${path.converted}"/>
</target>
I would simply use different names for path.converted for the example above.
path.converted.1, path.converted.2 etc.
If you would have created a macrodef you should definitely use the local task to make the property local.
How do I check for the existence of a folder using Ant?
We can check the existence of a file, but can we do the same for a folder as well?
You use the available task with type set to "dir".
For example:
<available file="${dir}" type="dir"/>
The standard way to do conditional processing is with the condition task. In the example below, running doFoo will echo a message if the directory exists, whereas running doBar will echo a message unless the directory exists.
The dir.check target is required by both doFoo and doBar, it sets the dir.exists property to true or false depending on the result of the available task. The doFoo target will only run if that propery is set to true and doBar will only run if it is not set or set to false.
<?xml version="1.0"?>
<project name="test" default="doFoo" basedir=".">
<property name="directory" value="c:\test\directory"/>
<target name="doFoo" depends="dir.check" if="dir.exists">
<echo>${directory} exists</echo>
</target>
<target name="doBar" depends="dir.check" unless="dir.exists">
<echo>${directory} missing"</echo>
</target>
<target name="dir.check">
<condition property="dir.exists">
<available file="${directory}" type="dir"/>
</condition>
</target>
</project>
Antelope provides additional tasks, including an If task that can make the processing simpler (and to me, more intuitive), you can download the Antelope tasks from the download page.
Here's a small example incorporating the available element into an if test.
<!-- Test if a directory called "my_directory" is present -->
<if>
<available file="my_directory" type="dir" />
<then>
<echo message="Directory exists" />
</then>
<else>
<echo message="Directory does not exist" />
</else>
</if>
Warning: you need ant-contrib.jar in your ANT_HOME\lib directory otherwise you won't have access to the if elements, and your script will fail with this error:
Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
Here's my solution, which doesn't require setting properties and using targets with 'if' or 'unless':
Macro:
<macrodef name="assertDirAvailable">
<attribute name="dir" />
<sequential>
<fail message="The directory '#{dir}' was expected to be available but is not">
<condition>
<not>
<available file="#{dir}" type="dir" />
</not>
</condition>
</fail>
</sequential>
</macrodef>
Usage:
<assertDirAvailable dir="${dirToCheck}" />
My solution using ANT 1.8 version, older versions may not work due if/unless not supporting ${evalTrueOrFalse} syntax.
<?xml version="1.0" encoding="UTF-8"?>
<project name="DoMagic" default="build" basedir=".">
<property environment="env" />
<property name="name" value="Do the ANT Magic" />
<property name="somedir" value="./must_exist_folder"/>
<tstamp><format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" /></tstamp>
<target name="doMagic" if="${dir.exists}">
<echo message="Do the magic stuff" />
</target>
<target name="doUsage" unless="${dir.exists}">
<echo message="Do usage and help" />
</target>
<target name="build">
<echo message="Do the magic" />
<condition property="dir.exists" else="false"><available file="${somedir}" type="dir" /></condition>
<echo message="Folder found: ${dir.exists}" />
<antcall target="doCustomize"></antcall>
<antcall target="doUsage"></antcall>
</target>
</project>
ANT 1.6 or early ANT 1.7 does not work, upgrade to ANT 1.8 release.
Target attributes if and unless evaluates ${var} syntax to true/false
Condition attribute else value is set to property if available condition was false, without it variable is not set. NotSet value is not same as an explicit false value.
call any target but if/unless attribute defines whether its actually run
http://ant.apache.org/manual/properties.html#if+unless
[If/Unless] In Ant 1.7.1 and earlier, these attributes could only be property names. As of Ant 1.8.0, you may instead use property expansion. Compared to the older style, this gives you additional flexibility.
Here is another approach, allows to call just one task without using ant-contrib.jar.
<target name="my-task" depends="dir-check">
<antcall target="my-task-install"/>
<antcall target="my-task-update"/>
</target>
<target name="my-task-install" unless="dir.exists" >
{some task}
</target>
<target name="my-task-update" if="dir.exists" >
{another task}
</target>
<target name="dir-check">
<condition property="dir.exists">
<available file="my-dir" type="dir" />
</condition>
</target>
Here is another example involving for loop. Fail if a directory does not exist.
<for list="dir1/, dir2/, dir3/" param="local.dir" >
<sequential>
<fail message="Directory #{local.dir} does not exist">
<condition>
<not>
<available file="#{local.dir}" type="dir" />
</not>
</condition>
</fail>
</sequential>
</for>