If you have not read the first part of the tutorial, please read it first and then return here to continue our second part.
Our next step is to release our website, that is, create a publish target. to make our target work, we need to pass two attributes to it: webprojectoutputdir and outdir. These two variables will ensure that we can publish our website files to the correct directory.
Msbuild allows us to declare a propertygroup. In this propertygroup, we can create some intermediate variables to store some set data. When we perform read/write operations, we need the set data.
Let's look at the example:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run"> 3 <PropertyGroup> 4 <OutputFolder>Output</OutputFolder> 5 </PropertyGroup> 6 7 <Target Name="Run"> 8 <CallTarget Targets="Clean" /> 9 <CallTarget Targets="Publish" />10 </Target>11 12 <Target Name="Clean">13 <ItemGroup>14 <BinFiles Include="bin\*.*" />15 </ItemGroup>16 <Delete Files="@(BinFiles)" />17 </Target>18 19 <Target Name="Publish">20 <RemoveDir Directories="$(OutputFolder)" ContinueOnError="true" />21 <MSBuild Projects="BuildDemoSite.csproj"22 Targets="ResolveReferences;_CopyWebApplication"23 Properties="WebProjectOutputDir=$(OutputFolder);OutDir=$(WebProjectOutputDir)\" />24 </Target>25 </Project>
As you can see, resolvereferences target is also called to ensure that third-party dependencies can be copied to the output directory.
Run the following command: msbuild build. xml:
However, you may have noticed that some files that I don't want to copy are also copied in the output directory, such as build. the XML script itself also has some environment-related configuration files: Live. config & test. config. that's because their build action is set to content. You only need to set the build action to none, and then re-execute the script to avoid the above problems.
Now this website can be deployed, but we still need to copy environment-related configurations to Web. config. Now let's start this tutorial.
As we mentioned in the first part of the tutorial, in addition to the Web. config file, we also have the live. config and test. config files. Web. config contains all the configurations about the website, live. config and test. config contains some configurations related to the deployment environment. The next step is to copy the values related to the deployment environment to the Web. config.
To achieve this goal, we usually use XPath to read configuration information from a file, and then write the information to another file through XPath. However, this feature was not found in the built-in tasks. Fortunately, the msbuild community tasks project saved us and provided xmlread and xmllupdate tasks. Let's download and install it first.
Before using them, make sure that the following XML has been inserted to the root node of Our build. xml.
<Import Project="C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
Then we add an intermediate variable of environment in propertygroup. Currently, it is written as test by hard encoding. We will modify this variable in a later tutorial so that it can be configured.
<PropertyGroup> <Environment>Test</Environment> <CompilationDebug /> <OutputFolder>Output</OutputFolder></PropertyGroup>
Well, the first step is to use xmlread task to obtain environment configuration data. Currently, I only list one xmlread example. You can add more
1 <Target Name="GetConfig">2 <XmlRead XPath="configuration/system.web/compilation/@debug"3 XmlFileName="$(Environment).config">4 <Output TaskParameter="Value" PropertyName="CompilationDebug" />5 </XmlRead>6 </Target>
- Getconfig target contains all xmlread tasks
- The xmlfilename attribute has a variable $ (Environment). config, which points to the hard-coded test.
- Output is used to store the returned values of xmlread to the compilationdebug attribute.
Similarly, our xmlupdate adopts the same syntax. Note that dependsontargets = "getconfig" is used to ensure that data is read and written before being copied.
1 <Target Name="SetConfig" DependsOnTargets="GetConfig">2 <XmlUpdate XPath="configuration/system.web/compilation/@debug"3 XmlFileName="Output\web.config"4 Value="$(CompilationDebug)" />5 </Target>
Okay, let's start to execute the following command to witness the miracle.
Msbuild build. xml/T: setconfig
Check whether all commands work. Open the Web. config file in the output file and check whether the data is consistent with that in test. config.
You can view the entire build. XML code.