I have a solution that contains several c# projects and I would like to be able to set the output path and other properties on all the projects together in a single place. Property Sheets (vsprops) do not seem to be able available for C# projects and the $(SolutionDir) variable is ignored. Are there any other methods to set properties across several C# projects?
我有一个包含几个c#项目的解决方案,我希望能够在一个地方将所有项目的输出路径和其他属性设置在一起。 Property Sheets(vsprops)似乎不能用于C#项目,$(SolutionDir)变量被忽略。是否有其他方法可以在多个C#项目中设置属性?
Update By Following the information in the answer by Bas Bossink I was able to set the output path of several projects by creating a common csproj and importing it into the individual project. A few other points:
更新按照Bas Bossink的回答中的信息,我能够通过创建一个公共csproj并将其导入到单个项目中来设置多个项目的输出路径。其他几点:
- When building in Visual Studio if changes are made to the common project it is necessary to touch/reload any projects that reference it for the changes to be picked up.
- Any properties which are also set in a individual project will override the common properties.
- Setting $(SolutionDir) as the output path via the Visual Studio UI does not work as expected because the value is treated as a string literal rather than getting expanded. However, Setting $(SolutionDir) directly into the csproj file with a text editor works as expected.
在Visual Studio中构建时,如果对公共项目进行了更改,则必须触摸/重新加载引用它的任何项目以获取要更改的更改。
在单个项目中也设置的任何属性都将覆盖公共属性。
通过Visual Studio UI将$(SolutionDir)设置为输出路径不会按预期工作,因为该值被视为字符串文字而不是扩展。但是,使用文本编辑器将$(SolutionDir)直接设置到csproj文件中可以正常工作。
4 个解决方案
#1
A csproj file is already an msbuild file, this means that csproj files can also use an import element as described here. The import element is exactly what you require. You could create a Common.proj that contains something like:
csproj文件已经是一个msbuild文件,这意味着csproj文件也可以使用这里描述的import元素。 import元素正是您所需要的。您可以创建一个包含以下内容的Common.proj:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5"xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputPath>$(SolutionDir)output</OutputPath>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
You can import this Common.proj in each of your csprojs, for instance like so:
您可以在每个csproj中导入此Common.proj,例如:
<Import Project="..\Common.proj" />
The import statement should precede any tasks that depend on the properties defined in Common.proj
import语句应该位于依赖于Common.proj中定义的属性的任何任务之前
I hope this helps. I can't confirm your problems with the $(SolutionDir) variable I've used it many times. I do know however that this variable does not get set when you run an msbuild command via the commandline on a specific project that is contained in a solution. It will be set when you build your solution in Visual Studio.
我希望这有帮助。我无法用$(SolutionDir)变量确认你的问题,我多次使用它。但我确实知道,当您通过命令行在解决方案中包含的特定项目上运行msbuild命令时,不会设置此变量。在Visual Studio中构建解决方案时将设置它。
#2
Unfortunately, these bits of information such as output path are all stored inside the individual *.csproj files. If you want to batch-update a whole bunch of those, you'll have to revert to some kind of a text-updating tool or create a script to touch each of those files.
不幸的是,这些信息(如输出路径)都存储在各个* .csproj文件中。如果您想批量更新其中的一大堆,那么您将不得不恢复某种文本更新工具或创建脚本来触摸每个文件。
For things like this (apply changes to a bunch of text files at once) I personally use WildEdit by Helios Software - works like a charm and it's reasonably priced.
对于这样的事情(同时对一堆文本文件应用更改)我个人使用Helios Software的WildEdit - 工作就像一个魅力,它的价格合理。
But I'm sure there are tons of free alternatives out there, too.
但我确信那里也有大量的免费替代品。
#3
I would suggest you to use a build tool such as MSBuild or NAnt which would give you more flexibility on your builds. Basically the idea is to kick off a build using (in most cases) a single configurable build file.
我建议你使用一个构建工具,如MSBuild或NAnt,它可以为你的构建提供更大的灵活性。基本上,我们的想法是使用(在大多数情况下)单个可配置的构建文件来启动构建。
I would personally recommend NAnt.
我会亲自推荐NAnt。
You could find an awesome tutorial on NAnt on JP Boodhoo's blog here
您可以在JP Boodhoo的博客上找到关于NAnt的精彩教程
#4
Set the $(OutputPath)
property in a common property sheet. Then delete that entry in all the project files you want to it to affect. Then import that property sheet into all your projects.
在公共属性表中设置$(OutputPath)属性。然后删除要对其影响的所有项目文件中的该条目。然后将该属性表导入到所有项目中。
For hundreds of projects that can be very tedious. Which is why I wrote a tool to help with this:
数以百计的项目可能非常繁琐。这就是为什么我写了一个工具来帮助解决这个问题:
#1
A csproj file is already an msbuild file, this means that csproj files can also use an import element as described here. The import element is exactly what you require. You could create a Common.proj that contains something like:
csproj文件已经是一个msbuild文件,这意味着csproj文件也可以使用这里描述的import元素。 import元素正是您所需要的。您可以创建一个包含以下内容的Common.proj:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5"xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputPath>$(SolutionDir)output</OutputPath>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
You can import this Common.proj in each of your csprojs, for instance like so:
您可以在每个csproj中导入此Common.proj,例如:
<Import Project="..\Common.proj" />
The import statement should precede any tasks that depend on the properties defined in Common.proj
import语句应该位于依赖于Common.proj中定义的属性的任何任务之前
I hope this helps. I can't confirm your problems with the $(SolutionDir) variable I've used it many times. I do know however that this variable does not get set when you run an msbuild command via the commandline on a specific project that is contained in a solution. It will be set when you build your solution in Visual Studio.
我希望这有帮助。我无法用$(SolutionDir)变量确认你的问题,我多次使用它。但我确实知道,当您通过命令行在解决方案中包含的特定项目上运行msbuild命令时,不会设置此变量。在Visual Studio中构建解决方案时将设置它。
#2
Unfortunately, these bits of information such as output path are all stored inside the individual *.csproj files. If you want to batch-update a whole bunch of those, you'll have to revert to some kind of a text-updating tool or create a script to touch each of those files.
不幸的是,这些信息(如输出路径)都存储在各个* .csproj文件中。如果您想批量更新其中的一大堆,那么您将不得不恢复某种文本更新工具或创建脚本来触摸每个文件。
For things like this (apply changes to a bunch of text files at once) I personally use WildEdit by Helios Software - works like a charm and it's reasonably priced.
对于这样的事情(同时对一堆文本文件应用更改)我个人使用Helios Software的WildEdit - 工作就像一个魅力,它的价格合理。
But I'm sure there are tons of free alternatives out there, too.
但我确信那里也有大量的免费替代品。
#3
I would suggest you to use a build tool such as MSBuild or NAnt which would give you more flexibility on your builds. Basically the idea is to kick off a build using (in most cases) a single configurable build file.
我建议你使用一个构建工具,如MSBuild或NAnt,它可以为你的构建提供更大的灵活性。基本上,我们的想法是使用(在大多数情况下)单个可配置的构建文件来启动构建。
I would personally recommend NAnt.
我会亲自推荐NAnt。
You could find an awesome tutorial on NAnt on JP Boodhoo's blog here
您可以在JP Boodhoo的博客上找到关于NAnt的精彩教程
#4
Set the $(OutputPath)
property in a common property sheet. Then delete that entry in all the project files you want to it to affect. Then import that property sheet into all your projects.
在公共属性表中设置$(OutputPath)属性。然后删除要对其影响的所有项目文件中的该条目。然后将该属性表导入到所有项目中。
For hundreds of projects that can be very tedious. Which is why I wrote a tool to help with this:
数以百计的项目可能非常繁琐。这就是为什么我写了一个工具来帮助解决这个问题: