![](/img/trans.png)
[英]Visual Studio 2010 — selected Solution Configuration affecting Batch Build output
[英]Visual studio to override file on selected build configuration
目前我的項目有一個許可證文件。 文件沒有附加擴展名。 文件本身只包含一個密鑰。
我有 3 個構建配置
Dev
Stage
Prod
目前我有 4 個許可證文件。
GLicense
GLicense_dev
GLicense_stage
GLicense_prod
我嘗試使用 #c 預處理器指令,但第三方 dll 要求我擁有與GLicense
完全相同的許可證名稱。 我正在考慮采用的下一個方法是在構建時覆蓋 GLicense 的內容。 我想知道我該怎么做?
我相信真正的答案是在“復制到”中使用$(TargetDir)
與$(ProjectDir)$(OutDir)
。 您不想在右側提及$(ProjectDir)
。 如果您的輸出在您的項目文件夾之外,甚至可能是不同的計算機怎么辦。 另外,無需在項目 XML 中更改項目文件,您只需打開屬性並在“構建事件”中添加此單個事件
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"
另一種方式(這是真正的 Visual Studio 解決方案,因為它消除了 CMD/Batch 編碼。您的情況的缺點是,您在 3 個不同的文件夾中有 3 個具有相同名稱的不同文件),將您的 3 個文件作為 3 個中的內容添加到項目中不同的文件夾但文件名 - 相同( GLicense
)。 並在構建操作中選擇“始終復制”。 然后在項目文件 XML 中添加Condition=" '$(Configuration)' == 'dev'
文件本身而不是屬性組
<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>
修改您的項目預構建命令行以包含每個配置所需的源文件
copy /y "$(ProjectDir)GLicense_dev" "$(ProjectDir)$(OutDir)GLicense"
copy /y "$(ProjectDir)GLicense_stage" "$(ProjectDir)$(OutDir)GLicense"
copy /y "$(ProjectDir)GLicense_prod" "$(ProjectDir)$(OutDir)GLicense"
您的項目文件將如下所示
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.