问题
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