简体   繁体   中英

MSBuild task with AfterTargets to encrypt web.config after transform

I'm new to MSBuild and I'm trying to set up a single MSBuildSettings.xml file in my project, called via the "Post-build event command line" option in the Project Preferences, that does 3 particular tasks.

$(MSBuildBinPath)\msbuild.exe "$(ProjectDir)MSBuild\MSBuildSettings.xml"

The MSBuildSettings does 3 particular tasks. My first 2 tasks ("YUI" and "LESS", below) are working perfectly. The new task I'm trying to add ("Encrypt") is to encrypt the web.config AFTER transformation has occurred.

I'm using a single xml file structured as follows:

<Project DefaultTargets="YUI;LESS;Encrypt;">
    ...
    <Target Name="YUI">...</Target>

    <Target Name="LESS" DependsOnTargets="YUI">...</Target>

    <Target Name="Encrypt" AfterTargets="CopyAllFilesToSingleFolderForPackage">
        <Exec Command="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pef &quot;connectionStrings&quot; $(MSBuildProjectDirectory)\..\obj\Release\Package\PackageTmp -prov &quot;MyProtectedDataProvider&quot;" />
    </Target>
</Project

I've basically trying to encrypt the connectionStrings section using the aspnet_regiis via commandline. (I also have my own Provider defined and working.) I do find it a little strange that I'm trying to modify the web.config in the PackageTmp folder, but as far as I can tell that's the final location before the transformed web.config is deployed (I obviously don't want to update the original web.config file in the project). I don't think there's anything wrong with the command/execution so much as it is the timing of it. I simply can't figure out how to make this third step execute only AFTER transformation has occurred.

I've enabled the Detail view option of the build output as well as dug through the Microsoft.Web.Publishing.targets file trying to come up with a target that I can use as to only invoke my web.config encryption step AFTER the transformation has occurred, but regardless of what AfterTargets I try, I seem to always get:

The target "whatever" does not exist in the project.

I've tried PipelinePreDeployCopyAllFilesToOneFolder, TransformWebConfig, PipelineTransformPhase, CopyAllFilesToSingleFolderForPackage, etc..

Additional notes: I understand that the web.config is only transformed upon Publish (and I am publishing using the File System option).

Also this is plain MSBuild (no TFS team build or anything like that).

Any additional or alternative options on a post-transform step to encrypt sections of the web.config would also be greatly appreciated.

I finally wound up using only the *.wpp.targets file with the following structure:

<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">
    <Target Name="AfterBuild">
        ... My YUI steps and my LESS steps ...
    </Target>
    <Target Name="Encrypt" AfterTargets="CopyAllFilesToSingleFolderForPackage">
        ... My web.config encrypt steps ...
    </Target>
</Project>

Honestly, I don't completely understand the reasoning behind the naming of the first step (AfterBuild) and it just automagically knowing when to run. I'd really rather have my own name and a clear AfterTargets or BeforeTargets or something, but I didn't have any luck with that approach. The second Target with the AfterTargets makes some sense to me.

But anyway, it works. Everything is now just in the one wpp.targets file and seems to work great. It's a little bit of a pain having to unload/reload the project file every time you make a change to the wpp.targets file though - that's just goofy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM