简体   繁体   中英

msbuild building projects in solution multiple times

I'm trying to automate a build, and I'm running into a problem building a single solution.

In this solution, project 1 references project 2, which references project 3. Project 4 also references project 3. These are VS2015 projects, and all projects have a postbuild event that copies the dll to an Assemblies folder.

When msbuild 14 starts to build the solution, it mentions in its output that project 1 builds project 2, and project 2 builds project 3, as you would expect because of the dependencies outlined above. So project 3 is built and its output copied to the Assemblies folder, then project 2, then project 1; so far so good.

Then the solution builds project 4, which also needs project 3. Unlike what I would expect, the process seems to once again build project 3, even though it was already built successfully by the same solution. Even so, it wouldn't matter (it is not very efficient, but it just takes a second), except that when the postbuild xcopy is executed again for project 3, a Sharing Violation occurs, and the xcopy fails with exit code 4. Apparently the solution or the previously built project 2 is holding a ref to the copied file or something.

Is this the usual behavior for msbuild (ie to build projects multiple times when they are referenced by multiple other projects in the same solution), or is this some setting somewhere I can affect? Note that if I open the same solution in VS2015 and do a rebuild, it works just fine, and the dll's are copied to the Assemblies folder as expected.

Thanks for any insights!

I assume these are C# projects and you added xcopy commands in the Project settings in 'Build Events'. Is that correct?

When MSBuild 'builds' a project, it checks dependencies to see if anything is out of date. The 'Build Events' settings are open-ended and the pre and post targets generated from the 'Build Events' settings don't have dependencies that can be checked. Essentially the targets are treated as always "out of date" and must be run every time.

I avoid using the 'Build Events' settings. There is a Copy task that is part of MSBuild. Override the AfterBuild task and use the Copy task. Something like the following:

<Target Name="AfterBuild">
    <Copy SourceFiles="@(OutDir)\$(AssemblyName).*" DestinationFolder="$(AssembliesDir)" />
</Target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM