Visual studio to override file on selected build configuration

泄露秘密 提交于 2020-04-30 07:17:29

问题


at the moment my project has a license file. No extension attached to the file. The file itself only contains a key.

I have 3 build configuration

Dev
Stage
Prod

At the moment I have 4 license file.

GLicense 

GLicense_dev
GLicense_stage
GLicense_prod

I tried to use #c preprocessor directive but the third party dll requires me to have the license name exactly as GLicense. The next approach I was thinking of taking was overriding the content of GLicense when building. I was wondering how abouts do I do that?


回答1:


Modify your Projects pre-build command line to include the desired source file for each configuration

copy /y "$(ProjectDir)GLicense_dev" "$(ProjectDir)$(OutDir)GLicense"

copy /y "$(ProjectDir)GLicense_stage" "$(ProjectDir)$(OutDir)GLicense"

copy /y "$(ProjectDir)GLicense_prod" "$(ProjectDir)$(OutDir)GLicense"

Your Project file will look something like this

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'dev|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_dev.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'stage|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_stage.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'prod|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_prod.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>



回答2:


I believe the true answer is to use $(TargetDir) vs $(ProjectDir)$(OutDir) in "copy To". You don't want to have a mention of $(ProjectDir) on right side. What if your output is outside of your project folder, may be even different computer. Plus, instead of changing project file in project XML, you can just open properties and in "build events" add this single event

if $(ConfigurationName) == dev copy /y "$(ProjectDir)GLicense_dev" "$(TargetDir)GLicense"
if $(ConfigurationName) == stage copy /y "$(ProjectDir)GLicense_stage" "$(TargetDir)GLicense"
if $(ConfigurationName) == prod copy /y "$(ProjectDir)GLicense_prod" "$(TargetDir)GLicense"

Another way (and this is true Visual Studio solution because it eliminates CMD/Batch coding. The downside in your case, you have 3 different files with the same name in 3 different folders), add your 3 files to a project as content in 3 different folders but file name - same (GLicense). And pick "Copy Always" in build action. Then in project file XML add Condition=" '$(Configuration)' == 'dev' on the file itself rather than property group

<None Include="dev\GLicense" Condition=" '$(Configuration)' == 'dev'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="stage\GLicense" Condition=" '$(Configuration)' == 'stage'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="prod\GLicense" Condition=" '$(Configuration)' == 'prod'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>


来源:https://stackoverflow.com/questions/60957751/visual-studio-to-override-file-on-selected-build-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!