使用Nutarget使用.targets文件设置文件属性 [英] Setting file properties with NuGet using .targets file

查看:142
本文介绍了使用Nutarget使用.targets文件设置文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个要作为NuGet软件包安装的项目,并且我想设置SpecFlow功能文件的属性(因为它是最新的SpecFlow,并且不应为这些功能生成代码隐藏文件.)

I am building a project to be installed as a NuGet package and I want to set the properties of a SpecFlow feature file (because it is latest SpecFlow and should not produce code-behind files for the features.)

要获得选择文件,打开其属性"窗格并设置几个值的效果,我将项目结构设置如下:

To achieve the effect of selecting a file, opening it's Properties pane and setting a couple of values, I have set my project structure like this:

\MyProject
  \build
    MyProject.targets
    \Framework <the folder containing the file I want to affect>
      FrameworkTests.feature <the file I want to affect>
  \Framework
    FrameworkTests.feature <the original location of the file I want to affect>

我的.nuspec是这样的:

My .nuspec like this:

<?xml version="1.0"?>
<package >
  <metadata minClientVersion="2.5">
    <id>$id$</id>
    <version>$version$</version>
    ...
  </metadata>
  <files>
    <file src="build\MyProject.targets" target="build\MyProject.targets" />
    <file src="build\FrameworkTests\FrameworkTests.feature" target="build\Framework\FrameworkTests.feature" />
  </files>
</package>

我的.targets文件是这样的:

My .targets file like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)FrameworkTests\FrameworkTests.feature">
      <Link>FrameworkTests.feature</Link>
      <CopyToOutputDirectory>Copy if newer</CopyToOutputDirectory>
      <CustomToolNamespace></CustomToolNamespace>
    </None>
  </ItemGroup>
</Project>

安装软件包后,我没有看到FrameworkTests.feature文件被复制到项目中.我需要更改什么?

I am not seeing the FrameworkTests.feature file get copied to the project when the package is installed. What do I need to change?

推荐答案

安装软件包后,我没有看到FrameworkTests.feature文件被复制到项目中.我需要更改什么?

I am not seeing the FrameworkTests.feature file get copied to the project when the package is installed. What do I need to change?

这是nuget文件夹约定的默认行为.如果将文件设置在content文件夹中,nuget会将这些内容复制到项目根目录,然后您将在解决方案资源管理器中看到它们.如果您在build文件夹中设置文件,nuget会自动将它们插入到项目文件或project.lock.json中,就像项目文件中的以下脚本一样:

This is the default behavior of the nuget folder conventions. If you set the file in the content folder, nuget will copy those contents to the project root, then you will see them in the solution explorer. If you set files in the build folder, nuget will automatically inserted them into the project file or project.lock.json, like following script in the project file:

<Import Project="..\packages\MyProject.1.0.0\build\MyProject.targets" Condition="Exists('..\packages\MyProject.1.0.0\build\MyProject.targets')" />

因此,这就是为什么除非将文件夹更改为content,否则在解决方案资源管理器中看不到文件FrameworkTests.feature的原因.

So, this is the reason why you could not see the file FrameworkTests.feature in the solution explorer unless you change the folder to content.

您可以参考文档创建.nuspec文件以获取更多详细信息:

You can refer to the document Creating the .nuspec file for more details:

此外,如果将文件夹更改为内容,则.targets将不起作用.因为当您将文件夹更改为内容时,在.targets文件中,您需要将路径从以下位置更改:

Besides, if you change the folder to content, the .targets would not work. Because when you change the folder to content, in the .targetsfile, you need to change the path from:

<None Include="$(MSBuildThisFileDirectory)FrameworkTests\FrameworkTests.feature">

收件人:

<None Include="$(MSBuildThisFileDirectory)..\content\FrameworkTests\FrameworkTests.feature">

但是MSBuild无法解析路径..\content.要解决此问题,我们需要将.targets文件更改为:

But MSBuild could not parse the path ..\content. To resolve this issue, we need to change the .targets file to:

<None Include="$(ProjectDir)FrameworkTests.feature">

或者,您可以使用Install.ps1文件更改文件的属性,并将其设置为nuget包.

Alternatively, you can change property of files with Install.ps1 file, set it into the nuget package.

请参见此线程有关更多详细信息.

See this thread for more details.

更新:

我还应用了您描述的更改,但是仍然无法获取CopyToOutputDirectory文件属性进行更新.

I have also applied the changes you've described, but still cannot get the CopyToOutputDirectory file property to be updated.

找到了.有两点需要更新,而一点应该知道.

Found it. There are two points you need update and one point should to know.

第一点:

我在您的.nuspec中找到了以下脚本:

