如何在多个项目之间共享gruntfile.js文件或grunt任务?

时间:2016-01-11 17:00:11

标签: gruntjs

我的团队目前正在处理多个项目,这些项目都有自己的 gruntfile.js 文件。但它们都是一样的,复制粘贴在每个新项目上......哪个都不好!

我正在寻找一种只维护一个 gruntfile.js 的方法,并在所有项目之间共享最新版本。我看到两种方法:将共享的gruntfile.js部署到可通过我们的内部网络访问的某个路径...

  • ...在每次调用任何项目目录中的grunt任务时使用grunt --gruntfile = /path/to/share/gruntfile.js taskName。 Jenkins构建可以使共享文件保持最新,并在提交时重新部署它。这样,项目将不再拥有自己的 gruntfile.js 文件。

  • ...在每个项目的正确 gruntfile.js 内,找到一种方法告诉grunt导入共享中声明的所有内容(tasks,configInit等) gruntfile.js 。就像使用具有父pom的Maven pom.xml文件一样。

有没有人看到为什么一个或两个解决方案都不起作用的原因?

有没有人知道这样做的简单方法,可能使用现有的工具或插件?

编辑:我们使用的是SVN,而不是GIT。

1 个答案:

答案 0 :(得分:2)

最后,我找到了一种在多个项目上共享任务声明的方法:我创建了一个自定义的grunt插件。这个插件包含:

custom-grunt-plugin
|_ config
    |_ config.js : contains all the tasks configurations
|_ tasks
    |_ custom-grunt.js : contains all the tasks declarations
|_ package.json : package info
|_ README.md : package documentation

我发布了我的插件并将其添加为我所有项目的devDependency。

最后,我所有项目的grunfile.js:

module.exports = function (grunt) {

  // Load custom tasks
  grunt.loadNpmTasks('custom-grunt-plugin');

  // Load grunt tasks automatically
  require('load-grunt-tasks')( grunt );

  // Load configuration from the custom grunt plugin
  var config = require('custom-grunt-plugin/config/config');

  // Add project specific variables to the config
  config.pkg = grunt.file.readJSON('package.json');
  config.paths = {
    app: 'app',
    dist: 'dist/<%= pkg.name %>/<%= pkg.version %>'
  };      

  grunt.initConfig( config );

};

这就是全部!可能不是最好的解决方案。但是这个有效。

下一步是将项目的package.json中的依赖项列表传输到自定义插件的package.json,并使用&#39; npm install&#39;递归安装所有依赖项。但似乎npm无法加载和安装依赖项的依赖项......