5

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?

4
  • I can help you with the MSBuild if you like but let me suggest publish ignore blogs.msdn.com/b/webdev/archive/2013/08/22/…. Can you take a look and let me know if you still need the MSBuild publish profile solution? Commented Aug 28, 2014 at 0:03
  • Thanks this looks great, I just need to figure out how I can ignore .js files and not .min.js files using the syntax available. Does publish ignore follow gitignore syntax strictly? Commented Aug 29, 2014 at 8:37
  • Seems not 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.<ExecuteInstantiatedTask>d__20.MoveNext() Commented Aug 29, 2014 at 8:55
  • Sorry I forgot I never implemented support for !. I've opened an issue to track it github.com/ligershark/publish-ignore/issues/8. I've also answered below. Commented Sep 9, 2014 at 18:24

3 Answers 3

10
+100
<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>
Sign up to request clarification or add additional context in comments.

10 Comments

sorry I didn't have time to test before the bounty ran out. I have doubled the bounty and will reward within 24h
Strange, this conflicts with this line <ExcludeFoldersFromDeployment>bin;</ExcludeFoldersFromDeployment>. When using your code the bin folder is still deployed.
@Blowsie weird, works fine with an empty web app project, no bin deployed, just the .min.js. Do you have DeleteExistingFiles set? Exclude*FromDeployment is even better, no need to muck about with events, see the edit.
Updated example throws an error The attribute "Remove" in element <Content> is unrecognized
@Blowsie there is no Remove attribute in the second non-event based one.
|
3

If you want to exclude files you can place the files to be excluded in the the ExcludeFromPackageFiles item group. In your case you want to take all .js files and exclude all but those that are *.min.js. To do that in your .pubxml file add the following in your .pubxml file .

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

Note: this snippet assumes that your .js files are in a folder named js.

2 Comments

Thanks, another quick question. Is there a target for when a file-system web publish has finished?
Is this still applicable with VS 2015 when publishing to Azure? I'm finding the non-minified js files are in the temp folder created before pushing to Azure, and then also end up pushed to Azure as well. Js files for my project are in [solution folder][project folder]\wwwroot\js. I've tried paths such as "wwwroot\js\" and just "js\"
0

work to me:

  1. Edit .csproj file
  2. Find section MinifyJavaScriptAndCSS
  3. Edit property Exclude in JS tag
  4. Add directory or files to ignore during publish
<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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.