Search code examples
.netmsbuildbuild-process

Packaging web projects with MSBuild


I have a solution with 5 web projects and want them packaged so i run msbuild mysolution.sln /t:Package after building it and get this:

C:\mysolution.sln.metaproj : error MSB4057: The target "Package" does not exist in the project.

If i package each of the projects one by one all is fine. That's stupid and unnatural, i don't want to have 5 additional steps in my CI process. What's so special about Package target that MSBuild cannot execute it like regular Build or Clean? And what is *.sln.metaproj?


Solution

  • The .sln.metaproj file is auto-generated by MSBuild while it attempts to build the solution file. This file is created because solution files are not MSBuild projects. In this generated file are autocreated the standard tarets Build, Clean etc, but not Package. Your best bet may be to switch from using the solution file to using an MSBuild file that references the projects. It could look something like this:

    <Project ...>
       <ItemGroup>
          <Project Include="Project1.csproj" />
          <Project Include="Project2.csproj" />
          <Project Include="Project3.csproj" />
          <Project Include="Project4.csproj" />
          <Project Include="Project5.csproj" />
       </ItemGroup>
       <Target Name="Package">
          <MSBuild
             Projects="@(Project)"
             Targets="Package"
             Properties="Configuration=$(Configuration);Platform=$(Platform);..."
             />
       </Target>
       ...etc. for Build, Clean etc.
    </Project>
    

    You could also add a stub "makefile project" named something like "Package All" to your solution that calls MSBuild directly on an msbuild project like the one above above. Building this makefile project would perform the package step.

    FYI, to see the generated metaproj, set an environment variable named MSBuildEmitSolution to the value '1' and run a build, the temporary file will be preserved.