【问题标题】:BeforeBuild and AfterBuild events in Visual Studio Tools for Cordova 2015 Update 5适用于 Cordova 2015 更新 5 的 Visual Studio 工具中的 BeforeBuild 和 AfterBuild 事件
【发布时间】:2016-02-09 10:38:49
【问题描述】:

我正在尝试在我的 Visual Studio 2015 (TACO) 项目中使用 pre 和 post build。正如PreBuildEvent and PostBuildEvent on Visual Studio 2015 Tools for Apache Cordova 中所述,我已将<Target> 元素添加到我的 .jsproj 文件中,因此它现在以如下所示结束:

  <Import Project="_apacheCordovaProjectSourceItems.Targets" Condition="Exists('_apacheCordovaProjectSourceItems.Targets')" />
  <Target Name="BeforeBuild">
    <Exec Command="if $(ConfigurationName) == Release (echo ** Before Build **)" />  
    <Exec Command="attrib -R &quot;$(ProjectDir)\platforms\*.*&quot; /S" IgnoreExitCode="true" />
  </Target>
  <Target Name="AfterBuild">
    <Exec Command="if $(ConfigurationName) == Release (echo ** After Build **)" />  
    <Exec Command="if $(ConfigurationName) == Release (xcopy &quot;$(TargetDir)*.*&quot; &quot;$(SolutionDir)..\..\Binaries\$(PlatformName)\*.*&quot; /Y /S /E /F /I)" />
  </Target>
</Project>

我的问题是 BeforeBuild 和 AfterBuild 事件都在构建开始时触发

1>------ Build started: Project: MyProject, Configuration: Release Android ------
1>  ** Before Build **
1>  ** After Build **
1>  D:\Workspaces\Products\MyProduct\Projects\Main\Sources\Apps\MyProject\bin\Android\Release\android-release-unsigned.apk -> D:\Workspaces\Products\MyProduct\Projects\Binaries\Android\android-release-unsigned.apk
1>  D:\Workspaces\Products\MyProduct\Projects\Main\Sources\Apps\MyProject\bin\Android\Release\manifest-merger-release-report.txt -> D:\Workspaces\Products\MyProduct\Projects\Binaries\Android\manifest-merger-release-report.txt
1>  2 File(s) copied
1>  Your environment has been set up for using Node.js 0.12.2 (ia32) and npm.
1>   ... [Rest of output omitted] ...

谁能解释为什么会这样,或者我如何在构建完成后运行构建后事件?

【问题讨论】:

    标签: visual-studio-cordova post-build-event taco


    【解决方案1】:

    在用头撞墙一段时间后,我放弃了 Visual Studio AfterBuild 事件,并为 Cordova after_build 使用了一个钩子。它在整个构建过程中稍早触发,但足以满足我的要求。我将发布它的要点,以防其他人需要做类似的事情。

    1. 在解决方案资源管理器中找到config.xml,右键单击并选择查看代码
    2. 在 config.xml 中添加 &lt;hook&gt; 部分,如下所示

      <platform name="android">
          <hook type="after_build" src="scripts/afterbuild-copy-to-drop.js" />
      </platform>
      

      这里我只为 Android 构建挂钩 after_build 事件。

    3. 现在在项目的根目录下创建一个scripts文件夹,即与plugins和www文件夹同级。

    4. 在此处创建一个名称与挂钩定义中的 src 属性匹配的 JavaScript 文件,即“afterbuild-copy-to-drop.js”。
    5. 在此脚本文件中写入所需的代码。这是我的

      module.exports = function (ctx) {
          console.log('Executing custom "' + ctx.hook + '" hook for ' + ctx.opts.platforms);
      
          var path = ctx.requireCordovaModule('path'),
              shell = ctx.requireCordovaModule('shelljs');
      
          // make sure we are in a release build
          var isRelease = (ctx.cmdLine.indexOf('--configuration Release') >= 0);
      
          var solutionRoot = path.join(ctx.opts.projectRoot, '../..');
          var dropRoot = path.join(solutionRoot, '../../Binaries/Release/Apps');
      
          if (isRelease){
              if (ctx.opts.platforms == 'android') {
      
                  var platformRoot = path.join(ctx.opts.projectRoot, 'platforms/android');
                  var apkFileLocation = path.join(platformRoot, 'build/outputs/apk/android-release.apk');
      
                  dropRoot = path.join(dropRoot, 'Android');
                  var dropApkFileLocation = path.join(dropRoot, 'my-app.apk');
      
                  console.log('------ Making directory \'' + dropRoot + '\'');
                  shell.mkdir('-p', dropRoot);
      
                  console.log('------ Copying \'' + apkFileLocation + '\' to ' + dropApkFileLocation + '\'');
                  shell.cp('-f', apkFileLocation, dropApkFileLocation);
              }
          }
      
          console.log('Finished executing "' + ctx.hook + '" hook for ' + ctx.opts.platforms);
      };
      

    更多关于钩子的信息可以在https://cordova.apache.org/docs/en/dev/guide/appdev/hooks/找到

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 2016-07-05
      • 2018-10-03
      相关资源
      最近更新 更多