Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the assembly project file name from Output TaskParameter

Tags:

c#

.net

msbuild

I am using msbuild.exe to automate my build from the command line.

I do the following steps.

  • Compile projects into a folder each outside the project directory
  • zip every compiled project

here are my targets

first one to compile

<Target Name="BuildProjects"  DependsOnTargets="BeforeBuild">

    <ItemGroup>      
  <BuildProjectFiles Include="**\MyCompany.Project1\MyCompany.Project1.csproj" />
  <BuildProjectFiles Include="**\MyCompany.Project2\MyCompany.Project2.csproj" />
  <BuildProjectFiles Include="**\MyCompany.Project2\MyCompany.Project2-CE.csproj" />
      ... and some more
    </ItemGroup>

    <MSBuild Projects="@(BuildProjectFiles)" Properties="AllowUnsafeBlocks=true;Configuration=$(Configuration);OutputPath=$(MSBuildProjectDirectory)\Deploy\bin\%(BuildProjectFiles.FileName)">
        <Output TaskParameter="TargetOutputs"
                ItemName="BuildProjectsOutputFiles" />
    </MSBuild>

</Target>

and now the target to zip every compiled project to its one file.

<Target Name="ZipProjects" DependsOnTargets="BuildProjects">

    <CreateItem
        Include="@(BuildProjectOutputFiles)"
            AdditionalMetadata="AssemblyName=%(Filename);ProjectName=%(RelativeDir)">
        <Output TaskParameter="Include" ItemName="BuildProjectsOutputFiles123"/>
    </CreateItem>

    <CreateItem Include="%(BuildProjectsOutputFiles123.RelativeDir)*" AdditionalMetadata="OutputFileName=$(MSBuildProjectDirectory)\Deploy\dist\$(Configuration)\%(BuildProjectsOutputFiles123.AssemblyName)-$(MajorNumber).$(MinorNumber).$(ReleaseNumber).zip;WorkingDirectory=%(BuildProjectsOutputFiles123.RelativeDir)">
        <Output TaskParameter="Include" ItemName="BuildProjectsOutputFiles456"/>
    </CreateItem>

    <Zip Files="@(BuildProjectsOutputFiles456)"
        WorkingDirectory="%(BuildProjectsOutputFiles456.WorkingDirectory)" 
        ZipFileName="%(BuildProjectOutputFiles456.OutputFileName)"  />
        <!-- ZipLevel="9"/> -->

</Target>

So what happens is that every project I specified in BuildProjectFiles gets compiled into the folder <rootdir>\deploy\bin\<name of the csproj file without extension\

In the second step I use the MSBuild.Community.Tasks Zip-Task to zip every project and copy it to <rootdir>\deploy\dist\release\<assemblyname>-<version>.zip

So basically the assembly project1.exe and its dependencies are in the file project1-2.4.7.zip after executing msbuild.

That works very well. But now I have a change that I can't figure out how solve. I have two assemblys with the same assembly name (one for Windows and the other for Windows CE) so the first project compiles and creates a folder project2-2.4.7.zip and then the next project compiles and overwrites the zip file.

Now I want the Zip-File to be named after the .csproj file (like the bin folder). Since my one project file is called mycompany.project2.csproj and the other one mycompany.project2-ce.csproj I should be fine.

In short: How can I pass the project name to the zip target?

like image 297
Jürgen Steinblock Avatar asked Aug 20 '12 13:08

Jürgen Steinblock


1 Answers

Is this suitable? $(MSBuildProjectName)

MSBuild Reserved Properties

like image 113
Anssssss Avatar answered Oct 07 '22 03:10

Anssssss