How can I exclude the bin folder from Visual Studio publish? - c#

I want to exclude the bin folder from being copied to the publish directory.
Using Visual Studio 2015, Asp.Net Web Application Project
Below is my .pubxml file. Notice that "bin" is listed inside the ExcludeFoldersFromDeployment element. All of the other folders listed there are excluded as expected, but this bin folder is still copied!
The bin directory is different I imagine as it is not part of the project, but part of the build output. My build output path is pointing to a different directory than my publish and working correctly. why is the bin still being copied to the publish output directory?? can it be excluded??
<?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>
<ExcludeFilesFromDeployment>Web.config;Default.aspx;packages.config</ExcludeFilesFromDeployment>
<ExcludeFoldersFromDeployment>bin;js;TestingUtils;TestPages;UI</ExcludeFoldersFromDeployment>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>C:\website</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
</PropertyGroup>
</Project>

you can use the following tag in your xml publish profile:
<ExcludeFoldersFromDeployment>bin<

I just inserted <ExcludeFoldersFromDeployment>bin</ExcludeFoldersFromDeployment> in my publish profile and it didnt include bin folder

Related

Nuget package.targets not able to find files in copy task

Background: I have a library that's meant to be used with a C# game engine. That game engine has an editor that picks up plugins via a Plugins/ directory of the project (is not included in the build output). In order to make life easy for users I want to package my library and the associated editor plugins packaged and deployed together in a single nuget file. This way users don't have to manually manage the versions of two different sets of files.
Problem:
Nuget no longer has the ability to copy content files over by default. Based on searching the only way to accomplish this is with tasks set up in an msbuild package.targets file. So I have the following in my csproj
<ItemGroup>
<Content Include="../Parme.Frb.Example/Plugins/**/*">
<Pack>true</Pack>
<PackagePath>content\Plugins</PackagePath>
</Content>
<Content Include="Parme.Frb.targets">
<Pack>true</Pack>
<PackagePath>build</PackagePath>
</Content>
</ItemGroup>
The Parme.Frb.Example/Plugins folder contains the plugin files I want to include in the nuget file. My Parme.Frb.targets msbuild file contains:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<GluePluginFiles>$(MSBuildThisFileDirectory)..\content\Plugins\**\*</GluePluginFiles>
</PropertyGroup>
<Target Name="CopyGluePlugin" BeforeTargets="PreBuildEvent">
<Copy SourceFiles="$(GluePluginFiles)"
DestinationFolder="$(ProjectDir)Plugins\%(GluePluginFiles.RecursiveDir)"
SkipUnchangedFiles="true" />
</Target>
</Project>
The intention is that it will copy all files from the <nuget-cache>/<package>/content/Plugins directory and recursively copy it tho the project's Plugins/ directory.
When I look at the built nuget file I see all the included content files
So I load this nuget file into a blank project and run a build, which produces the following errors:
Build started 4/12/2021 4:56:42 PM.
Logging verbosity is set to: Normal. 1>Project "C:\Users\me\RiderProjects\NugetTest\NugetTest\NugetTest.csproj" on node 1 (build target(s)).
1>C:\Users\me\.nuget\packages\parme.frb\0.8.3-test10\build\Parme.Frb.targets(7,9): error MSB3030: Could not copy the file "C:\Users\me\.nuget\packages\parme.frb\0.8.3-test10\build\..\content\Plugins\**\*" because it was not found.
1>Done Building Project "C:\Users\me\RiderProjects\NugetTest\NugetTest\NugetTest.csproj" (build target(s)) -- FAILED.
Build FAILED.
"C:\Users\me\RiderProjects\NugetTest\NugetTest\NugetTest.csproj" (build target) (1) ->
(CopyGluePlugin target) ->
C:\Users\me\.nuget\packages\parme.frb\0.8.3-test10\build\Parme.Frb.targets(7,9): error MSB3030: Could not copy the file "C:\Users\me\.nuget\packages\parme.frb\0.8.3-test10\build\..\content\Plugins\**\*" because it was not found.
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.33
However, files most definitely do exist in this directory:
PS C:\Users\me> ls C:\Users\me\.nuget\packages\parme.frb\0.8.3-test10\build\..\content\Plugins
Directory: C:\Users\me\.nuget\packages\parme.frb\0.8.3-test10\content\Plugins
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 4/12/2021 4:56 PM Parme.Frb.GluePlugin
I've tried reorganizing the nuget package to remove the .., I've tried removing the GluePlugins. part of the RecursiveDir (which most tasks don't seem to have but that causes a different error`, etc.. I've tried a ton of stuff and can't get this to work.
Does anyone have any idea on how I can get this copying, without manually specifying each file?
I don't know specifically why this works, but after a lot of trial and error I got it working via:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<GluePluginFile Include="$(MSBuildThisFileDirectory)..\content\Plugins\**\*" />
</ItemGroup>
<Target Name="CopyGluePlugin" BeforeTargets="PreBuildEvent">
<Message Importance="normal" Text="Copying Glue plugin files" />
<Copy SourceFiles="#(GluePluginFile)"
DestinationFolder="$(ProjectDir)\Plugins\%(RecursiveDir)"
SkipUnchangedFiles="true" />
</Target>
</Project>

Create folder while publishing with MSBuild

I am Trying to create a Temp folder while doing the publish of my Web Application Project with file system location in Visual Studio 2015.I have tried adding the code below in .csfile but its not Creating folder in the target location of the Publish Directory.If given Static location to create folder its working fine.How can get Publish Directory dynamically?
<Target Name="CustomCollectFiles" AfterTargets="Build">
<MakeDir Directories="$(PublishDirectory)$(OutputDirectoryTemp)" />
</Target>
<PropertyGroup>
<OutputDirectoryTemp>\Temp\</OutputDirectoryTemp>
</PropertyGroup>
Create folder while publishing with MSBuild
You need to declare attributes PublishDirectory by creating an element with the attribute name as a child of the PropertyGroup element, because there is no such MSBuild Macros for this, you can check the Common Macros for Build Commands and Properties.
As test, I set PublishDirectory to the path D:\Publish:
<PropertyGroup>
<PublishDirectory>D:\Publish</PublishDirectory>
</PropertyGroup>
Then I add a output command line <Message Test="xxx" /> in the target to output the content of $(PublishDirectory)$(OutputDirectoryTemp):
<Target Name="CustomCollectFiles" AfterTargets="Build">
<MakeDir Directories="$(PublishDirectory)$(OutputDirectoryTemp)" />
<Message Text="$(PublishDirectory)$(OutputDirectoryTemp)">
</Message>
</Target>
<PropertyGroup>
<OutputDirectoryTemp>\Temp\</OutputDirectoryTemp>
</PropertyGroup>
<PropertyGroup>
<PublishDirectory>D:\Publish</PublishDirectory>
</PropertyGroup>
In the output window, you will see following log:
And the folder Temp would be created:
If you have already defined the variable of PublishDirectory, you can try to use the output command line <Message Test="xxx" /> check if the path is correct.
Update:
I will like to get the target location of the Publish Directory dynamically not static or hard coded
Since you want to get the target location of the Publish Directory dynamically, as we know, the location of the Publish Directory was stored in the .pubxml file, in the node <publishUrl>D:\Publish\Test</publishUrl>, to get this value dynamically, we could use $(publishUrl) to get this value in the target, However, the publishing process is after the build, we could not get this value in the build process, so we need to change the order of this target from AfterTargets="Build" to AfterTargets="PipelineTransformPhase". The target should be:
<Target Name="CustomCollectFiles" AfterTargets="PipelineTransformPhase">
<MakeDir Directories="$(publishUrl)$(OutputDirectoryTemp)" />
<Message Text="$(publishUrl)$(OutputDirectoryTemp)" />
</Target>
<PropertyGroup>
<OutputDirectoryTemp>\Temp\</OutputDirectoryTemp>
</PropertyGroup>
In this case, when you publish your project to the system location, the publish directory will be stored in the publishUrl, we could get it in that target.
Hope this helps.
A little irrelevant at this point in time, but I think it may help someone like me. Whenever I searched how to dynamically add the folders and files within the same project in my publish profile in Azure DevOps, I landed on this question. So, I decided to put my answer here if it can help someone.
MSBuild expects you to make sure that you include certain files into the project file if you want Azure to deploy them during a git deployment. It is very frustrating, especially when you're using an external tool to copy files into a certain folder.
Wouldn’t it be nice if Visual Studio and (therefore) MSBuild just recursively included and deployed a folder of files within the same project without you having to define it in the publish profile?
You have to hand-edit the .csproj/.vbproj file (using Notepad++), but all you need to do is use a wildcard in the Include statement:
MyProject.Web.vbproj
<!-- double wildcards include all files and folders -->
<Content Include="folder\**" />
<!-- You can also filter certain type of files (e.g. .js files) -->
<Content Include="folder\*.min.js" />
It helped me tremendously.
Try specifying your PropertyGroup before your target, to ensure it has been created.
Also check the value of PublishDirectory to make sure it is a valid path - I suspect it may come with a "\" on the end of it, so you're ending up with two slashes

Problems building web deploy package

Using visual studio I was able to configure a profile to create a web deploy package zip. I named the profile "deploy". I want to be able to do this from CI. This is the command line I have so far. I've been throwing every combination of options I've seen in the dozen blog posts out there:
msbuild HelloBeanstalk4.csproj /p:DeployOnBuild=true /p:PublishProfile=deploy /p:Configuration=Release;Platform="Any CPU" /p:PackageLocation="D:\x.zip" /p:OutputPath="d:" /p:DeployTarget=deploy /tv:14.0
When I run this it just spits out the contents of the /bin as well as a _PublishedWebsites directory with the site files in it. It doesn't make a zip file. I am using VS2013 to build either a 4.0 or 4.5 site.
What am I doing wrong?
In Visual Studio what is the file path for the setting Publish | Connection | Package Location? Mine is set to obj\Release\Package\package.zip
When you Publish in Visual Studio does it create the zip file in that location?
When I run the command in the folder with the .csproj it builds the package zip in the location I specified in the profile:
msbuild /p:DeployOnBuild=true /p:PublishProfile=deploy
UPDATE:
Using VS 2013 Pro, below are the contents of my ./Properties/PublishProfiles/deploy.pubxml file:
<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>False</ExcludeApp_Data>
<DesktopBuildPackageLocation>obj\Release\Package\package.zip</DesktopBuildPackageLocation>
<PackageAsSingleFile>true</PackageAsSingleFile>
<DeployIisAppPath />
<PublishDatabaseSettings>
<Objects xmlns="">
...
</Objects>
</PublishDatabaseSettings>
</PropertyGroup>
<ItemGroup>
<MSDeployParameterValue Include="$(DeployParameterPrefix)DefaultConnection-Web.config Connection String" />
</ItemGroup>
</Project>
Compared to your pubxml file any notable differences?

Msbuild for copying assemblies to a few folders of applications?

I have a VS solution that contains a few applications and public APIs to be published along with shared libraries. I have some shallow experience in crafting msbuild file like this one.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TPath>C:\Program Files (x86)\MSBuild\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks</TPath>
<ETPath>C:\Program Files (x86)\MSBuild\ExtensionPack\4.0\</ETPath>
</PropertyGroup>
<ItemDefinitionGroup />
<ItemGroup>
<SolutionFile Include="MyApplications.sln" />
</ItemGroup>
<Target Name="Build" Outputs="#(CollectedBuildOutput)">
<MSBuild Projects="#(SolutionFile)" Targets="Rebuild" BuildInParallel="True"
Properties="BuildingSolutionFile=true; Configuration=$(Configuration); Platform=$(Platform); TargetFrameworkVersion=$(TargetFrameworkVersion); WarningLevel=3"
SkipNonexistentProjects="%(ProjectReference.SkipNonexistentProjects)">
<Output TaskParameter="TargetOutputs" ItemName="CollectedBuildOutput"/>
</MSBuild>
</Target>
</Project>
Then I run
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe MyProjects.msbuild /p:outdir=C:\VSProjectsRelease\MyApplications\Release\
So all assemblies will go to the same directory. So far so good. However, if I want to zip files for each application, or harvest files for Wix setup projects, troubles will emerge.
For example, in MyApplications.sln, I have 10 projects, 3 of which are applications say AppA, AppB and AppC.
I would like to run a single msbuild file which will create 3 folders of applications, and have assemblies copied to there without explicitly defining dependencies since Sln and csproj files already have the knowledge. And I would want msbuild will build each project only once. How to do this?

adding extra files to published MVC API project

I am trying to add an extra XML file to a publishing process. I have a MVC API project which also has another project (V1.0) for controllers. We are using the self documenting help functionality which creates the .XML files for each project. When building on the local machine it all works but when publishing (with wizard) it will not include this file.
I have been trying to update the publish profile (.pubxml) file as described here:
http://www.asp.net/mvc/tutorials/deployment/visual-studio-web-deployment/deploying-extra-files
but without success. I can see the following is happening:
I do a clean to ensure nothing hanging around.
I publish with wizard
I can see in apiproject\bin\ there are all the files including the apiprojectv1 xml and dll files
I can see in apiproject\obj\x86\Release\AspnetCompileMerge\Source\bin it has the apiprojectv1 dll but not the xml file
I can see the same as above in apiprojet\obj\x86\Release\AspnetCompileMerge\TempBuildDir\bin
I can see the same as above in apiproject\obj\x86\Release\Package\PackageTmp\bin
I am not sure why the file is not being copied across. This is my full pubxml file:
<?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>
<SiteUrlToLaunchAfterPublish />
<publishUrl>\\myserver\wwwroot\apiproject</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="..\bin\apiprojectv1.XML" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension) </DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
</Project>
EDIT
I had forgot one major part, to put the below at the bottom of the pubxml file:
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
I do not get the file, but now get an error regarding the file not being found, (which I can now debug).
I had missed two things:
The second property group to actually tell it to perform the action.
The path was not right, had to use project directory path
Now looks like this and works:
<?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>
<SiteUrlToLaunchAfterPublish />
<publishUrl>\\myserver\wwwroot\apiproject</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="$(MSBuildProjectDirectory)\bin\apiprojectv1.XML" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension) </DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
</Project>

Categories