Code Monkey home page Code Monkey logo

grunt-war's Introduction

grunt-war

Pure JavaScript implementation for creating a WAR of your project for deployment on a JVM servlet container. Enjoy!

Getting Started

This plugin requires Grunt ~0.4.2

npm install grunt-war --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-war');

Then sip on some @AnodyneCoffee.

Are you on Strava?

Do you enjoy YouTube?

Enjoy!

The "war" task

Overview

In your project's Gruntfile, add a section named war to the data object passed into grunt.initConfig().

The simplest usage might look simiiar to the following:

grunt.loadNpmTasks('grunt-war');

grunt.initConfig({

      /*
       * Build a WAR (web archive) without Maven or the JVM installed.
       */
      war: {
        target: {
          options: {
            war_dist_folder: '<%= build_dir %>',    /* Folder where to generate the WAR. */
            war_name: 'webmagic'                    /* The name fo the WAR file (.war will be the extension) */
          },
          files: [
            {
              expand: true,
              cwd: '<%= build_dir %>',
              src: ['**'],
              dest: ''
            }
          ]
        }
      }
});

If your project does not include a web.xml then grunt-war can generate one for you. This is one option below:

grunt.loadNpmTasks('grunt-war');

grunt.initConfig({

      /*
       * Build a WAR (web archive) without Maven or the JVM installed.
       */
      war: {
        target: {
          options: {
            war_dist_folder: '<%= build_dir %>',
            war_name: 'grunt-magic',
            webxml_welcome: 'index.html',
            webxml_display_name: 'Grunt WAR',
            webxml_mime_mapping: [ { extension: 'woff', mime_type: 'application/font-woff' } ]
          },
          files: [
            {
              expand: true,
              cwd: '<%= build_dir %>',
              src: ['**'],
              dest: ''
            }
          ]
        }
      }
});

A complete and very simple Grunt file example will look like the following.

// Save this snippet as grunt-war.js and run with  "grunt --gruntfile grunt-war.js war" at the command line.
// Assumes simple layout:
// -project
// --build  (The folder where the generated grunt-magic.war file will go)
// --src    (all the source code, html, etc)
// --- index.html (The file name must match up with the webxml_welcome: property below)
module.exports = function ( grunt ) {
    grunt.loadNpmTasks( 'grunt-war' );

    var taskConfig = {
        war: {
            target: {
                options: {
                    war_verbose: true,
                    war_dist_folder: 'build',           // Folder path seperator added at runtime.
                    war_name: 'grunt-magic',            // .war will be appended if omitted
                    webxml_welcome: 'index.html',
                    webxml_display_name: 'Grunt WAR'
                },
                files: [
                    {
                        expand: true,
                        cwd: 'src',
                        src: ['**'],
                        dest: ''
                    }
                ]
            }
        }
    };

    grunt.initConfig( taskConfig );
};

Options

options.war_dist_folder

Type: 'string' Default value: 'test'

This is the folder that the war will be placed in. This folder has to exist before this task is run.

options.war_name

Type: 'string' Default value: 'grunt'

options.war_extras

Type: 'Array' Default value: []

A list of files and folders entries that are to be included in the war. Each object in the array has keys filename and data example: { filename: 'name_of_file.ext', data: file_data }. If the key data is omitted then an empty folder called filename will be added to the WAR. The value of key data can either be a string or a function that returns a string.

war_extras: [ {filename: 'grunt-war-credits.txt', data: 'This line will appear in the file!\n'} ]

options.war_verbose

Type: 'boolean' Default value: false

Logs progress to the grunt console log.

options.war_compression

Type: 'string' Default value: 'DEFLATE'

Compress ('DEFLATE') or leave uncompressed ('NONE').

options.webxml

Type: 'Function' Default value: Normally omitted. Only provide if you want absolute control over the format and contents of the web.xml. If you specify this option the other options.webxml_XXX will have no effect if specified.

Example 1

     /* Return a string that will become the complete contents of the web.xml */
     webxml: function (opts) { 
                return 'string containing contents of web.xml'; 
             },

Example 2

     /* Return the contents of a file which will become the web.xml */
     webxml: function (opts) { 
                var fs = require('fs'); 
                return fs.readFileSync(pathToFileWithContentsOfHardCodedWebXML, 'binary'); 
             },

options.webxml_welcome

Type: 'string' Default value: 'index.html'

options.webxml_display_name

Type: 'string' Default value: 'Grunt WAR'

options.webxml_mime_mapping

Type: 'Array' Default value: []

An array of objects with properties extension and mime_type.

options.webxml_webapp_extras

Type: 'Array' Default value: []

An array of objects that are either 'string' or 'function' that return 'string'. These entries are included directly into the generated web.xml.

webxml_webapp_extras: [
    '<login-config />\n',
    '<session-config>\n<session-timeout>\n30\n</session-timeout>\n</session-config>\n'
]

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

