Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files in ItemGroup not detected by AfterBuild Target

What the question should have been:

Here is the content of my ModuleCompilation.targets file :

<ItemGroup>
    <ConfigFilesToMove Include="bin\$(Configuration)\*.config"/>
</ItemGroup>
<Target Name="AfterBuild">
    <Message Text="Message for AfterBuild" />
    <Move SourceFiles="@(ConfigFilesToMove)" DestinationFolder="bin\$(Configuration)\Configuration\" />
</Target>

The Message is written to the console but the files are not moved, how come ?

Original question (you can ignore):

I have included an Import in my csproj file to import a task from another file :

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\ModuleCompilation.targets" />

Now, here is the content of my ModuleCompilation.targets file :

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="AfterBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <BaseFiles Include="bin\$(Configuration)\Libraries\ModuleBase*"/>
        <ConfigFilesToMove Include="bin\$(Configuration)\*.config"/>
        <OtherFilesToMove Include="bin\$(Configuration)\*" Exclude="bin\$(Configuration)\$(MSBuildProjectName)*" />
    </ItemGroup>
    <Target Name="AfterBuild">
        <Message Text="AfterBuild" />
        <Move SourceFiles="@(ConfigFilesToMove)" DestinationFolder="bin\$(Configuration)\Configuration\" />
        <Move SourceFiles="@(OtherFilesToMove)" DestinationFolder="bin\$(Configuration)\Libraries\" />
        <Message Text="bin\$(Configuration)\Libraries\ModuleBase*" />
        <Delete Files="@(BaseFiles)" />
    </Target>
</Project>

When I build or rebuild, I do have the "AfterBuild" message in my console, and the files are all indeed generated, but they haven't moved from the Debug folder to where I want them to be.

If I rebuild my projet, the files are moved, I guess because there already were present, but I want to files to be generated and then to be moved, I don't want to have them to be already present !

I must be missing something really obvious, can you help me please ?

Edit:

Ok so I've made some tests and this works :

<ConfigFilesToMove Include="bin\Debug\App.config"/>

While this doesn't:

<ConfigFilesToMove Include="bin\Debug\*.config"/>

Does anyone knowns why ?

Edit 2: This is really weird, here is my code :

<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Files Include="$(OutputPath)*.config"/>

        <Pouet Include="bin\Debug\*.dll"/>
      </ItemGroup>

      <Target Name="AfterBuild">
            <Message Text="@(Pouet)" />
            <Message Text="pouetss" Condition="Exists(@(Pouet))"/>
            <Message Text="nooo" Condition="!Exists(@(Pouet))"/>
            <MakeDir Directories="$(OutputPath)Configuration\" />
            <Copy SourceFiles="@(Pouet)" DestinationFolder="$(OutputPath)Configuration\" />
  </Target>
</Project>

Here is what the log from MSBuild says :

1>Target "AfterBuild: (TargetId:75)" in file "C:\Tests\ModuleTest\Modules\ModuleCompilation.targets" from project         "C:\Tests\ModuleTest\Modules\FirstModule\FirstModule.csproj" (target "Build" depends on it):
1>Task "Message" (TaskId:47)
1>Done executing task "Message". (TaskId:47)
1>Task "Message" skipped, due to false condition; (Exists(@(Pouet))) was evaluated as (Exists()).
1>Task "Message" (TaskId:48)
1>  Task Parameter:Text=nooo (TaskId:48)
1>  nooo (TaskId:48)
1>Done executing task "Message". (TaskId:48)
1>Task "MakeDir" (TaskId:49)
1>  Task Parameter:Directories=bin\Debug\Configuration\ (TaskId:49)
1>  Creating directory "bin\Debug\Configuration\". (TaskId:49)
1>Done executing task "MakeDir". (TaskId:49)
1>Task "Copy" (TaskId:50)
1>  Task Parameter:DestinationFolder=bin\Debug\Configuration\ (TaskId:50)
1>Done executing task "Copy". (TaskId:50)
1>Done building target "AfterBuild" in project "FirstModule.csproj".: (TargetId:75)

So MSBuild ignores every value that contains a wildcard ?

like image 264
Apollidore Avatar asked Aug 06 '15 21:08

Apollidore


People also ask

When build target option is selected which file is made*?

Targets are declared in a project file with the Target element. For example, the following XML creates a target named Construct, which then calls the Csc task with the Compile item type.

What is target file in visual studio?

targets files that contain items, properties, targets, and tasks for common scenarios. These files are automatically imported into most Visual Studio project files to simplify maintenance and readability. Projects typically import one or more .

What is target in MSBuild?

MSBuild allows you to specify build-targets such as Build or Clean with -target:<build-target> (Example: MSBuild.exe -target:Clean ). Of course, a developer can define their own target using a . targets xml file. However, some targets, such as Build or Clean , are pre-defined by Microsoft.


1 Answers

Problem :

ItemGroup created outside of a Target are resolved before any Target is run, so you can't target files that a Target will create

Solution :

I changed my code to :

<Target Name="AfterBuild">
    <ItemGroup>
        <ConfigFilesToMove Include="bin\$(Configuration)\*.config"/>
    </ItemGroup>
    <Message Text="Message for AfterBuild" />
    <Move SourceFiles="@(ConfigFilesToMove)" DestinationFolder="bin\$(Configuration)\Configuration\" />
</Target>

And now my files are detected because the ItemGroup is resolved after the files are created.

like image 53
Apollidore Avatar answered Sep 27 '22 23:09

Apollidore