How to include a property sheet in VC project temp

2019-02-20 14:51发布

问题:

I have a lot of VC projects in Visual Studio 2013 solution. For each project I keep most of it's properties in property sheet AllConfigurations.props, belonging to project's folder. I'm trying to create a project template for such project. I add AllConfigurations.props to template zip file and reference it in .vstemplate:

  <Project TargetFileName="Test.vcxproj" File="Test.vcxproj" ReplaceParameters="true">
  <ProjectItem ReplaceParameters="false" TargetFileName="AllConfigurations.props">AllConfigurations.props</ProjectItem>

But when creating new project with this template, Visual Studio displays an error:

Unable to read the project file "Test1.vcxproj". Test1\Test1.vcxproj(76,5): The imported project "Test1\AllConfigurations.props" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

The project is not being added to the solution. It seems that VS tries to load new project before unpacking all files from the template.

How do I handle it?

回答1:

Here is how you can create a Visual Studio C++ project template that includes your user property sheets.

  1. Create a project that you want to export to a template and add the property sheet in the Property Manager window. In your case, the AllConfigurations.props file.
  2. In the Solution Explorer window, add the property sheet file to the project. You must do this to get the file into the template archive and be automatically copied to the new project.
  3. Export the project template using File->Export Template. On the second page of the wizard, un-check the Automatically import the template into Visual Studio option.
  4. Extract the .vcxproj file from the template zip file and open in a text editor. Search for the lines that include your property sheet file. There will be one for each build configuration in your project. In your case they should look like this: <Import Project="AllConfigurations.props" />. Change the lines to this: <Import Project="AllConfigurations.props" Condition="exists('AllConfigurations.props')" />. This will allow the project to open even if the file is not available yet.
  5. Save the file and add it back to the template zip file.
  6. Copy the zip file to the <My Documents>\Visual Studio version \Templates\ProjectTemplates directory.

Close and re-open Visual Studio and you should be able to create a new project with your new template. You may have to close the solution and re-open it for the property sheet to be visible under all the configurations in the Property Manager window. You can remove the .props file from your new project in the Solution Explorer window if you don't want it visible there. It will still be used in Property Manager.



回答2:

I had the same problem. I was unable to actually add the property sheet to the project, since it looks like Visual Studio (2013 in my case) treats property sheets as projects. I expect that I could have used some combination of ProjectCollection and a second vstemplate file to create the project that consists of the property sheet first, followed by my actual project. However, I ended up hacking a solution together instead because it was much less work.

  1. Open up the .vcxproj file and find the first line that looks like <ImportGroup Label="PropertySheets" />

  2. Open up your custom property sheet file that contains the properties you're interested in adding. Copy all elements under (but not including) the Project element.

  3. Paste all of that content immediately before the location you found in step (1).

  4. Comment out or remove all sub-elements of ImportGroup elements that reference your custom property sheet (it should now look like <!-- <Import Project="whatever.props" /> -->).

Re-zip and install the template. Now you should be able to create a project with the properties you're interested in. The downsides are that the resulting .vcxproj is kind of ugly and you won't have a separate property sheet, but you should at least be able to distribute and reference your properties in a new project type.