用于删除文件的 MSBuild 任务语法

发布于 2024-08-05 08:15:45 字数 132 浏览 5 评论 0原文

我正在尝试编写一个 MSBuild 任务,从我的生产构建脚本上的 bin 文件夹中删除 Obj 目录和 PDB,但似乎无法使其正常工作。

有没有人有执行此操作或类似操作的示例,或者使用 MSBuild 删除文件和目录的简单示例的链接?

I'm trying to write a MSBuild Task that deletes the Obj directory and PDBs from my bin folder on my production build scripts and can't seem to get it to work right.

Does anyone have an example where they do this or similar, or a link to a simple example of removing files and a directory with MSBuild?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小苏打饼 2024-08-12 08:15:45

您可以先删除这些目录中的文件,然后删除目录本身

<Target Name="SomeTarget">
    <ItemGroup>
        <FilesToDelete Include="Path\To\Obj\**\*"/>
    </ItemGroup>   
    <Delete Files="@(FilesToDelete)" />   
    <RemoveDir Directories="Path\To\Obj\" />
</Target>

You can delete the files in those directories first and then the dir itself with

<Target Name="SomeTarget">
    <ItemGroup>
        <FilesToDelete Include="Path\To\Obj\**\*"/>
    </ItemGroup>   
    <Delete Files="@(FilesToDelete)" />   
    <RemoveDir Directories="Path\To\Obj\" />
</Target>
若言繁花未落 2024-08-12 08:15:45

如果您要删除整个目录,则需要 RemoveDir 任务:

<RemoveDir Directories="Path/To/Obj" />

如果您想从 bin 中删除 PDB 文件,您需要 删除任务:

<Delete Files="Path/To/Bin/MyApp.pdb" />

请注意,您不能在删除任务中使用通配符,因此如果您有多个 pdb 文件,则必须提供 ItemGroup 作为参数。

If you're looking to delete an entire directory you require the RemoveDir task:

<RemoveDir Directories="Path/To/Obj" />

And if you're wanting to delete the PDB files from bin you'll want the Delete task:

<Delete Files="Path/To/Bin/MyApp.pdb" />

Note that you cannot use wildcards in the Delete task, so if you have multiple pdb files you're have to provide an ItemGroup as an argument.

太阳男子 2024-08-12 08:15:45

为可能遇到我遇到的同样问题的其他人发帖。

删除任务无法删除只读文件,而我需要能够做到这一点,因为当 MSBuild 从 TFS 获取最新文件时,这些文件被标记为只读。我使用 EXEC 命令删除只读文件:

<ItemGroup>
    <FileToDelete Include="c:\temp\fileToDelete.txt"/>
</ItemGroup>
<Exec Command="del /F /Q "@(FileToDelete)""/>

Posting for others that might have ran into the same problem I was having.

The Delete task cannot delete readonly files, which I needed to be able to do, as when MSBuild gets latest from TFS, the files are marked as readonly. I used the EXEC command to delete readonly files:

<ItemGroup>
    <FileToDelete Include="c:\temp\fileToDelete.txt"/>
</ItemGroup>
<Exec Command="del /F /Q "@(FileToDelete)""/>
海未深 2024-08-12 08:15:45

只要您必须处理单个目录,发布的答案就可以工作。如果您碰巧有嵌套文件夹,RemoveDir 将失败,并出现 Directory notempty 错误。

一种稍微通用的方法也可以处理嵌套文件夹:

<Target Name="CleanOutDir">
    <ItemGroup>
        <FilesToClean Include="$(OutDir)\**\*.*" />

        <!-- Bit of .Net to get all folders and subfolders -->
        <FoldersToClean Include="$([System.IO.Directory]::GetDirectories("$(OutDir)"))" />
    </ItemGroup>

    <Delete Files="@(FilesToClean)"/>
    <RemoveDir Directories="@(FoldersToClean)" />
</Target>

The posted answers will work as long as you have to deal with a single directory. If you happen to have nested folders, RemoveDir will fail with Directory not empty error.

A slightly generic approach takes care of nested folders as well:

<Target Name="CleanOutDir">
    <ItemGroup>
        <FilesToClean Include="$(OutDir)\**\*.*" />

        <!-- Bit of .Net to get all folders and subfolders -->
        <FoldersToClean Include="$([System.IO.Directory]::GetDirectories("$(OutDir)"))" />
    </ItemGroup>

    <Delete Files="@(FilesToClean)"/>
    <RemoveDir Directories="@(FoldersToClean)" />
</Target>
倾听心声的旋律 2024-08-12 08:15:45

