简体   繁体   中英

MSBuild Copy task issue

the problem I face is my target 'CopyBuildFiles' does not copy as I think it is unable to find the build folder. The build folder is defined in the property $(BuildFolder), this folder name is created based on date.time and is created in one of the previous targets that runs before 'CopyBuildFiles'.

If I hardcode a path for @(SrcFiles) instead of deriving this from $(BuildFolder) it works.

Any thoughts, what is happening here and how can this be resolved?

Many thanks.

<PropertyGroup>

    <year>$([System.DateTime]::Now.ToString("yy"))</year>
    <month>$([System.DateTime]::Now.ToString("MM"))</month>
    <day>$([System.DateTime]::Now.ToString("dd"))</day>
    <time>$([System.DateTime]::Now.ToString("HHmm"))</time>
    <AssemblyFileVersionAttribute>[$(year).$(month).$(day).$(time))]      </AssemblyFileVersionAttribute>
    <BuildFolder>c:\website.builds\$(AssemblyFileVersionAttribute)\</BuildFolder>
    <IISFolder>c:\website.publish\</IISFolder>        
    <LogDirectory>C:\website.builds.logs</LogDirectory>
    <LogFile>C:\website.builds.logs\Buildlog_$(AssemblyFileVersionAttribute).txt</LogFile>

</PropertyGroup>


<Target Name="PreBuild">
    <MakeDir Directories="$(LogDirectory)" />       
    <RemoveDir Directories="$(IISFolder)"></RemoveDir>        
    <MakeDir Directories="$(BuildFolder)" />
    <MakeDir Directories="$(IISFolder)" />        
</Target>

<Target Name="Compile">
    <MSBuild Projects="$(MSBuildStartupDirectory)\websitev2.sln"  Properties="OutDir=$(BuildFolder)\" />        
</Target>

<ItemGroup>
    <SrcFiles Include="$(BuildFolder)_PublishedWebsites\**\*.*"/>
</ItemGroup>

<Target Name="CopyBuildFiles"
          Inputs="@(SrcFiles)"
          Outputs=
      "@(SrcFiles->'$(IISFolder)%(RecursiveDir)%(Filename)%(Extension)')">
    <Copy SourceFiles="@(SrcFiles)"
          DestinationFiles="@(SrcFiles->'$(IISFolder)%(RecursiveDir)%(Filename)%(Extension)')"
      />      
</Target>

<Target Name="Deploy">
    <CallTarget Targets="PreBuild" />
    <CallTarget Targets="Compile" />
    <CallTarget Targets="CopyBuildFiles" />
</Target>

Since one of your targets defines a $(BuildFolder) property, the @(SrcFiles) item group needs to be defined also in the same target, or in some other target that is executed after $(BuildFolder) is defined.

Something like this:

<Target Name="MyTarget">
    <PropertyGroup>
        <BuildFolder>... some path ... </BuildFolder>
    </PropertyGroup>
    <ItemGroup>   
        <SrcFiles Include="$(BuildFolder)_PublishedWebsites\**\*.*"/>   
    </ItemGroup>  
</Target>
Replaced ItemGroup with **CreateItem**. This makes the copy work. 
**CreateItem** works without parameters. The folder name did not get passed through a parameter for some reason??!?

<Target Name="BuildInit">
    <CreateItem Include="$(BuildFolder)_PublishedWebsites\**\*.*">
      <Output TaskParameter="Include" ItemName="YourFilesToCopy" />
    </CreateItem>

    <Copy SourceFiles="@(YourFilesToCopy)"
                    DestinationFiles="@(YourFilesToCopy->'$(IISFolder)\%(RecursiveDir)%(Filename)%(Extension)')" />
  </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