Change AssemblyName of a csproject via msbuild - msbuild

I have a msbuild that calls a *.sln file when doing compilation. This solution file contains 10 csprojects, one of them ( let's call it main.csproject) has the AssemblyName as WinMusic. The content of the msbuild is as follows:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- Default value here -->
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE</DefineConstants>
<SlnFiles Condition=" '$(SlnFiles)'==''" >FullProject.sln</SlnFiles>
</PropertyGroup>
<!-- <ItemGroup> -->
<!-- <SlnFiles Include="SlnFiles=$(SlnFiles2)"/> -->
<!-- </ItemGroup> -->
<Target Name="Build">
<MSBuild Projects="$(SlnFiles)"
Properties="DefineConstants=$(DefineConstants)"/>
</Target>
</Project>
My question is, how to set the AssemblyName property from the above msbuild task?
Just to clarify, I'm talking about AssemblyName in csproject, not in AssemblyInfo.cs.
Edit: This is the new build.proj file I tried, the FullProject.sln is a solution file with one exe and one dll, but the msbuild file renamed both the dll and the exe to NoMusic. What I want is just to rename the exe to NoMusic and the dll should retain the same name.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- Default value here -->
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE</DefineConstants>
<SlnFiles Condition=" '$(SlnFiles)'==''" >FullProject.sln</SlnFiles>
</PropertyGroup>
<Target Name="Build">
<MSBuild Projects="$(SlnFiles)"
Properties="DefineConstants=$(DefineConstants)"/>
<MSBuild Projects="WindowsFormsApplication1\WindowsFormsApplication1.csproj"
Properties="DefineConstants=$(DefineConstants);Platform=ANYCPU;AssemblyName=NoMusic"/>
</Target>
</Project>

Just do this:
<Target Name="Build">
<MSBuild Projects="#(SlnFiles)"
Properties="DefineConstants=$(DefineConstants)"/>
<MSBuild Projects="main.csproject.csproj"
Properties="AssemblyName=NoMusic"/>
Love to know why though.

Related

Set output path for MSBuild task

In the following simple MSBuild file I'd like to overwrite the output path that is defined in the .sln or .csproj file. In line 13 you can see that I call an MSBuild task for an existing VS solution. Usually, the projects that are part of that solution have a property where the output is stored. With my script I'd like to overwrite that so that my "build automation" uses a different directory than the default one.
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="Default">
<PropertyGroup>
<appname>Some App</appname>
<version>2.9.1</version>
<file_xap>Some.App.WP8_$(version).$([System.DateTime]::Now.ToString(`yyyyMMddHHmmss`)).xap</file_xap>
</PropertyGroup>
<Target Name="Default">
<MSBuild Projects="C:\Users\User\Documents\Visual Studio 2013\Projects\SomeApp\SomeApp.sln" Properties="Configuration=Debug;Platform=Any CPU">
</MSBuild>
<Message Text="Output file: $(file_xap)"/>
</Target>
</Project>
So the actual question is: How can I call MSBuild for that sln in a way that the output (the xap-file in that case) to another directory (having all the output apart from the xap-file is fine as well)?
I will post my full xml here so you can understand it all
the structure of the project is like this:
MyProject----MyProject.sln
----MyProject.Server---
----MyProject.Server.proj
----Other server project classes and stuff
----MyProject.Client---
----MyProject.Client.proj
----Client project related stuff
----BuildFromXmlFldr---
----build_both_proj.xml <---This is the example file i posted here
Here is the build_both_proj.xml
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0"
DefaultTargets="Build">
<PropertyGroup>
<SolutionDir>..\</SolutionDir>
<ServerProjectFile>..\MyProject.Server\MyProject.Server.csproj</ServerProjectFile>
<ClientProjectFile>..\MyProject.Client\MyProject.Client.csproj</ClientProjectFile>
<ServerProjectName>MyProject.Server</ServerProjectName>
<ClientProjectName>MyProject.Client</ClientProjectName>
<ServerOutput>C:\_Publish\Server\</ServerOutput>
<ClientOutput>C:\_Publish\Client\</ClientOutput>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
</PropertyGroup>
<Target Name="BuildServer">
<MSBuild Projects="$(ServerProjectFile)"
Targets="Build"
Properties="Configuration=$(Configuration);Platform=$(Platform);OutputPath=$(ServerOutput);">
</MSBuild>
</Target>
<Target Name="BuildClient">
<MSBuild Projects="$(ClientProjectFile)"
Targets="Build"
Properties="Configuration=$(Configuration);Platform=$(Platform);OutputPath=$(ClientOutput);"
StopOnFirstFailure="true">
</MSBuild>
</Target>
<PropertyGroup>
<BuildAllDependsOn>BuildServer;BuildClient</BuildAllDependsOn>
</PropertyGroup>
<Target Name="BuildAll" DependsOnTargets="$(BuildAllDependsOn)"/>
</Project>
This is the msbuild.exe command inside the folder BuildFromXmlFldr that I used:
c:\path_to_msbuild\MSBuild.exe build_both_proj.xml /t:BuildAll
so the output is determined in the Properties attribute in the Target tag
Properties="Configuration=$(Configuration);Platform=$(Platform);OutputPath=$(ServerOutput);"

msbuild task to remove all sub directories with a particular name

I want a msbuild task to remove all sub-directories with a specified name, I tried this but it's not working.
What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<directory></directory>
<subDirectory></subDirectory>
</PropertyGroup>
<Target Name="Main">
<RemoveDir Directories="$(directory)\**\subDirectory\*" />
</Target>
</Project>

How to extract directory from property?

I have a property GroupProj storing a full path name. How can I extract the directory of the property?
I have the following code, but it doesn't work as expected:
<PropertyGroup>
<GroupProj>C:\development\project\default.groupproj</GroupProj>
</PropertyGroup>
<Target Name="Default">
<Message Text="Echo: $(GroupProj->'%(RootDir)')" />
</Target>
I will describe my actual intention of doing so. Perhaps there is a way to do the job that I am not aware of.
I have a Delphi groupproj (MSBuild project) file, C:\development\project\default.groupproj:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Projects Include="project1.dproj">
<Dependencies/>
</Projects>
<Projects Include="project2.dproj">
<Dependencies/>
</Projects>
<Projects Include="project3.dproj">
<Dependencies/>
</Projects>
</ItemGroup>
...
</Project>
There are other 3 MSBuild files (project1.dproj, project2.dproj and project3.dproj) stored in same folder as default.groupproj.
I create a MSBuild project file (c:\test.targets):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="3.5">
<Import Project="$(GroupProj)" />
<Target Name="Build">
<MSBuild BuildInParallel="True" Projects="project1.dproj;project2.dproj;project3.dproj"/>
</Target>
</Project>
And execute as:
c:\> msbuild /p:GroupProj="C:\development\project\default.groupproj" test.targets
The execution shall fail as MSBuild can't find projectN.dproj file. The issue shall be the working directory isn't set to default.groupproj.
One straight solution come into my mind is to extract directory of $(GroupProj) and concat to there projectN.dproj file.
That's the whole story of my question.
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<GroupProj>C:\development\project\default.groupproj</GroupProj>
</PropertyGroup>
<Target Name="Build">
<CreateItem Include="$(GroupProj)">
<Output TaskParameter="Include" ItemName="ItemFromProp"/>
</CreateItem>
<Message Text="1. #(ItemFromProp -> '%(RootDir)%(Directory)')"/>
<Message Text="2. %(ItemFromProp.RootDir)%(ItemFromProp.Directory)"/>
<Message Text="3. %(ItemFromProp.Identity)"/>
<Message Text="4. %(ItemFromProp.FullPath)"/>
<Message Text="5. %(ItemFromProp.FileName)"/>
<Message Text="6. %(ItemFromProp.Extension)"/>
</Target>
</Project>
EDIT:
To build the projects in parallel try this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="GetGroupProjPath">
<ItemGroup>
<GroupProj Include="$(GroupProj)" />
<GroupProjPath Include="#(GroupProj->'%(Directory)')" />
</ItemGroup>
<PropertyGroup>
<GroupProjPath>#(GroupProjPath->'%(RootDir)%(Identity)')</GroupProjPath>
</PropertyGroup>
</Target>
<Import Project="$(GroupProj)" />
<Target Name="GetDProjs" DependsOnTargets="GetGroupProjPath">
<ItemGroup>
<DProjs Include="#(Projects->'$(GroupProjPath)%(FileName)%(Extension)')" />
</ItemGroup>
</Target>
<Target Name="Build" DependsOnTargets="GetDProjs">
<Message Text="#(DProjs)" />
</Target>
</Project>

Using MSBuild to buld a solution (.sln) with many projects in how can I make each project build into its own folder?

I am trying to create a simple build process for a quite complex (many projects) vs2010 solution.
I wish for a folder structure such as this
-Build
-Proj1
-proj1.exe
-proj1.dll
-Proj2
-proj2.exe
-proj2.dll
......
-Projn
-projn.exe
-projn.dll
What I am getting from my attempts below is
-Build
-proj1.exe
-proj1.dll
-proj2.exe
-proj2.dll
-projn.exe
-projn.dll
I currently have this as a .proj file. (see below)
This builds things fine, however it puts everything in the "build" folder that I specify. I want each project to be in its own seperate folder within that 'build' folder. How can I achive this?
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildOutputDir>C:\Projects\BuildScripts\Build</BuildOutputDir>
<SolutionToCompile>PathToSolution.sln</SolutionToCompile>
</PropertyGroup>
<Target Name="Clean">
<RemoveDir Directories="$(BuildOutputDir)" />
</Target>
<Target Name="Compile">
<MakeDir Directories="$(BuildOutputDir)" />
<MSBuild Projects="$(SolutionToCompile)"
properties = "OutputPath=$(BuildOutputDir)" Targets="Rebuild" />
</Target>
<Target Name="Build" DependsOnTargets="Clean;Compile">
<Message Text="Clean, Compile"/>
</Target>
</Project>
I call the .proj with a simple bat
"%windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" /nologo externalBuild.proj /m:2 %*
pause
I have also tried a more complex version (copy and paste!) that looks more like it should work, but still puts things in a single folder.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="BuildAll" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectsToBuild Include="path to solution folder\**\*proj" Exclude="$(MSBuildProjectFile)"/>
</ItemGroup>
<PropertyGroup>
<Configuration>CI</Configuration>
</PropertyGroup>
<Target Name="CoreBuild">
<MSBuild Projects ="#(ProjectsToBuild)"
ContinueOnError ="false"
Properties="Configuration=$(Configuration)">
<Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
</MSBuild>
</Target>
<PropertyGroup>
<DestFolder>Build\</DestFolder>
</PropertyGroup>
<Target Name="CopyFiles">
<Copy SourceFiles="#(OutputFiles)"
DestinationFiles="#(OutputFiles->'$(DestFolder)%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
<Target Name="CleanAll">
<!-- Delete any files this process may have created from a previous execution -->
<CreateItem Include="$(DestFolder)\**\*exe;$(DestFolder)\**\*dll">
<Output ItemName="GeneratedFiles" TaskParameter="Include"/>
</CreateItem>
<Delete Files="#(GeneratedFiles)"/>
<MSBuild Projects="#(ProjectsToBuild)" Targets="Clean" Properties="Configuration=$(Configuration);"/>
</Target>
<PropertyGroup>
<BuildAllDependsOn>CleanAll;CoreBuild;CopyFiles</BuildAllDependsOn>
</PropertyGroup>
<Target Name="BuildAll" DependsOnTargets="$(BuildAllDependsOn)"/>
</Project>
Using devenv.com to build from the command line will do what you want. It will use the output directories specified in the project files. This is what we're using, because at the moment we don't need more control over the build mechanism.

Why won't the copy task item work in my msbuild script?

I have the following code in my msbuild script:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" InitialTargets="Build">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SolutionName>CommTrac.Web\CommTrac.Web</SolutionName>
<SolutionFileName>$(SolutionName).csproj</SolutionFileName>
</PropertyGroup>
<Target Name="Build">
<Message Text="Building the solution"/>
<MSBuild Projects="$(SolutionFileName)" ContinueOnError="false" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="CopyOutput" DependsOnTargets="Build">
<ItemGroup>
<BinFolder Exclude="*.pdb" Include="$(ProjectDir)bin\**\*.*"/>
<BuildOutputFolder Include="C:\BuildOutput" />
</ItemGroup>
<Message Text="Copying from directory: $(BinFolder)"/>
<Copy SourceFiles="$(BinFolder)" DestinationFolder="$(BuildOutputFolder)"/>
</Target>
</Project>
For some reason, it will not copy the files to my output directory. I have tried all the similar
solutions with other questions that I have seen similar to this issue. Anyone have any ideas?
BindFolder and BuildOutputFolder are items, not properties. So you need to reference them using #(BindFolder) and #(BuildOutputFolder) instead of using '$'.