Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

msbuild - web.config file transform from command line

I have two build environments to target; Release and Staging. The Web.config looks like this:

<system.web>     
    <authentication mode="Windows">
    </authentication>

    <authorization>
        <deny users="?" />
    </authorization>
</system.web>

I want to transform it by building to staging config: Web.Staging.config

<system.web>     
    <authentication mode="Windows">
    </authentication>

    <authorization xdt:Transform="Replace">
        <deny users="?" />
        <allow roles="StagingRoles" />
        <deny users="*" />
    </authorization>
</system.web>

I build from command line like this:

msbuild buildscript.build /p:Configuration=Staging

After the build, I don't see the web.config file transformed in the build artifacts folder. Is there something wrong here?

Thanks

question from:https://stackoverflow.com/questions/10506308/web-config-file-transform-from-command-line

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you add the following xml to the bottom of the .csproj file for your web application, you'll ensure that the config transformation occurs before every build:

<Import Project="$(MSBuildExtensionsPath32)MicrosoftVisualStudiov10.0WebApplicationsMicrosoft.WebApplication.targets" />
<Target Name="BeforeBuild">
    <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>

Edit: In response to your comment, you should be able to use Web.config as the source parameter in the TransformXml task (see step #2). If you only want to perform the config transform in the build script, follow these instructions:

1) Import WebApplication.targets in your build script like so:

<Import Project="$(MSBuildExtensionsPath32)MicrosoftVisualStudiov10.0WebApplicationsMicrosoft.WebApplication.targets" />

2) Execute the TransformXml build task in your build script target:

<Target Name="MyBuildScriptTarget">
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
    ...other build tasks...
</Target>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...