Skip to content
This repository was archived by the owner on Jan 22, 2024. It is now read-only.

Commit 84cfc01

Browse files
committed
Dagger-Maven example added
1 parent 5b4cc8d commit 84cfc01

File tree

11 files changed

+287
-0
lines changed

11 files changed

+287
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target
2+
.idea
3+
4+
.settings/
5+
.classpath
6+
.project

maven/dagger-maven-example/pom.xml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>org.jetbrains.kotlin.examples</groupId>
9+
<artifactId>dagger-maven-example</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
12+
<properties>
13+
<kotlin.version>1.1-SNAPSHOT</kotlin.version>
14+
<junit.version>4.12</junit.version>
15+
<main.class>coffee.CoffeeApp</main.class>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.google.dagger</groupId>
22+
<artifactId>dagger</artifactId>
23+
<version>2.9</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.jetbrains.kotlin</groupId>
27+
<artifactId>kotlin-stdlib</artifactId>
28+
<version>${kotlin.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>${junit.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<artifactId>kotlin-maven-plugin</artifactId>
42+
<groupId>org.jetbrains.kotlin</groupId>
43+
<version>${kotlin.version}</version>
44+
<executions>
45+
<execution>
46+
<id>kapt</id>
47+
<goals>
48+
<goal>kapt</goal>
49+
</goals>
50+
<configuration>
51+
<sourceDirs>
52+
<sourceDir>src/main/kotlin</sourceDir>
53+
<sourceDir>src/main/java</sourceDir>
54+
</sourceDirs>
55+
<annotationProcessorPaths>
56+
<annotationProcessorPath>
57+
<groupId>com.google.dagger</groupId>
58+
<artifactId>dagger-compiler</artifactId>
59+
<version>2.9</version>
60+
</annotationProcessorPath>
61+
</annotationProcessorPaths>
62+
</configuration>
63+
</execution>
64+
<execution>
65+
<id>compile</id>
66+
<goals>
67+
<goal>compile</goal>
68+
</goals>
69+
<configuration>
70+
<sourceDirs>
71+
<sourceDir>src/main/kotlin</sourceDir>
72+
<sourceDir>src/main/java</sourceDir>
73+
</sourceDirs>
74+
</configuration>
75+
</execution>
76+
<execution>
77+
<id>test-kapt</id>
78+
<goals>
79+
<goal>test-kapt</goal>
80+
</goals>
81+
<configuration>
82+
<sourceDirs>
83+
<sourceDir>src/test/kotlin</sourceDir>
84+
<sourceDir>src/test/java</sourceDir>
85+
</sourceDirs>
86+
<annotationProcessorPaths>
87+
<annotationProcessorPath>
88+
<groupId>com.google.dagger</groupId>
89+
<artifactId>dagger-compiler</artifactId>
90+
<version>2.9</version>
91+
</annotationProcessorPath>
92+
</annotationProcessorPaths>
93+
</configuration>
94+
</execution>
95+
<execution>
96+
<id>test-compile</id>
97+
<goals>
98+
<goal>test-compile</goal>
99+
</goals>
100+
<configuration>
101+
<sourceDirs>
102+
<sourceDir>src/test/kotlin</sourceDir>
103+
<sourceDir>src/test/java</sourceDir>
104+
<sourceDir>target/generated-sources/kapt/test</sourceDir>
105+
</sourceDirs>
106+
</configuration>
107+
</execution>
108+
</executions>
109+
</plugin>
110+
<plugin>
111+
<groupId>org.apache.maven.plugins</groupId>
112+
<artifactId>maven-compiler-plugin</artifactId>
113+
<version>3.5.1</version>
114+
<configuration>
115+
<proc>none</proc>
116+
<source>1.6</source>
117+
<target>1.6</target>
118+
</configuration>
119+
<executions>
120+
<!-- Replacing default-compile as it is treated specially by maven -->
121+
<execution>
122+
<id>default-compile</id>
123+
<phase>none</phase>
124+
</execution>
125+
<!-- Replacing default-testCompile as it is treated specially by maven -->
126+
<execution>
127+
<id>default-testCompile</id>
128+
<phase>none</phase>
129+
</execution>
130+
<execution>
131+
<id>java-compile</id>
132+
<phase>compile</phase>
133+
<goals>
134+
<goal>compile</goal>
135+
</goals>
136+
</execution>
137+
<execution>
138+
<id>java-test-compile</id>
139+
<phase>test-compile</phase>
140+
<goals> <goal>testCompile</goal> </goals>
141+
</execution>
142+
</executions>
143+
</plugin>
144+
</plugins>
145+
</build>
146+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package coffee
2+
3+
import dagger.Component
4+
import javax.inject.Singleton
5+
6+
object CoffeeApp {
7+
@Singleton
8+
@Component(modules = arrayOf(DripCoffeeModule::class))
9+
interface Coffee {
10+
fun maker(): CoffeeMaker
11+
}
12+
13+
@JvmStatic
14+
fun main(args: Array<String>) {
15+
val coffee = DaggerCoffeeApp_Coffee.builder().build()
16+
coffee.maker().brew()
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package coffee
2+
3+
import dagger.Lazy
4+
import javax.inject.Inject
5+
6+
class CoffeeMaker @Inject constructor(
7+
private val heater: Lazy<Heater>,
8+
private val pump: Pump
9+
) {
10+
11+
fun brew() {
12+
heater.get().on()
13+
pump.pump()
14+
println(" [_]P coffee! [_]P ")
15+
heater.get().off()
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package coffee
2+
3+
import dagger.Module
4+
import dagger.Provides
5+
import javax.inject.Singleton
6+
7+
@Module(includes = arrayOf(PumpModule::class))
8+
class DripCoffeeModule {
9+
@Provides @Singleton
10+
fun provideHeater(): Heater {
11+
return ElectricHeater()
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package coffee
2+
3+
open class ElectricHeater : Heater {
4+
override var isHot: Boolean = false
5+
6+
override fun on() {
7+
println("~ ~ ~ heating ~ ~ ~")
8+
this.isHot = true
9+
}
10+
11+
override fun off() {
12+
this.isHot = false
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package coffee
2+
3+
interface Heater {
4+
fun on()
5+
fun off()
6+
val isHot: Boolean
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package coffee
2+
3+
interface Pump {
4+
fun pump()
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package coffee
2+
3+
import dagger.Binds
4+
import dagger.Module
5+
6+
@Module
7+
abstract class PumpModule {
8+
@Binds
9+
abstract fun providePump(pump: Thermosiphon): Pump
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package coffee
2+
3+
import javax.inject.Inject
4+
5+
class Thermosiphon @Inject
6+
constructor(private val heater: Heater) : Pump {
7+
override fun pump() {
8+
if (heater.isHot) {
9+
println("=> => pumping => =>")
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)