Publish WebApplication using NAnt - build

Is it possible to accomplish publish (as in Visual Studio publish on Web Application project) on solution using NAnt? I just can't find the solution.

They key is to use the built-in "_CopyWebApplication" target.
Here is what i do
<target name="compile" description="Compiles the project.">
<exec basedir="." program="${DotNetPath}msbuild.exe" commandline=" src/MyProject.Web/MyProject.Web.csproj /nologo
/t:Rebuild
/t:ResolveReferences;_CopyWebApplication
/p:OutDir=../../output/build/bin/
/p:WebProjectOutputDir=../../output/build/
/p:Debug=${debug}
/p:Configuration=${configuration}
/v:m"
workingdir="." failonerror="true" />
</target>
with the dir structure of:
/project.build
/src/myprojct.sln
/src/myporject.web/myproject.web.csproj
/output
Edit: i also use this to use the YUI compression to compress my css and js
<target name="compress-js">
<foreach item="File" property="filename">
<in>
<items basedir="output/build/assets/javascript/">
<include name="/**/*.js" />
<exclude name="/**/*.min.js" />
<exclude name="/**/*.pack.js" />
</items>
</in>
<do>
<exec basedir="." program="${JavaPath}java" commandline=" -jar S:\yuicompressor-2.4.1\build\yuicompressor-2.4.1.jar --type js --charset utf-8 -o "${filename}" "${filename}"" failonerror="true" />
</do>
</foreach>
</target>
<target name="compress-css" depends="combine-css">
<foreach item="File" property="filename">
<in>
<items basedir="output/build/assets/css/">
<include name="/**/*.css" />
<exclude name="/**/*.min.css" />
<exclude name="/**/*.pack.css" />
</items>
</in>
<do>
<exec basedir="." program="S:\Java\jdk1.6.0_11\bin\java" commandline=" -jar S:\yuicompressor-2.4.1\build\yuicompressor-2.4.1.jar --type css --charset utf-8 -o "${filename}" "${filename}"" failonerror="true" />
</do>
</foreach>
</target>

Using MSBuild beats the purpose of using NAnt, NAnt is something that replaces MSBuild, use the below to do a clean NAnt compilation of Web Application Projects as in VS 2003/2005/2008.
It works for me!
<?xml version="1.0"?>
<project name="MyTest" default="run">
<property name="basename" value="MyTest1x"/>
<property name="debug" value="false"/>
<property name="copytarget" value="c:\temp"/>
<target name="clean">
<delete>
<fileset basedir="${copytarget}">
<include name="bin/${basename}.dll"/>
<include name="**/*.???x"/>
<include name="Web.config"/>
</fileset>
</delete>
</target>
<target name="build">
<mkdir dir="${copytarget}/bin" />
<csc target="library" output="${copytarget}/bin/${basename}.dll" >
<sources>
<include name="*.cs"/>
</sources>
</csc>
</target>
<target name="run" depends="clean,build">
<copy todir="${copytarget}" overwrite="true">
<fileset basedir=".">
<include name="**/*.???x" />
<include name="Web.config" />
</fileset>
</copy>
</target>
</project>

