Skip to content

bmuschko/rules_java_war

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bazel rules for creating a Java Web Archive (WAR) Build Status

Overview

The rules provide a way for creating a Java Web Archive (WAR). The project is compatible with the standard rules_java and rules_jvm_external.

Setup

The easiest way to use the rules is by adding the following to your WORKSPACE file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JAVA_WAR_TAG = "0.1.0"

http_archive(
    name = "io_bazel_rules_java_war",
    strip_prefix = "rules_java_war-%s" % RULES_JAVA_WAR_TAG,
    url = "https://github.com/bmuschko/rules_java_war/archive/%s.tar.gz" % RULES_JAVA_WAR_TAG,
    sha256 = "38011f979713c4aefd43ab56675ce4c6c14bc949b128c3a303f1f57ebe4bfeac",
)

Basic Example

Suppose you are following the typical directory structure for a Java web application:

.
├── BUILD
├── WORKSPACE
└── src
    └── main
        ├── java
        │   └── com
        │       └── bmuschko
        │           └── web
        │               └── SimpleServlet.java
        └── webapp
            ├── WEB-INF
            │   └── web.xml
            ├── css
            │   └── style.css
            ├── index.html
            └── js
                └── dynamic.js

To build the WAR file for the application, a BUILD on the root level could look as follows:

load("@io_bazel_rules_java_war//java_war:defs.bzl", "java_war")

java_war(
    name = "web-app",
    java_srcs = glob(["src/main/java/**/*.java"]),
    deps = [
        "@maven//:org_mortbay_jetty_servlet_api",
        "@maven//:ch_qos_logback_logback_classic",
    ],
)

Under the hood, the macro named java_war will generate the java_library of the application and then include it as dependency for the WAR file.

$ bazel build //:web-app
INFO: Analyzed target //:web-app (17 packages loaded, 531 targets configured).
INFO: Found 1 target...
Target //:web-app up-to-date:
bazel-bin/web-app.war
INFO: Elapsed time: 7.440s, Critical Path: 5.30s
INFO: 4 processes: 3 darwin-sandbox, 1 worker.
INFO: Build completed successfully, 5 total actions

The result WAR file contains the content shown below. The rule will include the transitive closure of runtime dependencies. The dependency org_mortbay_jetty_servlet_api was declared as "compile-only" dependency and therefore isn’t included.

$ cd bazel-bin
$ jar -tf web-app.war
WEB-INF/web.xml
css/style.css
index.html
js/dynamic.js
WEB-INF/lib/liblibextdeps.jar
WEB-INF/lib/logback-classic-1.1.2.jar
WEB-INF/lib/slf4j-api-1.7.6.jar
WEB-INF/lib/logback-core-1.1.2.jar

API

war

war(name, compression, deps, web_app_root, web_app_srcs)

Rule for generating a Java Web Archive (WAR).

Attributes

name Name; required

A unique name for this target.

compression Boolean; required

Enables compression for the WAR file.

deps List of labels; optional

Dependencies for this target.

web_app_root String; required

Root directory containing web application files (e.g. web.xml, CSS or JavaScript files).

web_app_srcs List of labels; required

Source files to be included fromt the web application root directory.

java_war

java_war(name, web_app_dir, java_srcs, deps, compression, kwargs)

Creates a Java Web Archive (WAR).

Automatically creates a Java library and bundles it with the web application files and any dependencies. For more information on the internal structure of a WAR file, see the official documentation.

Parameters

name required.

A unique name for this rule.

web_app_dir optional. default is "src/main/webapp"

The root web application directory.

java_srcs optional. default is []

Java source files for compilation.

deps optional. default is []

Dependencies for this java_library target.

compression optional. default is False

Enables compression for the WAR.

kwargs optional.