简体   繁体   中英

MsBuild Target doesn't work

I want to change .csproj file to execute my target after I make publish on server from Visual Studio 2017.

<PropertyGroup>
    <PostBuildEvent>copy $(ProjectDir)\lib\Oracle.ManagedDataAccess.dll $(ProjectDir)\bin</PostBuildEvent>
  </PropertyGroup>
  <PropertyGroup>
    <PreBuildEvent>copy $(ProjectDir)\lib\Oracle.ManagedDataAccess.dll $(ProjectDir)\bin</PreBuildEvent>
  </PropertyGroup>  

  <ItemGroup>
    <OracleSourceFile Include="$(ProjectDir)\lib\Oracle.ManagedDataAccess.dll"/>
    <OracleDestinationFolder Include="$(ProjectDir)\bin"/>
  </ItemGroup>

  <Target Name="OracleTarget" AfterTargets="MSDeployPublish" >        
   <Copy
        SourceFiles="@(OracleSourceFile)"
        DestinationFolder="@(OracleDestinationFolder)">        
    </Copy>
  </Target>

But this doesn't work. I don't see in publish output that my target was executed. What I do wrong?

After reading the Microsoft best practices about deploying web application , specially at this page (which talks about Deploying Extra Files), I finally modified my .pubxml like below:

<Target Name="OracleCollectFiles">
    <ItemGroup>
      <OracleSourceFile Include="lib\**\*" />     
      <FilesForPackagingFromProject Include="%(OracleSourceFile.Identity)">
        <DestinationRelativePath>bin\%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>

<PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
      OracleCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>

    <CopyAllFilesToSingleFolderForMsdeployDependsOn>
      OracleCollectFiles;
      $(CopyAllFilesToSingleFolderForMsdeployDependsOn);
    </CopyAllFilesToSingleFolderForMsdeployDependsOn>
  </PropertyGroup>

</Project>

MsBuild Target doesn't work

You may publish your project from File System . The target "MSDeployPublish" is not supported by the File System .

"We currently do not support executing custom targets after publish from VS for the file system protocol. If you publish from the command line the target will be executed however."

So, we could use MSBuild command line to execute this custom target by specify the target, /t:OracleTarget :

msbuild "YourSolutionFile" /t:Build,OracleTarget /p:DeployOnBuild=true /p:PublishProfile=YourPublishFile.pubxml

Besides , another solution for this issue, you can use target CopyAllFilesToSingleFolderForPackage instead of MSDeployPublish :

  <Target Name="OracleTarget" AfterTargets="CopyAllFilesToSingleFolderForPackage" >        
   <Copy
        SourceFiles="@(OracleSourceFile)"
        DestinationFolder="@(OracleDestinationFolder)">        
    </Copy>
  </Target>

Hope this helps.

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