【问题标题】:Exclude `.js` files but not '.min.js' files from MSBuild publish从 MSBuild 发布中排除“.js”文件,但不排除“.min.js”文件
【发布时间】:2014-10-20 22:08:33
【问题描述】:

使用 Visual Studio 和 MSBuild,我希望能够在我的部署中排除所有 .js 文件并包含所有 .min.js 文件。

我知道这可以使用 Visual Studio 中的文件属性来实现,但这不是一个选项,因为文件太多。


我的 Visual Studio 项目中有以下 PublishProfile。除了<ItemGroup>

之外,一切都很好
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <LastUsedBuildConfiguration>Delpoy-Static</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <SiteUrlToLaunchAfterPublish />
        <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
        <ExcludeApp_Data>True</ExcludeApp_Data>
        <publishUrl>\\***\wwwroot\***.com\static</publishUrl>
        <DeleteExistingFiles>False</DeleteExistingFiles>
    </PropertyGroup>
    <!--This does not work, but gives the idea of what I want to achieve-->
    <ItemGroup>
        <Deploy Exclude="**\*.js" Include="**\*.min.js" />
    </ItemGroup>
</Project>

这可以使用PublishProfile 实现吗?如果是这样,怎么做?

【问题讨论】:

  • 如果您愿意,我可以在 MSBuild 方面为您提供帮助,但我建议您发布忽略 blogs.msdn.com/b/webdev/archive/2013/08/22/…。如果您仍然需要 MSBuild 发布配置文件解决方案,您可以看看并告诉我吗?
  • 谢谢,这看起来很棒,我只需要弄清楚如何使用可用的语法忽略 .js 文件而不是 .min.js 文件。发布忽略是否严格遵循 gitignore 语法?
  • 好像不是Error MSB4018: The "ReadPublishIgnoreFile" task failed unexpectedly. System.NotSupportedException: The ! operator is not currently supported in publish.ignore at InlineCode.ReadPublishIgnoreFile.Execute() in c:\Users\sblowes\AppData\Local\Temp\bzp34wjd.0.cs:line 89 at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.&lt;ExecuteInstantiatedTask&gt;d__20.MoveNext()
  • 对不起,我忘记了我从未实现过对 ! 的支持。我已经打开了一个问题来跟踪它github.com/ligershark/publish-ignore/issues/8。我也在下面回答了。

标签: visual-studio deployment build msbuild msdeploy


【解决方案1】:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <!-- ... -->
  </PropertyGroup>

  <Target Name="BeforeBuild">
    <ItemGroup>
      <Minified Include="**\*.min.js" />
      <Maxified Include="**\*.js" Exclude="@(Minified)" />
      <Content Remove="@(Maxified)" />
    </ItemGroup>
  </Target>
</Project>

编辑:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <!-- ... -->
  </PropertyGroup>

  <ItemGroup>
    <Minified Include="**\*.min.js" />
    <Maxified Include="**\*.js" Exclude="@(Minified)" />
  </ItemGroup>
  <PropertyGroup>
    <ExcludeFoldersFromDeployment>bin</ExcludeFoldersFromDeployment>
    <ExcludeFilesFromDeployment>@(Maxified);Web.config</ExcludeFilesFromDeployment>
  </PropertyGroup>
</Project>

【讨论】:

  • 对不起,在赏金用完之前我没有时间测试。我已将赏金翻倍,将在 24 小时内奖励
  • 奇怪,这与&lt;ExcludeFoldersFromDeployment&gt;bin;&lt;/ExcludeFoldersFromDeployment&gt;这一行冲突。使用您的代码时,仍会部署 bin 文件夹。
  • @Blowsie 很奇怪,适用于空的 Web 应用程序项目,没有部署 bin,只有 .min.js。你有DeleteExistingFiles 设置吗? Exclude*FromDeployment 更好,不用纠结事件,看编辑。
  • 更新后的例子抛出错误The attribute "Remove" in element &lt;Content&gt; is unrecognized
  • @Blowsie 在第二个非基于事件的属性中没有 Remove 属性。
【解决方案2】:

如果要排除文件,可以将要排除的文件放在 ExcludeFromPackageFiles 项目组中。在您的情况下,您想要获取所有 .js 文件并排除除 *.min.js 之外的所有文件。要在 .pubxml 文件中执行此操作,请在 .pubxml 文件中添加以下内容。

  <ItemGroup>
    <ExcludeFromPackageFiles Include="js\**\*.js" Exclude="js\**\*min*.js">
      <FromTarget>Project</FromTarget>
    </ExcludeFromPackageFiles>
  </ItemGroup>

注意:此 sn-p 假定您的 .js 文件位于名为 js 的文件夹中。

【讨论】:

  • 谢谢,又是一个简短的问题。文件系统网络发布完成时是否有目标?
  • 在发布到 Azure 时这是否仍然适用于 VS 2015?我发现未缩小的 js 文件位于推送到 Azure 之前创建的临时文件夹中,然后最终也推送到 Azure。我的项目的 Js 文件位于 [解决方案文件夹][项目文件夹]\wwwroot\js 中。我尝试过诸如“wwwroot\js\”之类的路径,而只是“js\”
【解决方案3】:

为我工作:

  1. 编辑.csproj文件
  2. 查找版块MinifyJavaScriptAndCSS
  3. JS 标签中编辑属性Exclude
  4. 添加要在发布期间忽略的目录或文件
<Target Name="MinifyJavaScriptAndCSS" AfterTargets="CopyAllFilesToSingleFolderForPackage" Condition="'$(Configuration)'=='Release'">
    <ItemGroup>

      <!-- Every .js file (exclude *.min.js and *.vsdoc.js files) -->
      <JS Include="$(_PackageTempDir)\**\*.js" Exclude="$(_PackageTempDir)\**\*.min.js;$(_PackageTempDir)\**\*vsdoc.js;" />

      <CSS Include="$(_PackageTempDir)\**\*.css" Exclude="$(_PackageTempDir)\**\*.min.css" />
    </ItemGroup>
    <AjaxMin JsKnownGlobalNames="jQuery,$" JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".js" CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".css" />
    <Message Text="[pcv] $(MSBuildProjectName) -&gt; Minified: @(JS)" Importance="high" />
    <Message Text="[pcv] $(MSBuildProjectName) -&gt; Minified: @(CSS)" Importance="high" />

【讨论】:

    猜你喜欢
    • 2017-06-08
    • 2015-11-10
    • 2012-09-11
    • 1970-01-01
    • 2016-02-13
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    相关资源
    最近更新 更多