Search code examples
visual-studiodeploymentbuildmsbuildmsdeploy

Exclude `.js` files but not '.min.js' files from MSBuild publish


Using Visual Studio and MSBuild I would like to be able to exclude all .js files and include all .min.js files in my deployments.

I know this can be achieved using the file properties in visual studio, but this is not an option as there are far too many files.


I have the following PublishProfile in my Visual Studio project. Everything works just fine apart from the <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>

Can this be achieved using the PublishProfile? If so, how?


Solution

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

    Edit:

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