Ajax Minifier to Deploy Package .zip - msbuild

I'm using AjaxMin to minify my .js and .css files this is configured within my Release.pubxml file and it works fine as when I navigate to the Release/Package/PackageTemp folder the .js and .css have been minified correctly.
However when I navigate to the actual Package .zip file the .js and .css files have not been minified they are just the original files. I assume that the files in my PackageTemp are not replacing the Package deploy files and I'm not sure how to achieve this from within my .pubxml file. My .pubxml file currently looks this this:
<?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>Package</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>True</ExcludeApp_Data>
<DesktopBuildPackageLocation>C:\Documents\TestDeploy</DesktopBuildPackageLocation>
<PackageAsSingleFile>true</PackageAsSingleFile>
<DeployIisAppPath />
<PublishDatabaseSettings>
<Objects xmlns="" />
</PublishDatabaseSettings>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\AjaxMin.targets" />
<PropertyGroup>
<OnAfterPackageUsingManifest>
$(OnAfterPackageUsingManifest); Minify;
</OnAfterPackageUsingManifest>
</PropertyGroup>
<Target Name="Minify" AfterTargets="CopyAllFilesToSingleFolderForPackage">
<ItemGroup>
<JS Include="$(_PackageTempDir)\**\*.js" Exclude="$(_PackageTempDir)\**\*.min.js;" />
<CSS Include="$(_PackageTempDir)\**\*.css" Exclude="$(_PackageTempDir)\**\*.min.css" />
</ItemGroup>
<Message Text="**************MINIFYING**************" Importance="high"></Message>
<AjaxMin JsSourceFiles="#(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".js" CssSourceFiles="#(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".css" />
</Target>
<PropertyGroup>
<ExcludeFoldersFromDeployment>bin</ExcludeFoldersFromDeployment>
</PropertyGroup>
</Project>
Please could someone shed some light on how to replace the files in my deploy .zip file with the PackageTemp Files via my .pubxml file
Thanks

My error i just noticed i was using the OnAfterPackageUsingManifest option. For anyone that also faces this issue try using the OnBeforePackageUsingManifest option instead.

Related

Nuget targets, how to send a renamed content file to solution output folder?

I have created a Nuget package with a default json configuration file, 'config.json.pp'. On installation it is transformed and added as content; 'config.json'. I have also added a targets file in the build folder of the package, it renames the config file based on the assembly root namespace the package is installed in:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ConfigFile Include="config.json"/>
</ItemGroup>
<Target Name="CopyConfigFile" AfterTargets="Build;Rebuild">
<Copy SourceFiles="#(ConfigFile)" DestinationFiles ="#(ConfigFile->'$(OutDir)\$(RootNameSpace)%(Extension)')" />
</Target>
</Project>
When the assembly is build in a solution with more assemblies, it is not added to the output directory, only to the bin\$(configuration) folder of the assembly where the package is installed.
How can I add the file to the output directory of the main application of the solution?
You can use the link metadata to do this:
<ItemGroup>
<Content Include="config.json"
Link="$(RootNameSpace)%(Extension)"
CopyToOutputDirectory="PreserveNewest"
Visible="False"
Condition="Exists('config.json')" />
</ItemGroup>

Execute .bat file at end of VS2017 Asp.Net Core publish action?

In Visual Studio 2017 when publishing an Asp.Net Core Website using the File System publish method I want to trigger the execution of a .bat file after the publish operation copied the files to the output directory.
I have learned that the settings for the publish operation are stored by Visual Studio in the project's Properties/PublishProviles directory in a .pubxml file. So in may case the file is FolderProfile.pubxml and it currently looks like this:
<?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 https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<PublishProvider>FileSystem</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>False</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<PublishFramework>net461</PublishFramework>
<ProjectGuid>f58b5ca0-8221-4c97-aa6d-7fba93a3abeb</ProjectGuid>
<publishUrl>C:\inetpub\wwwGiftOasisResponsivePublished</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
</PropertyGroup>
</Project>
Based on the comments in the .pubxml file and additional research, it's my understanding this file is essentially an msbuild file and ultimately msbuild is used to perform the publish operation. msbuild files seem very flexible but more than a little complicated. I'm really struggling with this one.
If I had a batch file in the root of my project called finishPub.bat, how could I modify the above .pubxml file to cause the execute of the finishPub.bat file after the website has been copied to the output folder?
You can amend your publish profile with a custom target:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
...
</PropertyGroup>
<Target Name="ExecuteBatAfterPublish" AfterTargets="AfterPublish">
<Exec Command="example.bat" WorkingDirectory="$(publishUrl)" />
</Target>
</Project>

Creating a Release drop

