diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 753217c70..69ba6b354 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -15,3 +15,4 @@ Han-Wen Nienhuys Christopher Parsons Michael Staib Manfred Touron +Jingwen Chen diff --git a/java-maven/.gitignore b/java-maven/.gitignore new file mode 100644 index 000000000..6a6a799a8 --- /dev/null +++ b/java-maven/.gitignore @@ -0,0 +1,5 @@ +bazel-bin +bazel-genfiles +bazel-java-maven +bazel-out +bazel-testlogs diff --git a/java-maven/BUILD b/java-maven/BUILD new file mode 100644 index 000000000..7a17c63f2 --- /dev/null +++ b/java-maven/BUILD @@ -0,0 +1,23 @@ +package(default_visibility = ["//visibility:public"]) + +java_library( + name = "java-maven-lib", + srcs = glob(["src/main/java/com/example/myproject/*.java"]), + deps = ["@com_google_guava_guava//jar"], +) + +java_binary( + name = "java-maven", + main_class = "com.example.myproject.App", + runtime_deps = [":java-maven-lib"], +) + +java_test( + name = "tests", + srcs = glob(["src/test/java/com/example/myproject/*.java"]), + test_class = "com.example.myproject.TestApp", + deps = [ + ":java-maven-lib", + "@com_google_guava_guava//jar", + ], +) diff --git a/java-maven/README.md b/java-maven/README.md new file mode 100644 index 000000000..87b1b77d5 --- /dev/null +++ b/java-maven/README.md @@ -0,0 +1,24 @@ +Maven Java application +---------------------- + +This project demonstrates the usage of Bazel to retrieve dependencies from +Maven repositories. + +To build this example, you will need to +[install Bazel](http://bazel.io/docs/install.html). + +The Java application makes use of a library in [Guava](https://github.com/google/guava), which is downloaded from a remote repository using Maven. + +This application demonstrates the usage of rules `maven_jar` and `maven_server` to configure dependencies. The dependencies are configured in the `WORKSPACE` file. + +Build the application by running: + +``` +$ bazel build :java-maven +``` + +Test the application by running: + +``` +$ bazel test :tests +``` diff --git a/java-maven/WORKSPACE b/java-maven/WORKSPACE new file mode 100644 index 000000000..44ea366fc --- /dev/null +++ b/java-maven/WORKSPACE @@ -0,0 +1,10 @@ +maven_jar( + name = "com_google_guava_guava", + artifact = "com.google.guava:guava:18.0", + server = "maven_uk_server", +) + +maven_server( + name = "maven_uk_server", + url = "http://uk.maven.org/maven2", +) diff --git a/java-maven/src/main/java/com/example/myproject/App.java b/java-maven/src/main/java/com/example/myproject/App.java new file mode 100644 index 000000000..79f1709f1 --- /dev/null +++ b/java-maven/src/main/java/com/example/myproject/App.java @@ -0,0 +1,20 @@ +package com.example.myproject; + +import com.google.common.primitives.Ints; + +/** + * This application compares two numbers, using the Ints.compare + * method from Guava. + */ +public class App { + + public static int compare(int a, int b) { + return Ints.compare(a, b); + } + + public static void main(String... args) throws Exception { + App app = new App(); + System.out.println("Success: " + app.compare(2, 1)); + } + +} diff --git a/java-maven/src/test/java/com/example/myproject/TestApp.java b/java-maven/src/test/java/com/example/myproject/TestApp.java new file mode 100644 index 000000000..f5e24856e --- /dev/null +++ b/java-maven/src/test/java/com/example/myproject/TestApp.java @@ -0,0 +1,17 @@ +package com.example.myproject; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +/** + * Tests for correct dependency retrieval with maven rules. + */ +public class TestApp { + + @Test + public void testCompare() throws Exception { + App app = new App(); + assertEquals("should return 0 when both numbers are equal", 0, app.compare(1, 1)); + } + +}