这段代码太难看了,应该附带一个晕机包。 ;-) 但它很快,因为它不构建要删除的文件列表等。

<Target Name="DeleteBuildFolder">
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
</Target>

需要多少个 RmDir 命令?足够了,一些 RmDir 命令返回“系统找不到指定的文件”而不是“目录不为空”。在我的机器上,如果 $(BuildFolder) 在 Windows 资源管理器中打开,则似乎需要另一个 RmDir。防病毒程序可能会影响 RmDir,就像它偶尔影响 Subversion 一样,但我宁愿拥有全面的 AV 保护,也不愿(错误)管理排除列表。

This code is so ugly it should come with an airsickness bag. ;-) But it is fast because it doesn't build a list of files to delete etc.

<Target Name="DeleteBuildFolder">
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
    <Exec Command="RmDir /S /Q "$(BuildFolder)"" />
</Target>

How many RmDir commands are needed? Enough so a few RmDir commands return "The system cannot find the file specified" instead of "The directory is not empty." On my machine it seems to take another RmDir if $(BuildFolder) is open in Windows Explorer. The antivirus program may affect RmDir like it occasionally does Subversion but I'd rather have blanket AV protection than (mis)manage an exclusion list.

眼中杀气 2024-08-12 08:15:45

也可以先从文件中删除只读属性,然后执行 msbuild 删除任务。

就像这样:

<Target Name="DeleteFiles">
 <Message Text="Delete File" Importance="high"/>
 <Attrib Files="$(FileToDelete)" ReadOnly="false" />
 <Delete Files="$(FileToDelete)" />
</Target>`

It is also possible to first remove the readonly property from the file and to execute the msbuild delete Task.

Like so:

<Target Name="DeleteFiles">
 <Message Text="Delete File" Importance="high"/>
 <Attrib Files="$(FileToDelete)" ReadOnly="false" />
 <Delete Files="$(FileToDelete)" />
</Target>`
_畞蕅 2024-08-12 08:15:45

在 Visual Studio 2013 中,将其添加到我的 .csproj 文件末尾 结束标记之前

<Target Name = "clean_folders" AfterTargets="Clean">
 <Exec Command = "rd /S /Q obj" />
 <Exec Command = "rd /S /Q bin" />
</Target>

起初它似乎不起作用,但我注意到 Visual Studio(或 R #,不确定)重新将 DesignTimeResolveAssemblyReferencesInput.cache 添加到 obj 文件夹,并且还重新添加了当前的 \bin 文件夹(我在不同的子文件夹中有不同的版本在 \bin 下)。它清除了其他所有内容,包括我从导入的 .csproj 文件中获得的 25 个其他构建配置(是的,我知道)。

如果您批量重建多个配置,请务必小心,因为它只会清除每次重建之前的所有工作,只留下最后一个配置。哎哟。

In Visual Studio 2013, added this to the end of my .csproj file just before the </Project> closing tag

<Target Name = "clean_folders" AfterTargets="Clean">
 <Exec Command = "rd /S /Q obj" />
 <Exec Command = "rd /S /Q bin" />
</Target>

At first it didn't appear to work but I noticed that Visual Studio (or R#, not sure) re-re-added DesignTimeResolveAssemblyReferencesInput.cache to the obj folder and it also re-added the current \bin folder (I have different builds in different subfolders under \bin). It cleaned away everything else, including the 25 other build configs I have from imported .csproj files (yes, I know).

Be careful if you Batch Rebuild more than one config as it just wipes all previous efforts on each rebuild leaving you with only the last one. Whups.

彻夜缠绵 2024-08-12 08:15:45

只是为了添加我发现的另一道皱纹。我正在使用 Visual Studio 2015。通过通配符删除的发布答案对我来说仍然很麻烦。 我怀疑通配符是在构建之前而不是之后评估的。这意味着如果您要删除的文件是在构建期间创建的,则不会发生删除。它还会导致奇妙的行为,即每次构建时都会进行删除,这使得测试这一切变得非常愉快。

我正在放弃通配符。对于我正在做的事情,我知道导致麻烦的文件,并且我对实际文件名进行硬编码(如果可以在项目文件内部调用)。

Just to add one more wrinkle that I've discovered. I'm using Visual Studio 2015. The posted answers that are deleting via wildcard are still troublesome for me. I suspect that the wildcards are evaluated before the build, not after. That means the deletions won't happen if the files you want to delete are created during the build. It also leads to wonderful behavior where the delete works every second time you build, which makes this all very enjoyable to test.

I'm giving up on wildcards. For what I'm doing I know the files that are causing trouble and I'm hard-coding (if it can be called that inside of a project file) the actual file names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文