从MSBuild发布中排除`.js`文件而不是'.min.js'文件

时间:2014-08-27 10:48:52

标签: visual-studio deployment build msbuild msdeploy

使用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来实现吗?如果是这样,怎么样?

3 个答案:

答案 0 :(得分:10)

<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>

答案 1 :(得分:3)

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

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

注意:此代码段假定您的.js文件位于名为js的文件夹中。

答案 2 :(得分:0)

为我工作:

  1. 编辑 .csproj 文件
  2. 查找部分 MinifyJavaScriptAndCSS
  3. Exclude 标记中编辑属性 JS
  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" />