0.5.1

  • Sample webxml_webapp_extras. Thanks to @pthaden

0.5.0

  • Fixed issue when using NPM3 that grunt-war dependencies had to be manually installed. Thanks to @bradrox

0.4.9

  • Just a version bump of dependencies used internally.

0.4.8

  • Fixed issue with "normalize" creating folders. Thanks to rbalmf and laszlogmenyhart.

0.4.7

  • Fixed issue with "extras" that was not consistent with EMFILE fix. Thanks to laszlogmenyhart.

0.4.6

  • Fixed EMFILE exception (no many open files).

0.4.5

  • Fixed a second WEB-INF/web.xml being added on windows.

0.4.4

  • Bug fix that prevented old wars from being deleted

0.4.3

  • Update to package.json for grunt-war.

0.4.2

  • Fixed bad archive entry names. (mmaday)

0.4.1

  • Documenation update.

0.4.0

  • This build uses streams to build the WAR and can process vastly larger projects (huge) than in prior versions. This should fix the "Out of memory error - FATAL ERROR". that has been reported by a few individuals.

0.3.4

  • If the project being processed provides a top level WEB-INF/web.xml then grunt-war will use it in lieu of generating one automatically and will ignore the webxml options configured on the grunt-war task.

0.3.3

  • Removed a project dependency (not actively maintained) to try and address reports of a out-of-memory exception while generating a war file from 2 users.
  • Grammer corrections - Thank you Tim Russell (trussell314).

0.3.2

  • If the war_dist_folder does not already exist then grunt-war will create it. This saves the pain of adding this check before calling grunt-war. (skray)

0.3.1

  • Added options.webxml so that you may provide the raw contents of the web.xml when necessary. Requested by MartinDoyleUK.
     /* Example of how to return the contents of a file which will become the web.xml */
     webxml: function (opts) { 
                var fs = require('fs'); 
                return fs.readFileSync(pathToFileWithContentsOfHardCodedWebXML, 'binary'); 
             },

0.3.0 (Breaking changes)

  • This release allows for more flexible output paths inside the war bundle using Grunt's built-in dest write instead of the prior custom and less flexible rewrites. If you want the same behavior as in prior releases that did not depend on the 'dest' configuration then you must modify the task to something similiar to the following snippet. (shcarrico)
    ...
    war: {
      target: {
        options: { ... },
        files: [
            {
              expand: true,
              cwd: '<%= build_dir %>',
              src: ['**'],
              dest: ''  
            }
        ]
      }
    },
    ...

0.2.7

  • Fixed the inclusion of source files defined in previous WAR task configurations for all future WAR tasks. Example: a war.foo task includes dist/foo in foo.war, and a war.bar task includes dist/bar in bar.war. bar.war would include both dist/bar and the previously added dist/foo. (jbenner)

0.2.6

  • Fixed grunt-war encoding text incorrectly. Example: 'Količina' and 'Osveži' would get encoded as 'KoliÄ�ina' and 'Osveži'. (sobrle)

0.2.5

  • Updated peerDependencies to include node-zip. (@augier)

0.2.4

  • Now deletes options.war_name from options.war_dist_folder before trying to generate a new war file.

0.2.3

  • Fixed file names being trimmed when included in war. (@augier)

0.2.1

  • Fixed deployment issue resulting from using wrong option when generating webapp tag.

0.2.0

  • Renamed options.war_filename to options.war_name.
  • Added options.war_extras
  • Added options.webxml_webapp_extras.

0.1.4 Initial

grunt-war's People

Contributors

augier avatar jbenner avatar pthaden avatar shcarrico avatar wibobm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-war's Issues

broken on node 8

I'm getting errors when running grunt war on node 8. It works fine on v7. This is the output:

