我需要在我的.NET Framework控制台应用程序的.csproj文件中使用<attrib>
[Docs]元素。
它嵌套在<Target Name="BeforeBuild">
元素中,因为我需要在构建之前编辑几个文件的属性,这里是.csproj的完整代码:
<Target Name="BeforeBuild">
<Attrib Files="App.config" ReadOnly="false" />
<Attrib Files="Ocelot.json" ReadOnly="false" />
<Attrib Files="OcelotLogging.json" ReadOnly="false" />
</Target>
当代码写成这样时,编辑器给我这个错误:Task 'Attrib' is not defined
。
我试过了什么?
<UsingTask>
元素,其中参数是NAnt.Core NuGet包的路径。整个代码如下所示:
<UsingTask TaskName="Attrib" AssemblyFile="C:\Users\UserName\.nuget\packages\nant.core\0.92.0\lib\net40\NAnt.Core.dll" />
<Target Name="BeforeBuild">
<Attrib Files="App.config" ReadOnly="false" />
<Attrib Files="Ocelot.json" ReadOnly="false" />
<Attrib Files="OcelotLogging.json" ReadOnly="false" />
</Target>
但错误并没有消失。当我尝试编译应用程序时,我得到以下错误:The "Attrib" task could not be loaded from the assembly C:\Users\UserName\.nuget\packages\nant.core\0.92.0\lib\net40\NAnt.Core.dll. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
我认为你正在混合NAnt和MSBuild任务。
NAnt任务写在.build文件上,并通过将此文件传递给NAnt可执行文件来调用,如here所解释的那样。你用loadtasks
加载它们。
但是,可以在.csproj文件中使用MSBuild tasks。您使用usingtask
与他们合作。
所以在你的情况下,你可以做的是使用msbuildtasks package,它也有attrib
任务。
安装包:
可以从发布部分下载最新版本。 https://github.com/loresoft/msbuildtasks/releases
也可以通过软件包名称> MSBuildTasks在nuget.org上获得MSBuild社区任务库。
要安装MSBuildTasks,请在程序包管理器控制台中运行以下命令
PM> Install-Package MSBuildTasks
安装还确保您可以使用csproj中的任务而无需使用usingtask
,因此:
<Target Name="BeforeBuild">
<Attrib Files="App.config" ReadOnly="false" />
<Attrib Files="Ocelot.json" ReadOnly="false" />
<Attrib Files="OcelotLogging.json" ReadOnly="false" />
</Target>
请注意,它们是使用MSBuild执行此操作的其他方法,这只是最接近您编写的内容。