【问题标题】:Use .targets to replace file in NuGet package使用 .targets 替换 NuGet 包中的文件
【发布时间】:2021-05-04 15:43:08
【问题描述】:

我有一个 NuGet 包,它依赖于另一个 NuGet 包,但我想用我的一个替换一个非托管 DLL。这很好用,但现在我正在尝试使用 .targets 文件添加对 x86 和 x64 的支持。当我的目标应用程序构建时,它会拉取原始版本,而不是替换。

这是我的 .nuspec:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>MyPackage</id>
    <version>2.0.0</version>
    <dependencies>
      <group targetFramework=".NETFramework4.5.2">
        <dependency id="YourPackage" version="1.0.0" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="MyPackage.targets" target="build\net452" />
    <file src="..\lib\x86\thepackage.dll" target="lib\net452\x86" />
    <file src="..\lib\x64\thepackage.dll" target="lib\net452\x64" />
  </files>
</package>

这是我的 .targets:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="PlatformCheck" BeforeTargets="InjectReference"
    Condition="(('$(Platform)' != 'x86') AND  ('$(Platform)' != 'x64'))">
    <Error  Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)' platform. You need to specify platform (x86 or x64)." />
  </Target>

  <Target Name="InjectReference" AfterTargets="ResolveAssemblyReferences">
    <ItemGroup Condition="'$(Platform)' == 'x86' or '$(Platform)' == 'x64'">
      <Reference Include="MyPackage">
        <HintPath>$(MSBuildThisFileDirectory)$(Platform)\thepackage.dll</HintPath>
      </Reference>
    </ItemGroup>
  </Target>
</Project>

我做错了什么?

编辑:如果我更改 .nuspec...

<file src="..\lib\x86\thepackage.dll" target="lib\net452" />
<file src="..\lib\x64\thepackage.dll" target="lib\net452" />

...然后它会拉下我的版本,但将 x86 用于 x86 和 x64 构建。

【问题讨论】:

    标签: nuget


    【解决方案1】:

    我通过将文件放在构建文件夹中然后简单地复制目标文件来解决它。

    .nuspec:

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
      <metadata>
        <id>MyPackage</id>
        <version>2.0.0</version>
        <dependencies>
          <group targetFramework=".NETFramework4.5.2">
            <dependency id="YourPackage" version="1.0.0" />
          </group>
        </dependencies>
      </metadata>
      <files>
        <file src="MyPackage.targets" target="build\net452" />
        <file src="..\lib\x86\thepackage.dll" target="build\net452\x86" />
        <file src="..\lib\x64\thepackage.dll" target="build\net452\x64" />
      </files>
    </package>
    

    .目标:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
      <Target Name="PlatformCheck" BeforeTargets="InjectReference"
        Condition="(('$(Platform)' != 'x86') AND  ('$(Platform)' != 'x64'))">
        <Error Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)' platform. You need to specify platform (x86 or x64)." />
      </Target>
    
      <ItemGroup>
        <None Include="$(MSBuildThisFileDirectory)$(Platform)\thepackage.dll">
          <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    
    </Project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2013-06-13
      相关资源
      最近更新 更多