-
Notifications
You must be signed in to change notification settings - Fork 110
/
dagger.gradle
178 lines (156 loc) · 5.97 KB
/
dagger.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Manages classpath and IDE annotation processing config for dagger.
//
// setup:
// Add the following to your root build.gradle
//
// apply plugin: 'idea'
// subprojects {
// apply from: rootProject.file('dagger.gradle')
// }
//
// do not use gradle integration of the ide. instead generate and import like so:
//
// ./gradlew clean cleanEclipse cleanIdea eclipse idea
//
// known limitations:
// as output folders include generated classes, you may need to run clean a few times.
// incompatible with android plugin as it applies the java plugin
// unnecessarily applies both eclipse and idea plugins even if you don't use them
// suffers from the normal non-IDE eclipse integration where nested projects don't import properly.
// change your structure to flattened to avoid this.
//
// deprecated by: https://github.com/Netflix/gradle-template/issues/8
//
// original design: cfieber
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
if (!project.hasProperty('daggerVersion')) {
ext {
daggerVersion = "1.2.2"
}
}
configurations {
daggerCompiler {
visible false
}
}
configurations.all {
resolutionStrategy {
eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'com.squareup.dagger') {
details.useVersion daggerVersion
}
}
}
}
def annotationGeneratedSources = file('.generated/src')
def annotationGeneratedTestSources = file('.generated/test')
task prepareAnnotationGeneratedSourceDirs(overwrite: true) << {
annotationGeneratedSources.mkdirs()
annotationGeneratedTestSources.mkdirs()
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
sourceSets {
main {
java {
compileClasspath += configurations.daggerCompiler
}
}
test {
java {
compileClasspath += configurations.daggerCompiler
}
}
}
dependencies {
compile "com.squareup.dagger:dagger:${project.daggerVersion}"
daggerCompiler "com.squareup.dagger:dagger-compiler:${project.daggerVersion}"
}
rootProject.idea.project.ipr.withXml { projectXml ->
projectXml.asNode().component.find { it.@name == 'CompilerConfiguration' }.annotationProcessing[0].replaceNode {
annotationProcessing {
profile(default: true, name: 'Default', enabled: true) {
sourceOutputDir name: relativePath(annotationGeneratedSources)
sourceTestOutputDir name: relativePath(annotationGeneratedTestSources)
outputRelativeToContentRoot value: true
processorPath useClasspath: true
}
}
}
}
tasks.ideaModule.dependsOn(prepareAnnotationGeneratedSourceDirs)
idea.module {
scopes.PROVIDED.plus += [project.configurations.daggerCompiler]
iml.withXml { xml->
def moduleSource = xml.asNode().component.find { it.@name = 'NewModuleRootManager' }.content[0]
moduleSource.appendNode('sourceFolder', [url: "file://\$MODULE_DIR\$/${relativePath(annotationGeneratedSources)}", isTestSource: false])
moduleSource.appendNode('sourceFolder', [url: "file://\$MODULE_DIR\$/${relativePath(annotationGeneratedTestSources)}", isTestSource: true])
}
}
tasks.eclipseClasspath.dependsOn(prepareAnnotationGeneratedSourceDirs)
eclipse.classpath {
plusConfigurations += [project.configurations.daggerCompiler]
}
tasks.eclipseClasspath {
doLast {
eclipse.classpath.file.withXml {
it.asNode().children()[0] + {
classpathentry(kind: 'src', path: relativePath(annotationGeneratedSources)) {
attributes {
attribute name: 'optional', value: true
}
}
}
}
}
}
// http://forums.gradle.org/gradle/topics/eclipse_generated_files_should_be_put_in_the_same_place_as_the_gradle_generated_files
Map pathMappings = [:];
SourceSetContainer sourceSets = project.sourceSets;
sourceSets.each { SourceSet sourceSet ->
String relativeJavaOutputDirectory = project.relativePath(sourceSet.output.classesDir);
String relativeResourceOutputDirectory = project.relativePath(sourceSet.output.resourcesDir);
sourceSet.java.getSrcDirTrees().each { DirectoryTree sourceDirectory ->
String relativeSrcPath = project.relativePath(sourceDirectory.dir.absolutePath);
pathMappings[relativeSrcPath] = relativeJavaOutputDirectory;
}
sourceSet.resources.getSrcDirTrees().each { DirectoryTree resourceDirectory ->
String relativeResourcePath = project.relativePath(resourceDirectory.dir.absolutePath);
pathMappings[relativeResourcePath] = relativeResourceOutputDirectory;
}
}
project.eclipse.classpath.file {
whenMerged { classpath ->
classpath.entries.findAll { entry ->
return entry.kind == 'src';
}.each { entry ->
if(pathMappings.containsKey(entry.path)) {
entry.output = pathMappings[entry.path];
}
}
}
}
eclipse.jdt.file.withProperties { props ->
props.setProperty('org.eclipse.jdt.core.compiler.processAnnotations', 'enabled')
}
tasks.eclipseJdt {
doFirst {
def aptPrefs = file('.settings/org.eclipse.jdt.apt.core.prefs')
aptPrefs.parentFile.mkdirs()
aptPrefs.text = """\
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genSrcDir=${relativePath(annotationGeneratedSources)}
org.eclipse.jdt.apt.reconcileEnabled=true
""".stripIndent()
file('.factorypath').withWriter {
new groovy.xml.MarkupBuilder(it).'factorypath' {
project.configurations.daggerCompiler.files.each { dep ->
'factorypathentry' kind: 'EXTJAR', id: dep.absolutePath, enabled: true, runInBatchMode: false
}
}
}
}
}