I found following script in your .nuspec:

  <files>
    <file src="build\MyProject.targets" target="build\MyProject.targets" />
    <file src="build\FrameworkTests\FrameworkTests.feature" target="build\Framework\FrameworkTests.feature" />
  </files>

注意:您为build\Framework设置了目标文件夹,但在.targets文件中,将其包含在FrameworkTests中;

Note: You set your target folder for build\Framework but in your .targets file, you including it with FrameworkTests;

<None Include="$(MSBuildThisFileDirectory)FrameworkTests\FrameworkTests.feature">

因此,将其更改为内容文件夹时,您的.nuspec文件应为:

So, when change it to content folder, your .nuspec file should be:

  <files>
    <file src="build\MyProject.targets" target="build\MyProject.targets" />
    <file src="content\FrameworkTests\FrameworkTests.feature" target="content\FrameworkTests\FrameworkTests.feature" />
  </files>

.targets文件应为:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(ProjectDir)FrameworkTests\FrameworkTests.feature">
      <Link>FrameworkTests.feature</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CustomToolNamespace></CustomToolNamespace>
    </None>
  </ItemGroup>
</Project>

第二点需要更新:

.targets文件中CopyToOutputDirectory的值应为 PreserveNewest (如果较新则不复制).

The value of CopyToOutputDirectory in the .targets file should be PreserveNewest Not Copy if newer.

您需要知道的要点:

使用此方法更改FrameworkTests.feature的属性时,该文件的属性在解决方案资源管理器中不会更改,但是,MSBuild将在生成项目时应用此更改.那是因为<Import Project="....\MyProject.targets" Condition="Exists('...')" />在编译项目时将被解析.

When you use this method to change the property of the FrameworkTests.feature, the property of this file would not changed in the solution explorer, however, MSBuild will apply this change when you build the project. That because <Import Project="....\MyProject.targets" Condition="Exists('...')" /> will be parsed when you compile the project.

因此,您可以在构建项目后检查CopyToOutputDirectory文件属性的输出(构建项目后,文件FrameworkTests.feature将被复制到输出文件夹.)

So, you can check the output for CopyToOutputDirectory file property after build the project(After build your project, the file FrameworkTests.feature would be copied to the output folder.)

Update2:

许多评论表明无法在PowerShell脚本上运行PowerShell 构建服务器,因为install命令不会运行它们;仅有的 VisualStudio将

A lot of comments circulating that PowerShell scripts can't be run on build servers because the install command doesn't run them; Only VisualStudio will

不确定为何无法在构建服务器上运行PowerShell脚本的所有原因,但绝大多数原因是因为PowerShell的默认安全级别.

Not sure of all the reasons why PowerShell scripts can't be run on build servers, but the vast majority are because PowerShell's default security level.

尝试在PowerShell中输入以下内容:

Try typing this in the PowerShell:

set-executionpolicy remotesigned

在Visual Studio 2015中,您需要安装扩展用于Visual Studio 2015的PowerShell工具 Windows Management Framework 4.0 .安装它们之后,install.ps1可以正常工作.以下是我的.nuspec文件和install.ps 1脚本.

In the Visual Studio 2015, you need to install the extension PowerShell Tools for Visual Studio 2015 and the Windows Management Framework 4.0. After install them, the install.ps1 would works fine. Following is my .nuspec file and install.ps1 script.

.nuspec文件:

  <files>
    <file src="content\FrameworkTests\FrameworkTests.feature" target="content\FrameworkTests\FrameworkTests.feature" />
    <file src="scripts\Install.ps1" target="tools\Install.ps1" />
  </files>

注意:不要忘记删除.nuspec文件中的MyProject.targets.

Note: Do not forget remove the MyProject.targets in the .nuspec file.

install.ps`1脚本:

param($installPath, $toolsPath, $package, $project)

function MarkDirectoryAsCopyToOutputRecursive($item)
{
    $item.ProjectItems | ForEach-Object { MarkFileASCopyToOutputDirectory($_) }
}

function MarkFileASCopyToOutputDirectory($item)
{
    Try
    {
        Write-Host Try set $item.Name
        $item.Properties.Item("CopyToOutputDirectory").Value = 2
    }
    Catch
    {
        Write-Host RecurseOn $item.Name
        MarkDirectoryAsCopyToOutputRecursive($item)
    }
}

#Now mark everything in the a directory as "Copy to newer"
MarkDirectoryAsCopyToOutputRecursive($project.ProjectItems.Item("FrameworkTests"))

然后,在安装nuget软件包后,文件FrameworkTests.feature的属性将更改为copy if newer:

Then, after install the nuget package, the property of the file FrameworkTests.feature would be changed to copy if newer:

希望这会有所帮助.

这篇关于使用Nutarget使用.targets文件设置文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