I am using MSBuild to build my project, which references other projects. I need to force the output filename to "test.exe" (in this example) for the main project. However, when I set the AssemblyName property it changes the output of the dependency projects too. How can I change the AssemblyName only for the main project file output? I need the "OriginalFilename" and "InternalName" meta data to be set to the new filename (test.exe).
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0">
<Target Name="Build">
<MSBuild Projects="Project1\Project1.csproj" Targets="Rebuild"
Properties="AssemblyName=Test.exe; Configuration=Release;
Platform=AnyCPU;" />
</Target>
</Project>
Related
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>
I have the following dummy build script files:
Common.targets
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Target Name="TargetA">
<Message Text="This is TargetA"/>
</Target>
<Target Name="TargetB">
<Message Text="This is TargetB"/>
</Target>
</Project>
EntryPoint.proj
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Import Project="Common.targets"/>
<Target Name="EntryPointTarget" DependsOnTargets="TargetA">
<!--But why this message is not shown during build?-->
<Message Text="This is Entry Point Target"/>
</Target>
</Project>
Why is EntryPointTarget message not shown during build?
If you do not specify a target on the commandline and no DefaultTarget is specified then msbuild executes the first target it sees, TargetA in this case. If you switch TargetA and TargetB you'll see TargetB being executed first. If you remove the import and make EntryPointTarget not depend on any other targets EntryPointTarget will be executed. Those aren't proper fixes obviously, so either:
specify the target explicitly call msbuild EntryPoint.proj /t:EntryPointTarget
make it determinstic what gets executed by adding the DefaultTargets=EntryPointTarget attribute to the Project tag, then you can just call msbuild EntryPoint.proj and it will execute EntryPoint.
Is it possible to access (in a build file) the directory from which MSBuild.exe was called?
I have only been able to get the path of the build file itself. I want the directory from which MSBuild was called instead.
Expected
D:\> msbuild foo\foo.proj
...
MSBuild was called from: D:\>
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="BeforeBuild">
<Message Text="MSBuild was called from: ???" />
</Target>
</Project>
Why
I was wondering if one could replace the absolute path in
D:\workdir> msbuild foo\foo.proj /p:Parameter=d:\workdir\Projects\bar\
with a relative path
D:\workdir> msbuild foo\foo.proj /p:Parameter=Projects\bar\
See MSBuild Reserved and Well-Known Properties
It looks like MSBuildStartupDirectory will help you:
TestBuild.vcxproj snippet
<Target Name="BeforeBuild">
<Message Text="MSBuild was called from: $(MSBuildStartupDirectory)" />
</Target>
Build
d:\Data\Visual Studio 2013\Projects>msbuild /m .\TestMsBuild\TestMsBuild\TestMsBuild.vcxproj /verbosity:detailed /t:BeforeBuild
Task "Message"
MSBuild was called from: d:\Data\Visual Studio 2013\Projects
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
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>