Visual Studio always restoring because Directry.Build.props - .net-core

I created a Directory.Build.props file, with VersionSuffix contain the current Hour and Minutes:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VersionSuffix>$([System.DateTime]::Now.ToString(yyyy-MM-dd-HHmm))-$(Configuration)</VersionSuffix>
</PropertyGroup>
</Project>
Now, because this file changed every minute, the Visual Studio complete it auto-restore, and start it again, because after a minute the properties are different.
So, I added a Target to update the properties only before the Build:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Version" BeforeTargets="Build">
<PropertyGroup>
<VersionSuffix>$([System.DateTime]::Now.ToString(yyyy-MM-dd-HHmm))-$(Configuration)</VersionSuffix>
</PropertyGroup>
</Target>
</Project>
Now the Visual Studio works fine, but from the command-line (msbuild MySln.sln /t:Build...) the properties don't apply.
(I tried with Directory.Build.targets and it acts the same (unless I missed something))
(You can try with more simple property, Company for example).
How can I use a time-based property, but apply it only when I creating the DLL?

Using InitialTargets solved my problem:
<Project InitialTargets="Version" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Version">
<PropertyGroup>
<VersionSuffix>$([System.DateTime]::Now.ToString(yyyy-MM-dd-HHmm))-$(Configuration)</VersionSuffix>
</PropertyGroup>
</Target>
</Project>

Related

Target rebuild only in a .NET Core 3.1 project

When compiling a .NET Core 3.1 console application, I want to execute a command only on Rebuild (not Build). I can successfully use the "PreBuild" event, but that executes on both Build & Rebuild. Here is an example of what I am trying to achieve:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<Target Name="BeforeRebuild" BeforeTargets="Rebuild">
<Exec Command="foo" />
</Target>
</Project>
As written, the command never executes. I believe the problem is that "Rebuild" is not a valid target.
Try this out:
<Target Name="This is a custom rebuild event" BeforeTargets="BeforeRebuild">
<Exec Command="echo You did a rebuild" />
</Target>
More information here.

Debug specific PropertyGroup in csproj file

Is it possible to have a section of my csproj file in dotnet core (3.1.1) which is only included when debugging? I'd like to accomplish something like the following
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <!--always valid-->
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<AssemblyName>SomeAssembly</AssemblyName>
<RootNamespace>Some.Root.Namespace</RootNamespace>
</PropertyGroup>
<PropertyGroup> <!--Debugonly-->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
....
</Project>
Yes, use conditional PropertyGroup:
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<!--Debugonly-->
</PropertyGroup>

.wpp.targets file is ignored during build

I'm trying to apply custom ACLs using webdeploy (set Write permission to a folder - the folder is empty but added to the project and correctly deployed).
I'm building a VS solution on my CI server with the following command line:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe"
/consoleloggerparameters:ErrorsOnly
/maxcpucount
/nologo
/property:Configuration=Release
/p:Platform=x86
/verbosity:quiet
MySolution.sln
/p:DeployOnBuild=True
/p:PublishProfile="ci"
/p:ProfileTransformWebConfigEnabled=False
/p:WebPublishPipelineCustomizeTargetFile="MyProject\MyProject.wpp.targets"
It builds correctly but it completely ignores the content of my MyProject.wpp.targets. I tried to write wrong XML but it gets silently ignored, the build is successful but clearly without my wpp.targets applied. The /p:WebPublishPipelineCustomizeTargetFile parameter doesn't have any effect.
This is the content of MyProject.wpp.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="SetupCustomAcls" AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
<ItemGroup>
<MsDeploySourceManifest Include="setAcl">
<Path>$(_MSDeployDirPath_FullPath)\Content\cache</Path>
<setAclAccess>Read,Write</setAclAccess>
<setAclResourceType>Directory</setAclResourceType>
<AdditionalProviderSettings>setAclResourceType;setAclAccess</AdditionalProviderSettings>
</MsDeploySourceManifest>
</ItemGroup>
</Target>
<Target Name="DeclareCustomParameters" AfterTargets="AddIisAndContentDeclareParametersItems">
<ItemGroup>
<MsDeployDeclareParameters Include="ContentCacheSetAclParam">
<Kind>ProviderPath</Kind>
<Scope>setAcl</Scope>
<Match>^$(_EscapeRegEx_MSDeployDirPath)\\Content\\cache$</Match>
<Description>Add write permission to the Content/cache folder.</Description>
<DefaultValue>{$(_MsDeployParameterNameForContentPath)}/Content/cache</DefaultValue>
<Value>$(_DestinationContentPath)/Content/cache</Value>
<Tags>Hidden</Tags>
<Priority>$(VsSetAclPriority)</Priority>
<ExcludeFromSetParameter>True</ExcludeFromSetParameter>
</MsDeployDeclareParameters>
</ItemGroup>
</Target>
</Project>
The project is a net641 csproj <Project Sdk="Microsoft.NET.Sdk.Web">.
What's wrong with my configuration? Thank you for your help.

<_CustomFiles> element not recognized in .pubxml

I'm trying to include a file in an ASP.NET filesystem publish that's not included in the solution (it's autogenerated from a post-build script). According to these instructions, I should be able to add a reference to the file in my .pubxml definition using the <_CustomFiles> element. Here's what my .pubxml file looks like:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>(publish URL here)</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
</PropertyGroup>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="scripts\app.min.js" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>Extra Files\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
</Project>
The second <PropertyGroup> and the <Target> elements were added by me.
However, Visual Studio isn't recognizing the <_CustomFiles> element and the file isn't being included in the publish. Notice the green squiggly line underneath the element:
When I hover over it, the following error message is shown:
The element 'ItemGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element '_CustomFiles' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'
Why is this element not recognized by Visual Studio? What do I need to change in order to include this file in my filesystem publish?
Figured it out. I misunderstood the purpose of the <_CustomFiles> element. It seems this is a custom element (i.e. not part of the XML schema, hence the VS warning) used to pass information to the <FilesForPackaginFromProject> element below it.
It turns out this method was working for me, I was simply using the relative paths incorrectly.

Unable to copy the output of the solution to a directory using MSBuild

I am trying to build web application and copy the output to build\bin\debug folder in the root. But the folder structure is getting created for every project that is referenced in the web application. I need output of web application and all referenced dll's in the root folder.
<?xml version="1.0" encoding="utf-8" ?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<BuildDir>build\bin\$(BuildConfig)</BuildDir>
<BuildConfig>Debug</BuildConfig>
</PropertyGroup>
<ItemGroup>
<ProjectToBuild Include="testwebapp.sln">
<Properties>OutputPath=$(BuildDir);Configuration=$(BuildConfig)</Properties>
</ProjectToBuild>
</ItemGroup>
<Target Name="Build">
<Message Text="Building solution..."/>
<MSBuild Projects="#(ProjectToBuild)" Targets="Build">
</MSBuild>
</Target>
</Project>
But it is working if I execute it using command line prompt like
msbuild "testwebapp.sln" /t:build /p:OutputPath="custompath"
Properties evaluate in order so your
<BuildDir>build\bin\$(BuildConfig)</BuildDir>
<BuildConfig>Debug</BuildConfig>
should be
<BuildConfig>Debug</BuildConfig>
<BuildDir>build\bin\$(BuildConfig)</BuildDir>

Resources