I use following config to unpack my files into one folder, and then rezip it into a single file:
<Target Name="Build_Package" DependsOnTargets="Build_Test">
<!-- Unzip artefacts to local workfolder -->
<unzip ZipFileName="$(MSBuildProjectDirectory)\MyZip.zip" TargetDirectory="$(MSBuildProjectDirectory)\temp\unzipped" />
<unzip ZipFileName="$(MSBuildProjectDirectory)\AnotherZip.zip" TargetDirectory="$(MSBuildProjectDirectory)\temp\unzipped" />
<!-- Zip files into one single package -->
<Zip Files="$(MSBuildProjectDirectory)\temp\unzipped"
WorkingDirectory="$(MSBuildProjectDirectory)\temp\work"
ZipFileName="$(MSBuildProjectDirectory)\temp\output\Output.zip" />
</Target>
This results in a zip file containing the subfolder unzipped which I want to remove. I´ve found the option RemoveRoot in older posts, but this is not working for me. I also can´t use the extension MSBuild.ExtensionPack.Compression.Zip.
Anyone an idea out there? Thank you! :)
Related
I want to add some files recursively to a single project that are located in folders inside my solution root.
Right now, I found a working solution to add those files by specifying each folder manually:
<ItemGroup>
<Content Include="..\Folder1\**\*">
<Link>Folder1\%(RecursiveDir)\%(Filename)%(Extension)</Link>
</Content>
<Content Include="..\Folder2\**\*">
<Link>Folder2\%(RecursiveDir)\%(Filename)%(Extension)</Link>
</Content>
<Content Include="..\Folder3\**\*">
<Link>Folder3\%(RecursiveDir)\%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>
As those can be a handfull folders and they can be different depending on my solution, I want to have an easy list to define the folder names.
Something like this:
<PropertyGroup>
<DefinedResourceFolders>Folder1;Folder2;Folder3</DefinedResourceFolders>
</PropertyGroup>
This would also allow me to input this as a property directly when calling msbuild to extend the files that are going to the build output, maybe.
I tried to look into the MSBuild Batching documentation and several other online sources, but I could not figure it out. Most batching examples I found were working with Targets, not Includes into the solution items.
Is this possible? How would I define the <ItemGroup> for the content then?
P.S.: I don't care about wildcard issues with .csproj files when new files are added, etc. This will either be a single "Resources" project only containing those displayed files, or I am using my external .props file that I am importing into each .csproj file anyway.
Suppose you want to include all files in folders root/Folder1, root/Folder2 and root/Folder3 recursively.
This is how the builds (both VS and CLI) do what you want. However, VS will not show the files as part of the project.
<Project Sdk="Microsoft.NET.Sdk" InitialTargets="__AddBatchedContent;$(InitialTargets)">
<!-- the usual properties -->
<!-- You could of course define the folder array directly in an Item,
but this is what you wanted :-) -->
<PropertyGroup>
<Lookups>Folder1;Folder2;Folder3</Lookups>
</PropertyGroup>
<ItemGroup>
<LookupDir Include="$(Lookups)" />
</ItemGroup>
<Target Name="__AddBatchedContent">
<ItemGroup>
<!-- save the original source folder of the globbed file name in custom
metadata "Folder" so we can use it later as its output base folder -->
<__BatchedFiles Include="..\%(LookupDir.Identity)\**\*" Folder="%(LookupDir.Identity)" />
<Content Include="#(__BatchedFiles)" Link="%(Folder)\%(RecursiveDir)\%(Filename)%(Extension)" CopyToOutputDirectory="Always" />
</ItemGroup>
</Target>
</Project>
Note that if you want to put this in a .targets file to use this technique in multiple projects, you should replace the ..\ with $(MSBuildThisFileDirectory)..\ so that the path is relative to the (known) location of the .targets file instead of the (unknown) location of the importing project.
Thanks to #Ilya Kozhevnikov for the basis of this answer.
You are not the only one who would like this to be simpler :-): https://github.com/dotnet/msbuild/issues/3274
I'm trying to package a few files into a NuGet package, but the issue is that all of the files are sent to the "content" folder within the NuGet package by default when packaged. Normally this is okay, but for the JSON files I have in "ABCJsons" I'd like them to be sent to "content/NewFolderName".
In my example below, the first block is my AbcToolTester, which has all of its project files files being successfully sent to the content directory in the NuGet package. The second block, is where I attempted to copy all the json files with ABCLibrary (ABCLibrary has subfolders where the actual Jsons are located) to the destination folder "ABCJsons". I thought this would do the trick, but unfortunately the ABCJson files just get sent to the content folder along with all the other files.
<ItemGroup>
<Content Include="..\AbcToolTester\bin\Debug\netcoreapp3.1\**" Exclude="..\AbcToolTester\bin\Debug\netcoreapp3.1\*.pdb">
<IncludeInPackage>true</IncludeInPackage>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<ItemGroup>
<Target Name="CopyABCLibrary" AfterTargets="AfterBuild">
<ItemGroup>
<ABCJsonsInclude="..\..\tests\ABCLibrary\**\*.*"/>
</ItemGroup>
<Copy SourceFiles="#(ABCJsons)" DestinationFolder="$(TargetDir)\ABCJsons" SkipUnchangedFiles="true" />
</Target>
It's hard to tell if the NuGet package you are creating is actually dependent on AbcToolTester project because there are easier ways to package that. That's another question though.
For your actual issue, you can simplify the copying process while also telling it where to pack the files. Replace your CopyABCLibrary target with this:
<ItemGroup>
<Content Include="..\..\tests\ABCLibrary\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>ABCJsons\%(RecursiveDir)</PackagePath>
<!-- This line hides the items from showing in the solution explorer -->
<Visible>false</Visible>
</Content>
</ItemGroup>
This will put all those files into the root of the nuget package into the ABCJsons folder and preserve the directory structure. Change the path accordingly to put it somewhere else.
We currently publish our web app vis MSDeploy, to create a .ZIP file (file deployment).
I have some .config files that I want to apply transforms to when we publish. So I've created an appSettings.test.config and an appSettings.live.config.
To facilitate this, I have added this to the csproj of our web app, which applies the transforms:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="ApplyConfigTransforms" BeforeTargets="TransformWebConfigCore">
<TransformXml Source="appSettings.config" Transform="appSettings.$(Configuration).config" Destination="appSettings.config" />
<TransformXml Source="connectionStrings.config" Transform="connectionStrings.$(Configuration).config" Destination="connectionStrings.config" />
</Target>
This hooks in to the TransformWebConfigCore target and does the work. BUT, it applies the transforms to the appSettings file in my web folder (as expected from the destination). What I want to do is apply the transform to the file in the output pacakge.
How can I do this? I don't know how to get a handle on the temporary folder that is created when you publish..
Don't you just hate it when you find the answer yourself about 10 mins after posting.
I hacked the destination, which fixed the issue. Would be nice if I had this in a property, but I couldn't find one to use, so I did:
<Target Name="ApplyConfigTransforms" AfterTargets="CopyAllFilesToSingleFolderForMsdeploy">
<TransformXml Source="appSettings.config" Transform="appSettings.$(Configuration).config" Destination="obj\Test\Package\PackageTmp\appSettings.config" />
<TransformXml Source="connectionStrings.config" Transform="connectionStrings.$(Configuration).config" Destination="obj\Test\Package\PackageTmp\connectionStrings.config" />
</Target>
Note, I also changed the target, to make sure it happens just after all my files have been copied
I have an MVC application which I have got working on Azure apart from getting the published .cspkg file to include css/jscript that is created in an afterbuild process (this works if I publish to a normal server which isn't using Azure).
In the afterbuild process I minify and merge files then add them to a deploy zip:
<PackageLocation>..\Deploy\Website.zip</PackageLocation>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
What MSBuild code do I need to change in order to do the same task but adding to the cspkg instead?
Here is how I just did it. In this example I have a .csproj file that is part of an Azure solution and the dll produced by my C# project needs a particular Xml file to live right next to it in the deployment. Here are some msbuild fragments from my .csproj file that show the technique. You can place all of this code below the import of Microsoft.CSharp.targets in your .csproj file.
<!-- Identify the Xml input file that must be deployed next to our dll. -->
<ItemGroup>
<SpecialXmlFileItem Include="c:\temp\MySpecialFile.xml" />
</ItemGroup>
<PropertyGroup>
<!-- In my case I needed the as-deployed Xml filename to be fixed and yet I wanted it to be possible
to provide any filename at all to be provided as the source. Here we are defining the fixed,
as-deployed filename. -->
<AsDeployedXmlFilename>MyServiceStorageConfig.xml</AsDeployedXmlFilename>
<!-- Wire our own AddFilesToProjectDeployment target into the GetCopyToOutputDirectoryItems
target. That target is evaluated not only as part of normal .csproj evaluation, but also as part
of .ccproj evaluation. It is how the .ccproj manages to interrogate your dll producing projects
about all of the project files that need to be packaged. -->
<GetCopyToOutputDirectoryItemsDependsOn>
AddFilesToProjectDeployment;
$(GetCopyToOutputDirectoryItemsDependsOn)
</GetCopyToOutputDirectoryItemsDependsOn>
</PropertyGroup>
<Target Name="AddFilesToProjectDeployment">
<Error Condition="!Exists('#(SpecialXmlFileItem)')"
Text="The all important and very special XML file is not found: %(SpecialXmlFileItem.ItemSpec)" />
<ItemGroup>
<ContentWithTargetPath Include="#(SpecialXmlFileItem->'%(FullPath)')">
<!-- In my case I wanted to deploy my xml file right next to my .dll, so I included no relative
path information in the below value of TargetPath, just the simple filename. But, I think if you
included relative path information in the below value that it would be preserved in the deployment. -->
<TargetPath>$(AsDeployedXmlFilename)</TargetPath>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</ContentWithTargetPath>
</ItemGroup>
</Target>
-Bern McCarty
I think this is just a question of timing... make sure the files get combined, minified, and placed into build before the publishing (packaging) step happens.
Sorry I don't have more details; I've never tried to do this sort of thing.
I'm using a Web Deployment Project 2008 to build my web application. I'd like to exclude the contents of several folders from the build but keep the blank directory itself. However, if I do this
<ExcludeFromBuild Include="$(SourceWebPhysicalPath)\ImageCache\**\*.*" />
it will exclude the ImageCache directory itself. So how do I keep the directory? Thanks in advance?
I'm afraid you must do this in two lines :
<ExcludeFromBuild Include="$(SourceWebPhysicalPath)\ImageCache\**\*.*" />
<IncludeFromBuild Include="$(SourceWebPhysicalPath)\ImageCache\" />
But that could not work because The "Copy" task does not support copying directories.
Thus, what I suggest is that you exclude the files like you did and create an empty directory in the AfterMerge target :
<ItemGroup>
<ExcludeFromBuild Include="$(SourceWebPhysicalPath)\ImageCache\**\*.*" />
<ItemGroup>
<...>
<Target Name="AfterMerge">
<MakeDir Directories="$(SourceWebPhysicalPath)\ImageCache" />
</Target>