So, I have used MSBuild but this was years ago.
I want to create a Release build for a solution where once built, it will copy all files into a variable set folder "ReleaseDrop" and zip up the contents.
Before zipping, I want to make sure it copies only the necessary files (i.e no pdb, no sln, no csproj, no .cs files (but .cshtml is allowed) or only certain directories and exclude other directories within a directory.
how can I do this?
This should be a start. It specifies a bunch of files to include in a release, copies them to a directory and zips them. For the zip part I used MSBuild Extension Pack since I have it installed anyway, but you could just as well use a prtable version of 7z or so and incoke it with the Exec task.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>
<!--default values for properties if not passed-->
<PropertyGroup>
<ProjectDir Condition="'$(ProjectDir) == ''">C:\Projects\MyProject</ProjectDir>
<ReleaseDrop Condition="'$(ReleaseDrop) == ''">c:\Projects\MyProject\ReleaseDrop</ReleaseDrop>
</PropertyGroup>
<!--build list of files to copy-->
<ItemGroup>
<SourceFiles Include="$(ProjectDir)\bin\*.exe" Exclude="$(ProjectDir)\bin\*test*.exe"/>
<SourceFiles Include="$(ProjectDir)\bin\*.cshtml" />
</ItemGroup>
<!--copy files-->
<Target Name="CopyFiles">
<MakeDir Directories="$(ReleaseDrop)" />
<Copy SourceFiles="#(SourceFiles)" DestinationFolder="$(ReleaseDrop)" />
</Target>
<!--after files are copied, list them then zip them-->
<Target Name="MakeRelease" DependsOnTargets="CopyFiles">
<ItemGroup>
<ZipFiles Include="$(ReleaseDrop)\*.*"/>
</ItemGroup>
<Zip ZipFileName="$(ReleaseDrop)\release.zip" Files="#(ZipFiles)" WorkingDirectory="$(ReleaseDrop)"/>
</Target>
</Project>
can be invoked like
msbuild <name of project file> /t:MakeRelease /p:ProjectDir=c:\projects

Working directory issue when importing msbuild file in another msbuild file

I am trying to specify some additional targets/tasks to an msbuild file by extending an existing msbuild file (a web applicartion .csproj file). The idea is to put configuration specific tasks in this "extended ms build file" and use this file in our build server (TeamCity). The way I tried to solve it at first was to add a folder "msbuildscripts" to my web project and put the extended ms build file there:
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="../My.Web.csproj" />
...more stuff...
</Project>
and then build this file using something like:
c:\myweb\msbuild.exe msbuildscripts/extended.msbuild.file.xml
Now, this wont work because when importing the original ms build file, that csproj file will be "executed" in the "wrong" folder (msbuildscripts), and the csproj-build-file wont find any of its referenced folders/items.
Is there any way to tell msbuild.exe to use a specific working directory? I know it is possible to solve this problem using an execute task, but that doesnt seem like a good solution.
Use MSBuild task like this:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MyBuild">
<ItemGroup>
<ProjectToBuild Include="../My.Web.csproj" />
</ItemGroup>
<Target Name="MyBuild">
<MSBuild Targets="Build" Projects="#(ProjectToBuild)"></MSBuild>
</Target>
</Project>

How do I use MSBuild on a C# project with no compilable code

I have a C# web app project which actually has no ASP.Net or C# in it. It's just a single html page with some Javascript, CSS, and a couple of images.
I want to use MSBuild to deploy a version of this app to an output folder with minified JS and CSS.
With the following code, I get an error "CSC: fatal error CS2008: No inputs specified." I'm guessing because the there is no actual C# code to compile but I'm not sure.
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<CssTidy>..\build_tools\csstidy.exe</CssTidy>
</PropertyGroup>
<PropertyGroup>
<DeploymentFolder>Test\</DeploymentFolder>
<SourceProject>..\..\Test\Test.csproj</SourceProject>
</PropertyGroup>
<Import Project="Common.Web.targets" />
<ItemGroup>
<CssFiles Include="..\..\Test\CSS\stylesheet.css" />
<ScriptFiles Include="..\..\Test\JavaScript\javascript.js"/>
</ItemGroup>
<Target Name="compress_css">
<Attrib Files="%(CssFiles.FullPath)" ReadOnly="false"/>
<Exec Command="$(CssTidy) %(CssFiles.FullPath) %(CssFiles.FullPath) --template=highest" />
</Target>
<Target Name="compress_js">
<Attrib Files="%(ScriptFiles.FullPath)" ReadOnly="false"/>
<JSCompress Files="%(ScriptFiles.FullPath)"></JSCompress>
</Target>
<Target Name="call_targets">
<CallTarget Targets="compress_css"/>
<CallTarget Targets="compress_js"/>
</Target>
</Project>
How can I accomplish this?
You could override the CoreCompile target and do nothing there:<Target name="CoreCompile" />. This will skip its activities and move on. You may have to override additional targets to avoid errors.
At the top of the file you have the DefaultTargets="Build"
Change "Build" to "call_targets" and you should be good to go.
What is inside "common.web.targets"? I assume that the error is generated from a target in that file (or another that it imports).
A quick fix for this would be to add a dummy page to the project. The build would work after that.