Skip to content

Commit

Permalink
Add basic support for building Java AppEngine WAR
Browse files Browse the repository at this point in the history
This support is based on kchodorow@'s genrule to build the Bazel's dashboard
AppEngine and was tested with it.

--
MOS_MIGRATED_REVID=97135934
  • Loading branch information
damienmg committed Jun 29, 2015
1 parent cd3df4c commit 5b16b19
Show file tree
Hide file tree
Showing 5 changed files with 519 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tools/build_rules/appengine/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# A target to ensure the servlet-api is not linked in the webapp.
java_library(
name = "javax.servlet.api",
neverlink = 1,
visibility = ["//visibility:public"],
exports = ["@javax-servlet-api//jar:jar"],
)
202 changes: 202 additions & 0 deletions tools/build_rules/appengine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
# Java AppEngine Rules for Bazel

## Overview

These build rules are used for building
[Java AppEngine](https://cloud.google.com/appengine/docs/java/)
application with Bazel. It does not aim at general WebApplication
support but can be easily modified to handle a standard WebApplication.

* [Setup](#setup)
* [Basic Example](#basic-example)
* [Build Rule Reference](#reference)
* [`appengine_war`](#appengine_war)
* [`java_war`](#java_war)

<a name="setup"></a>
## Setup

To be able to use the Java AppEngine rules, you must make the AppEngine SDK
available to Bazel. The easiest way to do so is by copying the content of
`appengine.WORKSPACE` to your workspace file.

<a name="basic-example"></a>
## Basic Example

Suppose you have the following directory structure for a simple AppEngine
application:

```
[workspace]/
WORKSPACE
hello_app/
BUILD
java/my/webapp/
TestServlet.java
webapp/
index.html
webapp/WEB-INF
web.xml
appengine-web.xml
```

Then, to build your webapp, your `hello_app/BUILD` can look like:

```python
load("/tools/build_rules/appengine/appengine", "appengine_war")

java_library(
name = "mylib",
srcs = ["java/my/webapp/TestServlet.java"],
deps = [
"//external:appengine/java/api",
"//external:javax/servlet/api",
],
)

appengine_war(
name = "myapp",
jars = [":mylib"],
data = glob(["webapp/**"]),
data_path = "webapp",
)
```

For simplicity, you can use the `java_war` rule to build an app from source.
Your `hello_app/BUILD` file would then look like:

```python
load("/tools/build_rules/appengine/appengine", "java_war")

java_war(
name = "myapp",
srcs = ["java/my/webapp/TestServlet.java"],
data = glob(["webapp/**"]),
data_path = "webapp",
deps = [
"//external:appengine/java/api",
"//external:javax/servlet/api",
],
)
```

You can then build the application with `bazel build //hello_app:myapp` and
run in it a development server with `bazel run //hello_app:myapp`. This will
bind a test server on port 8080. If you wish to select another port,
simply append the `--port=12345` to the command-line.

<a name="reference"></a>
## Build Rule Reference [reference]

<a name="appengine_war"></a>
### `appengine_war`

`appengine_war(name, jars, data, data_path)`

<table>
<thead>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>name</code></td>
<td>
<code>Name, required</code>
<p>A unique name for this rule.</p>
</td>
</tr>
<tr>
<td><code>jars</code></td>
<td>
<code>List of labels, required</code>
<p>
List of JAR files that will be uncompressed as the code for the
Web Application.
</p>
<p>
If it is a `java_library` or a `java_import`, the
JAR from the runtime classpath will be added in the `lib` directory
of the Web Application.
</p>
</td>
</tr>
<tr>
<td><code>data</code></td>
<td>
<code>List of files, optional</code>
<p>List of files used by the Web Application at runtime.</p>
<p>
This attribute can be used to specify the list of resources to
be included into the WAR file.
</p>
</td>
</tr>
<tr>
<td><code>data_path</code></td>
<td>
<code>String, optional</code>
<p>Root path of the data.</p>
<p>
The directory structure from the data is preserved inside the
WebApplication but a prefix path determined by `data_path`
is removed from the the directory structure. This path can
be absolute from the workspace root if starting with a `/` or
relative to the rule's directory. It is set to `.` by default.
</p>
</td>
</tr>
</tbody>
</table>

<a name="java_war">
### `java_war`

`java_war(name, data, data_path, **kwargs)`

<table>
<thead>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>name</code></td>
<td>
<code>Name, required</code>
<p>A unique name for this rule.</p>
</td>
</tr>
<tr>
<td><code>data</code></td>
<td>
<code>List of labels, optional</code>
<p>List of files used by the Web Application at runtime.</p>
<p>Passed to the <a href="#appengine_war">appengine_war</a> rule.</p>
</td>
</tr>
<tr>
<td><code>data_path</code></td>
<td>
<code>String, optional</code>
<p>Root path of the data.</p>
<p>Passed to the <a href="#appengine_war">appengine_war</a> rule.</p>
</td>
</tr>
<tr>
<td><code>**kwargs</code></td>
<td>
<code>see <a href="http://bazel.io/docs/build-encyclopedia.html#java_library">java_library</a></code>
<p>
The other arguments of this rule will be passed to build a `java_library`
that will be passed in the `jar` arguments of a
<a href="#appengine_war">appengine_war</a> rule.
</p>
</td>
</tr>
</tbody>
</table>
34 changes: 34 additions & 0 deletions tools/build_rules/appengine/appengine.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# BUILD file to use the Java AppEngine SDK with a remote repository.
java_import(
name = "jars",
jars = glob(["**/*.jar"]),
visibility = ["//visibility:public"],
)

java_import(
name = "api",
jars = ["appengine-java-sdk-1.9.23/lib/impl/appengine-api.jar"],
visibility = ["//visibility:public"],
neverlink = 1,
)

filegroup(
name = "sdk",
srcs = glob(["appengine-java-sdk-1.9.23/**"]),
visibility = ["//visibility:public"],
path = "appengine-java-sdk-1.9.23",
)
46 changes: 46 additions & 0 deletions tools/build_rules/appengine/appengine.WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# WORKSPACE file example to download Java AppEngine dependencies.
new_http_archive(
name = "appengine-java",
url = "http://central.maven.org/maven2/com/google/appengine/appengine-java-sdk/1.9.23/appengine-java-sdk-1.9.23.zip",
sha256 = "05e667036e9ef4f999b829fc08f8e5395b33a5a3c30afa9919213088db2b2e89",
build_file = "tools/build_rules/appengine/appengine.BUILD",
)

bind(
name = "appengine/java/sdk",
actual = "@appengine-java//:sdk",
)

bind(
name = "appengine/java/api",
actual = "@appengine-java//:api",
)

bind(
name = "appengine/java/jars",
actual = "@appengine-java//:jars",
)

maven_jar(
name = "javax-servlet-api",
artifact = "javax.servlet:servlet-api:2.5",
)

bind(
name = "javax/servlet/api",
actual = "//tools/build_rules/appengine:javax.servlet.api",
)
Loading

0 comments on commit 5b16b19

Please sign in to comment.