Skip to content

Commit

Permalink
Implement an example maven project (#15)
Browse files Browse the repository at this point in the history
* Implement an example maven project

* Removed glob for srcs
  • Loading branch information
jin authored and kchodorow committed Aug 29, 2016
1 parent ad1e034 commit 5547cbe
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Han-Wen Nienhuys <hanwen@google.com>
Christopher Parsons <cparsons@google.com>
Michael Staib <mstaib@google.com>
Manfred Touron <m@42.am>
Jingwen Chen <jingwen@google.com>
5 changes: 5 additions & 0 deletions java-maven/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bazel-bin
bazel-genfiles
bazel-java-maven
bazel-out
bazel-testlogs
23 changes: 23 additions & 0 deletions java-maven/BUILD
Original file line number Diff line number Diff line change
@@ -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",
],
)
24 changes: 24 additions & 0 deletions java-maven/README.md
Original file line number Diff line number Diff line change
@@ -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
```
10 changes: 10 additions & 0 deletions java-maven/WORKSPACE
Original file line number Diff line number Diff line change
@@ -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",
)
20 changes: 20 additions & 0 deletions java-maven/src/main/java/com/example/myproject/App.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
17 changes: 17 additions & 0 deletions java-maven/src/test/java/com/example/myproject/TestApp.java
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit 5547cbe

Please sign in to comment.