diff options
author | bcorso <bcorso@google.com> | 2020-06-10 12:29:20 -0700 |
---|---|---|
committer | Chris Povirk <beigetangerine@gmail.com> | 2020-06-12 13:49:18 -0400 |
commit | 5ed8544a80517bc63e0faf6c23885f338f320ade (patch) | |
tree | c1c25fbdbe753885dd2d88f3572225040534e3e7 /examples | |
parent | 2c8d0d1dd5cb46db077a67f49c831f79aabdc2ae (diff) | |
download | dagger2-5ed8544a80517bc63e0faf6c23885f338f320ade.tar.gz |
Add back maven example and add build to Travis
This CL also updates `.travis.yml` to run `run-local-tests.sh` to make it more in line with what we do internally.
RELNOTES=Add back maven example
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=315743889
Diffstat (limited to 'examples')
11 files changed, 435 insertions, 0 deletions
diff --git a/examples/maven/coffee/pom.xml b/examples/maven/coffee/pom.xml new file mode 100644 index 000000000..a196d997d --- /dev/null +++ b/examples/maven/coffee/pom.xml @@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (C) 2012 The Dagger Authors. + + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>com.google.dagger.examples</groupId> + <artifactId>parent</artifactId> + <version>LOCAL-SNAPSHOT</version> + </parent> + + <artifactId>coffee</artifactId> + <name>Examples: Coffee</name> + + <dependencies> + <dependency> + <!-- Force the correct version of Guava to be on the classpath. --> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + <dependency> + <groupId>com.google.dagger</groupId> + <artifactId>dagger</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.6.1</version> + <configuration> + <annotationProcessorPaths> + <path> + <groupId>com.google.dagger</groupId> + <artifactId>dagger-compiler</artifactId> + <version>${project.version}</version> + </path> + </annotationProcessorPaths> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/examples/maven/coffee/src/main/java/example/dagger/CoffeeApp.java b/examples/maven/coffee/src/main/java/example/dagger/CoffeeApp.java new file mode 100644 index 000000000..723ab919c --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/CoffeeApp.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +import dagger.Component; +import javax.inject.Singleton; + +/** The main app responsible for brewing the coffee and printing the logs. */ +public class CoffeeApp { + @Singleton + @Component( + modules = { + HeaterModule.class, + PumpModule.class + } + ) + public interface CoffeeShop { + CoffeeMaker maker(); + CoffeeLogger logger(); + } + + public static void main(String[] args) { + CoffeeShop coffeeShop = DaggerCoffeeApp_CoffeeShop.builder().build(); + coffeeShop.maker().brew(); + coffeeShop.logger().logs().forEach(log -> System.out.println(log)); + } +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/CoffeeLogger.java b/examples/maven/coffee/src/main/java/example/dagger/CoffeeLogger.java new file mode 100644 index 000000000..16d2bdbc6 --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/CoffeeLogger.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +import java.util.ArrayList; +import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; + +/** A logger to logs steps while brewing coffee. */ +@Singleton +public final class CoffeeLogger { + private final List<String> logs = new ArrayList<>(); + + @Inject + CoffeeLogger() {} + + public void log(String msg) { + logs.add(msg); + } + + public List<String> logs() { + return new ArrayList<>(logs); + } +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/CoffeeMaker.java b/examples/maven/coffee/src/main/java/example/dagger/CoffeeMaker.java new file mode 100644 index 000000000..20c1f27a8 --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/CoffeeMaker.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +import dagger.Lazy; +import javax.inject.Inject; + +/** A coffee maker to brew the coffee. */ +public class CoffeeMaker { + private final CoffeeLogger logger; + private final Lazy<Heater> heater; // Create a possibly costly heater only when we use it. + private final Pump pump; + + @Inject + CoffeeMaker(CoffeeLogger logger, Lazy<Heater> heater, Pump pump) { + this.logger = logger; + this.heater = heater; + this.pump = pump; + } + + public void brew() { + heater.get().on(); + pump.pump(); + logger.log(" [_]P coffee! [_]P "); + heater.get().off(); + } +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/ElectricHeater.java b/examples/maven/coffee/src/main/java/example/dagger/ElectricHeater.java new file mode 100644 index 000000000..567c7aa6b --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/ElectricHeater.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +import javax.inject.Inject; + +/** An electric heater to heat the coffee. */ +public class ElectricHeater implements Heater { + + private final CoffeeLogger logger; + private boolean heating; + + @Inject + ElectricHeater(CoffeeLogger logger) { + this.logger = logger; + } + + @Override + public void on() { + this.heating = true; + logger.log("~ ~ ~ heating ~ ~ ~"); + } + + @Override + public void off() { + this.heating = false; + } + + @Override + public boolean isHot() { + return heating; + } +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/Heater.java b/examples/maven/coffee/src/main/java/example/dagger/Heater.java new file mode 100644 index 000000000..3d6d1f2b4 --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/Heater.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +/** A heater to heat the coffee. */ +public interface Heater { + void on(); + void off(); + boolean isHot(); +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/HeaterModule.java b/examples/maven/coffee/src/main/java/example/dagger/HeaterModule.java new file mode 100644 index 000000000..fb8c9691b --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/HeaterModule.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +import dagger.Binds; +import dagger.Module; +import javax.inject.Singleton; + +@Module +interface HeaterModule { + @Binds + @Singleton + Heater bindHeater(ElectricHeater impl); +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/Pump.java b/examples/maven/coffee/src/main/java/example/dagger/Pump.java new file mode 100644 index 000000000..712b21c43 --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/Pump.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +/** A pump to pump the coffee. */ +public interface Pump { + void pump(); +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/PumpModule.java b/examples/maven/coffee/src/main/java/example/dagger/PumpModule.java new file mode 100644 index 000000000..202fed3b6 --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/PumpModule.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +import dagger.Binds; +import dagger.Module; + +@Module +abstract class PumpModule { + @Binds + abstract Pump providePump(Thermosiphon pump); +} diff --git a/examples/maven/coffee/src/main/java/example/dagger/Thermosiphon.java b/examples/maven/coffee/src/main/java/example/dagger/Thermosiphon.java new file mode 100644 index 000000000..d94c33f22 --- /dev/null +++ b/examples/maven/coffee/src/main/java/example/dagger/Thermosiphon.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * 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. + */ + +package example.dagger; + +import javax.inject.Inject; + +/** A thermosiphon to pump the coffee. */ +public class Thermosiphon implements Pump { + private final CoffeeLogger logger; + private final Heater heater; + + @Inject + Thermosiphon(CoffeeLogger logger, Heater heater) { + this.logger = logger; + this.heater = heater; + } + + @Override + public void pump() { + if (heater.isHot()) { + logger.log("=> => pumping => =>"); + } + } +} diff --git a/examples/maven/pom.xml b/examples/maven/pom.xml new file mode 100644 index 000000000..be21b9649 --- /dev/null +++ b/examples/maven/pom.xml @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (C) 2013 The Dagger Authors. + + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.sonatype.oss</groupId> + <artifactId>oss-parent</artifactId> + <version>9</version> + </parent> + + <groupId>com.google.dagger.examples</groupId> + <artifactId>parent</artifactId> + <packaging>pom</packaging> + <name>Examples</name> + <version>LOCAL-SNAPSHOT</version> + + <modules> + <module>coffee</module> + </modules> + + <!-- Example-only dependencies. --> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>com.google.dagger</groupId> + <artifactId>dagger</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.google.dagger</groupId> + <artifactId>dagger-compiler</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>26.0-jre</version> + </dependency> + </dependencies> + </dependencyManagement> + + <build> + <pluginManagement> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + </plugins> + </pluginManagement> + </build> +</project> |