I know this is an old question, but I just learned something, so I decided I'd share: While it is 100% true that the "_CopyWebApplication" target exists and works, as of .NET 4.0 it has been superseded by the "_WPPCopyWebApplication" target in Microsoft.Web.Publishing.targets, which supports new features like web.config transformation syntax, etc.
(reposting to all the similarly worded questions, so don't vote for this one :) )

Here is how you can do it using the MSBUILD task for nant:
<property name="debug" value="AutomatedDebug" />
<property name="configuration" value="Debug;TargetFrameworkVersion=v3.5" />
<msbuild project="src\WebApplication1\WebApplication1.csproj" failonerror="true">
<arg value="/nologo" />
<arg value="/t:Rebuild" />
<arg value="/t:ResolveReferences;_CopyWebApplication" />
<arg value="/p:OutDir=../../build/Debug/WebApplication/" />
<arg value="/p:WebProjectOutputDir=../../build/Debug/WebApplication-Deploy/" />
<arg value="/p:Debug=${debug}" />
<arg value="/p:Configuration=${configuration}" />
<arg value="/v:m" />
</msbuild>
This will compile the WebApplication into a deployable folder, just like using the "Publish" feature from within Visual Studio.

Related

How can I execute an ant target depending on the value of an environment variable?

I want to execute an ant script on two different computers. Depending on the name of the computer either one of the two targets should be executed. Following doesn't work:
<project name="import" default="all">
<property environment="env"/>
<target name="staging" if="${env.COMPUTERNAME}='STG'">
<echo>executed on staging</echo>
</target>
<target name="production" if="${env.COMPUTERNAME}='PRD'">
<echo>executed on production</echo>
</target>
<target name="all" depends="staging,production" description="STG or PRD"/>
</project>
As I understood, "if" can only be used with property and it checks whether a property is set or not. But is there a way to to make a condition depending on the value of a property?
I would suggest writing an "init" target that sets any conditions you need for later build steps, and also fails the build if certain required properties don't evaluate to what's expected.
For example:
<target name="all" depends="staging,production,init" />
<target name="staging" if="staging.environment" depends="init">
<echo message="executed on staging" />
</target>
<target name="production" if="production.environment" depends="init">
<echo message="executed on production" />
</target>
<target name="init">
<condition property="staging.environment">
<equals arg1="${env.COMPUTERNAME}" arg2="STG" />
</condition>
<condition property="production.environment">
<equals arg1="${env.COMPUTERNAME}" arg2="PRD" />
</condition>
<fail message="Neither staging nor production environment detected">
<condition>
<not>
<or>
<isset property="staging.environment" />
<isset property="production.environment" />
</or>
</not>
</condition>
</fail>
</target>

Can we convert a freestyle job in jenkins into a template

I have configured a freestyle Jenkins job to perform build, run some tests, perform code analysis and upload artifacts to Nexus. I will need to create more of such jobs in future and so I want to create a template so that its easy for anyone else to create those jobs in future. Is there a way to convert the freestyle job into template?
You can create generic mechanisms using Ant(In my case NAnt), and use them using a set of imported variables.
My config for web app looks like:
<?xml version="1.0" encoding="utf-8"?>
<webapps>
<add basedir="deploys\JobLocation" deploydir="C:\CIReference\Sites" appname="JobLocation" appid="5" appuri="http/*:8081" apppool.runtineversion="v4.0" />
</webapps>
The add tag has the basic configuration of the webapp
And the generic mechanism to deploy web application is like this
<?xml version="1.0" encoding="utf-8"?>
<project name="webdeploy" xmlns="http://nant.sf.net/release/0.92/nant.xsd">
<include buildfile="baseconfigs.xml" />
<include buildfile="external_tools.xml" />
<call target="baseConfigs" />
<property name="current.environment.path" value="${path::combine(nant.environmentsdir,environment)}"/>
<foreachxml file="${path::combine(current.environment.path,'webapps.xml')}" xpath="/webapps/add" property="basedir,deploydir,appname,appid,appuri,apppool.runtineversion">
<exec program="${appcmd.path}" failonerror="false">
<arg value="delete" />
<arg value="site" />
<arg value="/site.name: ${appname}" />
</exec>
<exec program="${appcmd.path}" failonerror="false">
<arg value="delete" />
<arg value="apppool" />
<arg value="/apppool.name: ${appname}" />
</exec>
<echo message="Deleting directory: ${path::combine(deploydir, appname)}" />
<delete dir="${path::combine(deploydir, appname)}" failonerror="false" />
<echo message="Coping directory to: ${path::combine(deploydir, appname)}" />
<copy todir="${path::combine(deploydir, appname)}" includeemptydirs="true" overwrite="true" verbose="true">
<fileset basedir="${path::combine(directory::get-current-directory(), basedir)}">
<include name="**\*.*" />
</fileset>
</copy>
<exec program="${appcmd.path}">
<arg value="add" />
<arg value="apppool" />
<arg value="/name: ${appname}" />
</exec>
<exec program="${appcmd.path}">
<arg value="set" />
<arg value="apppool" />
<arg value="/name: ${appname}" />
<arg value="/managedRuntimeVersion:${apppool.runtineversion}" />
</exec>
<exec program="${appcmd.path}">
<arg value="add" />
<arg value="site" />
<arg value="/name: ${appname}" />
<arg value="/id: ${appid}" />
<arg value="/bindings: ${appuri}" />
<arg value="/physicalPath: ${path::combine(deploydir, appname)}" />
</exec>
<exec program="${appcmd.path}" failonerror="false">
<arg value="set" />
<arg value="site" />
<arg value="${appname}" />
<arg value="/applicationPool:${appname}" />
</exec>
</foreachxml>
</project>
You can use this tool to create whatever mechanism do you need for your CI solution.
Order files...
baseconfigs.xml
<?xml version="1.0" encoding="utf-8"?>
<project>
<property name="nant.dir" value="" />
<property name="nant.environmentsdir" value="" />
<target name="baseConfigs">
<cd dir="..\..\" />
<property name="nant.dir" value="${path::combine(directory::get-current-directory(),'jenkins\NAnt')}" />
<property name="nant.environmentsdir" value="${path::combine(nant.dir,'environments')}" />
</target>
</project>
external_tools.xml
<?xml version="1.0" encoding="utf-8"?>
<project>
<property name="appcmd.path" value="c:\windows\System32\InetSrv\appcmd.exe" />
<property name="java_path" value="C:\Program Files (x86)\Jenkins\jre\bin"/>
<property name="liquibase_exe_path" value="C:\Program Files (x86)\Jenkins\tools\liquibase\"/>
<loadtasks assembly="C:\Program Files (x86)\Jenkins\tools\nant\nantcontrib\NAnt.Contrib.Tasks.dll" />
<loadtasks assembly="C:\Program Files (x86)\Jenkins\tools\nant\NAnt.GF.Custom.Tasks.dll" />
</project>

MsBuild Cpp BeforeBuild not working

I want to execute a target before project builds.
I have define the following in the .vcxproj file.
<Target Name="BeforeBuild">
<Message Text="BeforeBuilds" />
<CallTarget Targets="myTarget" />
</Target>
<Import Project="..\GetCat.targets" />
<Target Name="DefaultBeforeTarget" BeforeTargets="Default">
<CallTarget Targets="myTarget" />
</Target>
<Target Name="myTarget" Inputs="$(TargetPath)" Outputs="$(TargetDir)$(TargetName).tlb" DependsOnTargets="GetCat">
<Message Text="Calling myTarget" />
<Exec Command="mkdri HelloW" />
</Target>
The myTarget <Message> is not getting called.
How to fix this?

Nant Build script undifined Issue

I have the following code in a nant build script:
<project name="fgs">
<property name="build.dir" value="build"/>
<property name="build.bin.dir" value="${build.dir}/bin"/>
<fileset id="provider.1.0-references" basedir="${build.bin.dir}">
<include name="thenameofadllfile.*"/>
</fileset>
<macrodef name="build-dist">
<attributes>
<attribute name="version"/>
<attribute name="service.references"/>
<attribute name="release.type"/>
</attributes>
<sequential>
<echo message="service.references: ${service.references}" />
<copy todir="${build.dist.dir}/server/${version}/${release.type}/bin" >
<fileset refid="#{service.references}" casesensitive="false" />
</copy>
</sequential>
</macrodef>
<target name="create-dist">
<server-staging-dist release.type="staging" version="1.0" service.references="service.1.0-references" />
</target>
however when i run this code I get: fileset reference '#{service.references}' is not defined.
i have tried it with changing the doller sign for the # symbol. Not sure what the difference is?
Thanks in advance for any help or advice given.
Try <fileset refid="provider.1.0-references" casesensitive="false" />

I want to use NAnt's foreach to iterate files in a folder, how to force alphabetic iteration?

I have an NAnt task "ship" to package my current .sql scripts into a build, then name the build with an incrementing int {######} and copy it to a build folder.
I have another NAnt task which executes those build scripts.
They must execute in order, but in my last attempt, they were not. Can I "force" NAnt to work alphabetically?
FAIL:
<fileset basedir="source\tsql\builds\" id="buildfiles">
<include name="*.sql.template.sql" />
<exclude name="*.sql" />
<exclude name="*asSentTo*" />
</fileset>
<foreach item="File" property"filename">
<in refid="buildfiles">
<echo message="${filename}" />
</in>
</foreach>
PASS:
<foreach item="File" property="filename" in="source\tsql\builds">
<do>
<if test="${string::ends-with(filename,'.sql.template.sql')}">
<echo message="${filename}" />
</if>
</do>
</foreach>
To satisfy my curiosity I tried to reproduce the problem with this script:
<?xml version="1.0"?>
<project name="foreach.test" default="foreach.alpha">
<target name="foreach.alpha">
<foreach item="File" in="C:\foo" property="filename">
<do>
<echo message="${filename}" />
</do>
</foreach>
</target>
</project>
The filenames are printed out in alphabetical order. So conventional use of foreach already seems to be the solution to the problem.
Here is how you do it with a fileset
<fileset id="mySet">
<include name="*.sql" />
</fileset>
<copy>
<fileset refid="mySet" />
</copy>
<foreach item="File" property="filename">
<in>
<items refid="mySet" />
</in>
<do>
<echo message="Copied files: ${filename} to directory: ${Folder}." />
</do>
</foreach>