I'm a newbie trying to modify on publish the ApplicationVersion value by AssemblyVersion one on a MS Office AddIn.
Here is my csproj :
<Target Name="SetAssemblyVersionToPublish" AfterTargets="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
When I publish the app from Visual Studio 2015, logs show the good version :
app Version : 4.4.9.0
But in fact it is the ApplicationVersion defined in VS that is used :
<ApplicationVersion>1.0.0.0</ApplicationVersion>
I already tried :
reset all VS settings
disable any vs third party extensions
use safemode
if I publish a WPF app, it works well
Any one would know why I get this change ?
Update
After some test, I found that MSBUILD use a different target file for VSTO : MSBuild\Microsoft\VisualStudio\v14.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets.
It seems to use PublishVersion which is a copy of ApplicationVersion. I don't know why but if I set ApplicationVersion in my target, it does not update the PublishVersion... If I set the publishVersion, I get the good version in my bin folder, but publishing failed (don't copy to publish folder because it still want to get the folder with 1_0_0_0 in bin).
It may be a problem with a bad step which launch my target, but I don't figure when it must be done.
Any help ?
UPDATE 2
It seems to be a bug with VS2015 publishing tool. If I try with msbuild.exe command line, it works well:
On VS I get an error asking for the below folder :
Whereas with the following msbuild command, it works :
MSBuild xxx\ExcelAddIn1\ExcelAddIn1\ExcelAddIn1.csproj /t:clean;publish
Anyone know what can be done as a workaround ?
First of all, I have a question, when I use your code in a winform project and execute publish, I got an error like:
After I test, I found that
ApplicationVersion which you got is illegal three digits instead of four digits, so there is a problem with your method of obtaining AssemblyVersion.
In general, the publish version is under Project Properties(right-click on your project)-->Publish
You can note that it is four digits.
Which proves that you have miss a node on Pattern property.
Suggestion
You should use
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
The whole code are:
<Target Name="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
</Target>
It will reads the AssemblyVersion from the AssemblyInfo.cs file.
In my side, it is
Actually, your method already works and it overwrite the publish version for the app.
I think you have read the first part of the build output log like this:
It is only the default system initial value at the beginning of the build, and it is just displayed there. And then through your custom msbuild script actually has overwritten its value.
Before executing publish, you should delete the publish folder, bin and obj folder, then execute the publish, you can enter the folder to check:
Update 1
I think your csproj file has imported some targets or props file which has other targets overwrite the AfterCompile target. So your method failed.
That is, your method may be overwritten elsewhere.
So I think you should not use AfterCompile as the target name and should name it as another to distinguish between them.
Use this:
<Target Name="SetAssemblyVersionToPublish" AfterTargets="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
</Target>
Or, You can add a file called Directory.Build.targets file on your project folder
and then add my code on it:
<Project>
<Target Name="SetAssemblyVersionToPublish" AfterTargets="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
</Target>
</Project>
Then, delete bin, obj, publish folder and then republish again to check it.
If the issue still persists, you should try the following suggestions to troubleshoot the issue:
1) Try to reset vs settings by Tools-->Import and Export settings-->Reset all vs settings
2) disable any vs third party extensions under Tools-->Extensions and Updates -->Installed, after that, close VS ,restart your project.
3) you could try devenv /safemode on the Developer Command Prompt for VS2015 to start a pure, initial vs to test your solution.
===============================================
Update 2
The MS Office Excel AddIn project is quite different from the traditional projects. And it is impossible by using this way.
As a workaround, I think you should use msbuild script to publish the project.
1) create a file called PublishExcel.proj
2) add these content on that file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="SetAssemblyVersionToPublish">
<!--you must specify the full path of the AssemblyInfo.cs from your project -->
<ReadLinesFromFile File="C:\Users\xxx\Documents\Visual Studio 2015\Projects\ExcelAddIn1\ExcelAddIn1\Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
<MSBuild Projects="C:\Users\Administrator\Documents\Visual Studio 2015\Projects\ExcelAddIn1\ExcelAddIn1\ExcelAddIn1.csproj" Properties="ApplicationVersion=$(ApplicationVersion)" Targets="Publish">
</MSBuild>
</Target>
</Project>
3) then open Developer Command prompt for VS2015 and then type:
msbuild xxx\xxx\PublishExcel.proj -t:SetAssemblyVersionToPublish
And the publish folder will be under the bin\Debug or Release\app.publish folder.
This function will work for MS Office Excel AddIn project.
Related
I'm busy moving my code from .Net Framework libraries to .netstandard2.0 libraries. So far it's going pretty well, but now i'm stuck with the in the .csproj file.
The existing project file has this defined
<Target Name="Rebuild">
<Exec Command="echo Now Rebuilding the package" />
</Target>
the actual command executes an exe that generates a bunch of xml classes based on an xsd.
I cannot get this to work in a .netstandard2.0 project?
I've searched everywhere but i cannot find a reason for this not working...
I suspect that in your specific instance, the Rebuild target will be overwritten by the sdk targets that are implicitly imported after your code. If you want to overwrite SDK-provided tasks, you need to change to explicit SDK imports (instead of <Project Sdk="...">):
<Project>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<!-- other project content -->
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<Target Name="Build">
<!-- overwrite Build target here -->
</Target>
<Target Name="Rebuild">
<!-- overwrite Rebuild target here -->
</Target>
</Project>
The Exec target is supported though the echo command may or may not work depending on the platform you are running it on (since echo may be just a built-in command of the shell but no executable that can be run).
Make sure that:
The command starts with the path to an executable that is found on the PATH or is specified absolute or relative to the csproj file being built.
The target is actually executed. E.g. some programs could use /t:Clean;Build instead of /t:Rebuild.
I'm trying to upgrade our TFS server to 2013. We're currently using 2012, but we've also been clinging on to the upgrade template for dear life. With 2013, we'd like to go to the default template and modify it as little as possible.
The problem comes in when you consider that the default template asks you to add each individual .csproj or .sln file that you would like to build. The nice thing about the tfsbuild.proj files is that not only can you build on the server, but you can check out the entire branch and build everything locally, on the command line, by just passing the tfsbuild.proj file to msbuild.exe. Also, developers can own the tfsbuild.proj file without having write access to change the build definition.
What is the replacement for the TFSBuild.proj file in TFS 2013?
My requirements are:
Clean build configuration.
Can easily build everything locally.
What is the solution to this problem in TFS 2013?
Create a wrapper MSBuild .proj that your TFS build definition uses. We use this technique for NuGet package restore, but it can equally be used to chain together multiple solution files.
For local builds you can use msbuild with that .proj wrapper as the target (I just build the .sln file directly as there is only 1).
.proj file (not suggesting that you should use this exact .proj file, just an example)
<PropertyGroup>
<OutDir Condition=" '$(OutDir)'=='' ">$(MSBuildThisFileDirectory)bin\</OutDir>
<Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
<SourceHome Condition=" '$(SourceHome)'=='' ">$(MSBuildThisFileDirectory)</SourceHome>
<ToolsHome Condition=" '$(ToolsHome)'=='' ">$(MSBuildThisFileDirectory)tools\</ToolsHome>
</PropertyGroup>
<ItemGroup>
<Solution Include="$(SourceHome)*.sln">
<AdditionalProperties>OutDir=$(OutDir);Configuration=$(Configuration)</AdditionalProperties>
</Solution>
</ItemGroup>
<Target Name="RestorePackages">
<Exec Command=""$(ToolsHome)NuGet\NuGet.exe" restore "%(Solution.Identity)"" />
</Target>
<Target Name="Clean">
<MSBuild Targets="Clean"
Projects="#(Solution)" />
</Target>
<Target Name="Build" DependsOnTargets="RestorePackages">
<MSBuild Targets="Build"
Projects="#(Solution)" />
</Target>
<Target Name="Rebuild" DependsOnTargets="RestorePackages">
<MSBuild Targets="Rebuild"
Projects="#(Solution)" />
</Target>
I'm thinking that the final result is going to be "it can't be that easily done", but just seems like it should be. I have a personal project I am working on. I'd hate to have to manually (or even in script) change versions, company, copyright, and all that on ALL the assembly.cs files and would like all that to be either in a script or in a file I can change (so the script stays the same mostly) when I want to update the version. But it seems like MSBuild is mostly a "build as is specified in Visual Studio". I'd just hate to have all that history of these files where I change just the version and possibly even make a mistake as this project will continue to get bigger and bigger. I'd like to just be able to add a new project to Visual studio and have whatever command line in my powershell script just say "compile this, but give it this company name and this file version instead of whatever is listed in the code file".
Google has NOT proven fruitful in this. I've even found it difficult to build my files to a specific folder. I've had to so far make sure all my projects are 2 folders deep and was able to say to build them at ....\, but I would like to be able to change that randomly if I like and have them built elsewhere if I so desire.
Is MSBuild perhaps not the way to go? Is there someway else to build visual studio that would be better from command line? Eventually I also want to auto build the install with wix and be able to match its version with the binary versions.
thank you
Since csproj is xml, you can use XmlUpdate "helpers" to modify the values inside the csproj file before you do your build.
For other files, you can use some other Tasks to do the job.
Here is one helpful target:
http://msbuildtasks.tigris.org/ and/or https://github.com/loresoft/msbuildtasks has the ( FileUpdate (and SvnVersion task if that is your Source-Control) ) tasks.
<Target Name="BeforeBuild_VersionTagIt_Target">
<ItemGroup>
<AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
</ItemGroup>
<!--
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
<Output TaskParameter="Revision" PropertyName="MyRevision" />
</SvnVersion>
-->
<PropertyGroup>
<MyRevision>9999</MyRevision>
</PropertyGroup>
<FileUpdate Files="#(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="AssemblyFileVersion("$1.$2.$3.$(MyRevision)" />
</Target>
Below is an example of manipulating the csproj(xml).
How to add a linked file to a csproj file with MSBuild. (3.5 Framework)
But basically, when you build, you can put all the repetative stuff in a msbuild definition file (usually with the extension .proj or .msbuild)...and call msbuild.exe MyFile.proj.
Inside the .proj file, you will reference your .sln file.
For example:
$(WorkingCheckout) would be a variable (not defined here)...that has the directory where you got a copy of hte code from your source-control.
<Target Name="BuildIt" >
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"></Output>
</MSBuild>
<Message Text="BuildItUp completed" />
</Target>
So below is the more complete example.
You would save this as "MyBuild.proj" and then call
"msbuild.exe" "MyBuild.proj".
Start .proj code. (Note, I did not import the libraries for the FileUpdate Task)
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">
<PropertyGroup>
<!-- Always declare some kind of "base directory" and then work off of that in the majority of cases -->
<WorkingCheckout>.</WorkingCheckout>
</PropertyGroup>
<Target Name="AllTargetsWrapped">
<CallTarget Targets="BeforeBuild_VersionTagIt_Target" />
<CallTarget Targets="BuildItUp" />
</Target>
<Target Name="BuildItUp" >
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"></Output>
</MSBuild>
<Message Text="BuildItUp completed" />
</Target>
<Target Name="BeforeBuild_VersionTagIt_Target">
<ItemGroup>
<AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
</ItemGroup>
<!--
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
<Output TaskParameter="Revision" PropertyName="MyRevision" />
</SvnVersion>
-->
<PropertyGroup>
<MyRevision>9999</MyRevision>
</PropertyGroup>
<FileUpdate Files="#(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="AssemblyFileVersion("$1.$2.$3.$(MyRevision)" />
</Target>
</Project>
To enhance the above, you would create a new target that would run before "BeforeBuild_VersionTagIt_Target", that would pull your code from source-control and put it in the $(WorkingCheckout) folder.
The basic steps would then be: 1. Checkout code from Source-Control. 2. Run the targets that alter the AssemblyVersion (and whatever else you want to manipulate) and 3. Build the .sln file.
That's the basics of a .proj file. You can do much more. Usually by using helper libraries that already exists.
I have several XSLTs used in my ASP.NET web application.
I want these files to be compiled to dll whenever I build the project.
Currently, I'm compiling the xslts manually by invoking xsltc.exe from vs2010 tools command prompt.
How can I add msbuild task for xsltc.exe so that it will generate assembly whenevr i build my project?
I'm using .NET 4.0.
That works but doesn't really wrap the tool in a MSBuild friendly way.
I came up with this (which was good enough to get by).
<!-- The Transform File Names... -->
<ItemGroup>
<XsltcTransform Include="Transform1.xslt">
<!-- And the generated .Net Class name. -->
<Class>Transform1Class</Class>
</XsltcTransform>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Sadly using $(OutDir) MUST come after the Import of CSharp.targets -->
<PropertyGroup>
<XSLTCOutputDll>$(OutDir)xslts.dll</XSLTCOutputDll>
</PropertyGroup>
<Target Name="FindXSLTC">
<PropertyGroup>
<XSLTC>"$(TargetFrameworkSDKToolsDirectory)xsltc.exe"</XSLTC>
</PropertyGroup>
</Target>
<Target Name="XSLTC" Inputs="#(XsltcTransform)" Outputs="$(XSLTCOutputDll)" DependsOnTargets="FindXSLTC">
<Exec Command="$(XSLTC) /out:"$(XSLTCOutputDll)" #(XsltcTransform -> ' /class:%(Class) %(FullPath) ')" />
</Target>
<Target Name="BeforeResolveReferences" DependsOnTargets="XSLTC">
</Target>
These targets will let you compile multiple transforms into one DLL.
Running XSLTC before "BeforeResolveRefereneces" is necessary so that you can have an assembly reference to the generated DLL.
<PropertyGroup>
<WinSDK>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin</WinSDK>
</PropertyGroup>
<Target Name="Build">
<Exec Command="%22$(WinSDK)\xsltc.exe%22 /out:$(OutputPath)\_PublishedWebsites\xyzapp\bin\Xslts.dll /class:ABC %22$(MSBuildProjectDirectory)\xyzapp\a.xslt%22 /class:DEF %22$(MSBuildProjectDirectory)\xyzapp\b.xslt%22 /class:GHI %22$(MSBuildProjectDirectory)\xyzapp\c.xslt%22"/>
</Target>
So I edited my csproj file on an MVC 3 RTM application to set the following property:
<MvcBuildViews>true</MvcBuildViews>
This should cause my views to be complied during build and force a build error if my view is broken. This is the only change I made, however, when I try to build the application, I get the following error:
It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.
The project compiles and runs successfully if I change back to false,
The following are the build tasks configured in the csproj file (these were never manually edited, they were added by Visual Studio 2010)
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target> -->
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
Am I missing something here? How do I get MVC 3 / Visual Studio 2010 configured correctly to validate my views at build time?
I had this problem a few days ago and I fixed it by deleting obj/Debug folder. Cleaning the project also works. I have no idea about the cause of the issue, though.
See Joe Cartano's answer for a more permanent solution.
This problem occurs when there is web project output (templated web.config or temporary publish files) in the obj folder. The ASP.NET compiler used isn't smart enough to ignore stuff in the obj folder, so it throws errors instead.
Another fix is to nuke the publish output right before calling <AspNetCompiler>. Open your .csproj and change this:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
to this:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<ItemGroup>
<ExtraWebConfigs Include="$(BaseIntermediateOutputPath)\**\web.config" />
<ExtraPackageTmp Include="$([System.IO.Directory]::GetDirectories("$(BaseIntermediateOutputPath)", "PackageTmp", System.IO.SearchOption.AllDirectories))" />
</ItemGroup>
<Delete Files="#(ExtraWebConfigs)" />
<RemoveDir Directories="#(ExtraPackageTmp)" />
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
That will delete all web.configs under \obj, as well as all PackageTmp folders under \obj.
UPDATE:
Even better, based off https://stackoverflow.com/a/48582282/8037 you can exclude the obj folder entirely. Apparently the <AspNetCompiler /> task doesn't have an exclude parameter, but if you switch to calling the aspnet_compiler .exe directly, you can exclude obj like this:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<Exec Command="$(MSBuildFrameworkToolsPath)aspnet_compiler.exe -v temp -p $(WebProjectOutputDir) -x $(BaseIntermediateOutputPath)"/>
</Target>
When you get this error do you have another web.config file in your obj folder? If you are using MSDeploy this might help: MSDN Blog: The Aspnet Compiler Build Task in Visual Studio 2010 ASP.Net MVC 2 Projects, if not, maybe another web.config is being generated by some tool you are running.
This is what worked for me. Optionally, you may specify a condition with the configuration.
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
<Target Name="AfterBuild" Condition="'$(Configuration)'!='Debug'">
<RemoveDir Directories="$(BaseIntermediateOutputPath)" />
</Target>
This issue of Compile-time View Checking even though MvcBuildViews is set to 'true' is well-explained in the following MSDN blog:
https://blogs.msdn.microsoft.com/jimlamb/2010/04/20/turn-on-compile-time-view-checking-for-asp-net-mvc-projects-in-tfs-build-2010/
You could do the fix by editing .csproj file directly:
<PropertyGroup>
<MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
<Target Name="BuildViews" Condition="'$(MvcBuildViews)'=='true'" AfterTargets="Build">
<Message Importance="normal" Text="Precompiling views" />
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
A simple solution kinda compiled from the other answers here
You can simply remove the whole /obj folder like this:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!--add this line-->
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>