$ grunt war --stack
Running "war:dist" (war) task
Fatal error: Cannot read property 'pipesCount' of undefined
TypeError: Cannot read property 'pipesCount' of undefined
  at module.exports.Readable.pipe (_stream_readable.js:545:16)
  at module.exports.ZipArchiveOutputStream._smartStream (/Users/lloydb/Documents/objective/ecc-ui/node_modules/compress-commons/lib/archivers/zip/zip-archive-output-stream.js:184:11)
  at module.exports.ZipArchiveOutputStream._appendBuffer (/Users/lloydb/Documents/objective/ecc-ui/node_modules/compress-commons/lib/archivers/zip/zip-archive-output-stream.js:82:10)
  at module.exports.ArchiveOutputStream.entry (/Users/lloydb/Documents/objective/ecc-ui/node_modules/compress-commons/lib/archivers/archive-output-stream.js:86:10)
  at module.exports.ZipStream.entry (/Users/lloydb/Documents/objective/ecc-ui/node_modules/zip-stream/index.js:138:49)
  at Zip.append (/Users/lloydb/Documents/objective/ecc-ui/node_modules/archiver/lib/plugins/zip.js:53:15)
  at Archiver._moduleAppend (/Users/lloydb/Documents/objective/ecc-ui/node_modules/archiver/lib/core.js:172:16)
  at Archiver._onQueueTask (/Users/lloydb/Documents/objective/ecc-ui/node_modules/archiver/lib/core.js:370:8)
  at /Users/lloydb/Documents/objective/ecc-ui/node_modules/archiver/node_modules/async/dist/async.js:4045:9
  at process (/Users/lloydb/Documents/objective/ecc-ui/node_modules/archiver/node_modules/async/dist/async.js:2316:17)
  at Immediate._onImmediate (/Users/lloydb/Documents/objective/ecc-ui/node_modules/archiver/node_modules/async/dist/async.js:66:16)
  at runCallback (timers.js:800:20)
  at tryOnImmediate (timers.js:762:5)
  at processImmediate [as _immediateCallback] (timers.js:733:5)

Change encoding while creating war

Hi,

grunt-war changes encoding while combining files to war file.

In html and js files I have, for example, this stirngs : 'Količina' and 'Osveži'. In war file I get 'KoliÄ�ina' and 'Osveži'.

How can I force grunt-war not to change encoding?

war_extras not generating files

Hello,

I am using grunt-war to generate a weblogic.xml file along with the war, but the option seems not to be working.

I have tried it with this config:

var weblogic_contents
weblogic_contents  = '<?xml version="1.0" encoding="UTF-8"?>\n';
weblogic_contents += '<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">\n';
weblogic_contents += '  <jsp-descriptor>\n';
weblogic_contents += '    <keepgenerated>true</keepgenerated>\n';
weblogic_contents += '    <debug>true</debug>\n';
weblogic_contents += '  </jsp-descriptor>\n'
weblogic_contents += '  <context-root>APPNAME</context-root>\n';
weblogic_contents += '</weblogic-web-app>';

module.exports = {
  release: {
    options: {
      war_dist_folder: '<%= dist %>',
      war_name: '<%= appname %>',
      webxml_display_name: '<%= appname %>',
      webxml_webapp_version: '2.5', 
      war_extras: [{
        filename: 'WEB-INF/weblogic.xml',
        data: weblogic_contents
      }, {
        filename: 'testfile.txt',
        data: 'testfile content'
      }],
    // some more config
    },
    files: [{
        expand: true,
        cwd: '<%= app %>',
        src: [
          '**',
          // exclude build files
          '!index.tmpl.html',
          '!js/require.config.tmpl',
          '!js/components/**',
          '!styles/**/*.scss'
        ],
        dest: ''
    }]
  }

None of the files (weblogic.xml or testfile.txt) is generated, and tested also with the simplest of configs (only the testfile branch). The war file is generated though.

Identifier in camel case

Hi !

I think that the identifiers in Gruntfile.js should be in camel case. It makes jshint mad otherwise. :)

Task running recursively with no output, even with verbose set to true

Using the following setup:

war: {
      target: {
        options: {
          war_dist_folder: '<%= yeoman.exports %>',
          war_verbose: true,
          war_name: 'webmagic',
          webxml_welcome: 'index.html',
          webxml_display_name: 'Web Magic'
        },
        files: [
          {
            expand: true,
            src: ['<%=yeoman.dist %>/**']
          }
        ]
      }
    }

I get no output when running the task.

How to exclude files from the WAR?

I see the documentation list the src

src: ['**']

However, I can't find any documentation on how to manipulate this. I just want to exclude a file extension.

grunt-war not working at all

Hello,

I am trying to use grunt-war in my yeoman application and it's not working, I have my CPU running high and no errors are displayed on screen or anything like that.

Here is my task definition:

// Generate war file
    war: {
      target: {
        options: {
          war_verbose: true,
          war_dist_folder: '<%= yeoman.dist %>',
          war_name: 'dpaUi',
          webxml_welcome: 'index.html',
          webxml_display_name: 'Some description of the WAR'
        }
      },
      files: [
        {
          expand: true,
          cwd: '<%= yeoman.dist %>',
          src: ['**'],
          dst: '.'
        }
      ]
    }

I added the war step into default right after build and I tried it stand alone after doing a build. Nothing worked for me.

Caching

Is it possible to add a flag, that the index.html (the default file) is not cached? My index.html contains only very little code and mainly references to a javascript and a css files. Those files are revved with filerev, so the index.html would be the only file, which does not need to be cached.

Specify complete web.xml file to be used

I'm afraid I don't know enough about grunt-tasks yet to create a pull-request for this, but I'm assuming it might be quite simple? I was hoping it might be possible to specify a raw web.xml file that could be included in the WAR file. I have some relatively complex requirements for my web.xml file, which would most legibly be included in my project by putting them into a proper web.xml file, rather than by specifying all the properties in the Gruntfile.js file. Would this be possible to implement, please?

Version 0.4.1 target paths incorrect

Hello! While version 0.4.1 is much faster, it looks to be expanding the destination files incorrectly.

For the configuration (which worked in 0.3.x):
war: {
target: {
options: {
war_dist_folder: '<%= yeoman.dist %>',
war_verbose: true,
war_name: 'blah',
webxml_welcome: 'index.jsp',
webxml_display_name: 'Blah'
},
files: [{
expand: true,
cwd: '<%= yeoman.dist %>',
src: ['**'],
dest: ''
}]
}
}

The destination war contains the following (grepped for example purposes):
machine:dist user$ unzip -l bootcamp.war | grep scripts
0 11-13-14 10:07 scripts/dist/scripts/
0 11-13-14 10:07 scripts/config/dist/scripts/config/
3887 11-13-14 10:06 scripts/config/config-devint.54ef75e0.js/dist/scripts/config/config-devint.54ef75e0.js
2770 11-13-14 10:06 scripts/config/config-prod.19e47cc4.js/dist/scripts/config/config-prod.19e47cc4.js
3913 11-13-14 10:06 scripts/config/config-qa.77b01c4e.js/dist/scripts/config/config-qa.77b01c4e.js
19811 11-13-14 10:06 scripts/oldieshim.aef76c45.js/dist/scripts/oldieshim.aef76c45.js
123571 11-13-14 10:06 scripts/oldieshim.aef76c45.js.map/dist/scripts/oldieshim.aef76c45.js.map
256857 11-13-14 10:07 scripts/scripts.8f1949c2.js/dist/scripts/scripts.8f1949c2.js
778604 11-13-14 10:07 scripts/scripts.8f1949c2.js.map/dist/scripts/scripts.8f1949c2.js.map
772583 11-13-14 10:07 scripts/vendor.1ec64fc7.js/dist/scripts/vendor.1ec64fc7.js
4059868 11-13-14 10:07 scripts/vendor.1ec64fc7.js.map/dist/scripts/vendor.1ec64fc7.js.map

Notice that all the files are placed in scripts/config/[file name]/[directory]/[file name].

The actual files in that directory a
mac1:dist user$ ls -l scripts/
total 11768
drwxr-xr-x 5 user Domain Users 170B Nov 13 10:07 config/
-rw-r--r-- 1 user Domain Users 19K Nov 13 10:06 oldieshim.aef76c45.js
-rw-r--r-- 1 user Domain Users 121K Nov 13 10:06 oldieshim.aef76c45.js.map
-rw-r--r-- 1 user Domain Users 251K Nov 13 10:07 scripts.8f1949c2.js
-rw-r--r-- 1 user Domain Users 760K Nov 13 10:07 scripts.8f1949c2.js.map
-rw-r--r-- 1 user Domain Users 754K Nov 13 10:07 vendor.1ec64fc7.js
-rw-r--r-- 1 user Domain Users 3.9M Nov 13 10:07 vendor.1ec64fc7.js.map

This happens for all files in the WAR.

Thanks!

v0.4.7 copies files into WAR as folders

Since upgrading to 0.4.7 grunt-war has been producing war files full of folders, where files are expected. e.g. "index.html" file in root of war has become empty folder "index.html", and so on.

The problem goes away if we roll back to 0.4.6.

From the gruntfile (unchanged, works with <= 0.4.6, fails as above = 0.4.7):

    war: {
        dist: {
            options: {
                war_dist_folder: 'dist',
                war_verbose: true,
                war_name: 'foo',
                webxml: function() {
                    var fs = require('fs');
                    return fs.readFileSync('resources/web.xml');
                }
            },
            files: [{
                expand: true,
                cwd: 'dist/web',
                src: ['**']
            }]
        }
    }

Thanks.

Out of memory error - FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory Abort trap: 6

Get the following error when I invoke : 'grunt war'

FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
Abort trap: 6

seems to occur just after the lines,

adding node_modules/grunt-contrib-imagemin/node_modules/imagemin/node_modules/imagemin-optipng/node_modules/optipng-bin/node_modules/bin-wrapper/node_modules/download/node_modules/decompress/node_modules/adm-zip/test/assets/attributes_test/New folder/hidden.txt
adding node_modules/grunt-contrib-imagemin/node_modules/imagemin/node_modules/imagemin-optipng/node_modules/optipng-bin/node_modules/bin-wrapper/node_modules/download/node_modules/decompress/node_modules/adm-zip/test/assets/attributes_test/New folder/hidden_readonly.txt

I'm running on Mavericks with
node: v0.10.29
grunt-cli v0.1.13
grunt v0.4.5

regards,
Mo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.