Skip to content

Commit 9da545f

Browse files
author
Alexander Litus
authored
Merge pull request #166 from SpineEventEngine/refactor-gradle-build-scripts
Refactor Gradle build scripts
2 parents 05c2cb8 + 6200d7b commit 9da545f

File tree

16 files changed

+299
-296
lines changed

16 files changed

+299
-296
lines changed

build.gradle

Lines changed: 54 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1-
final String CREDENTIALS_LOCATION = 'credentials.properties';
1+
2+
project.ext {
3+
// NOTE: when updating Protobuf dependency, please check that KnownTypes.addStandardProtobufTypes() method
4+
// is updated with new Message types introduced in the new version of Protobuf.
5+
PROTOBUF_VERSION = '3.0.0-beta-3'
6+
PROTOBUF_DEPENDENCY = "com.google.protobuf:protoc:${PROTOBUF_VERSION}"
7+
GRPC_VERSION = '0.14.0'
8+
SLF4J_VERSION = '1.7.21'
9+
10+
SPINE_PROTOBUF_PLUGIN_ID = 'org.spine3.tools.protobuf-plugin'
11+
PUBLISH_PLUGIN_PATH = "$projectDir/gradle/plugins/publish.gradle"
12+
13+
CREDENTIALS_FILE_PATH = 'credentials.properties'
14+
PROJECTS_TO_PUBLISH = ["client", "server", "values", "testutil"]
15+
}
216

317
buildscript {
418
repositories {
519
mavenCentral()
620
mavenLocal()
21+
maven {
22+
url = 'http://maven.teamdev.com/repository/spine'
23+
}
724
}
825
dependencies {
9-
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.7'
26+
classpath group: 'com.google.protobuf', name:'protobuf-gradle-plugin', version: '0.7.7'
27+
classpath group: 'org.spine3.tools', name: 'protobuf-plugin', version: '1.4.8', changing: true
1028
}
29+
configurations.all({
30+
resolutionStrategy.cacheChangingModulesFor(0, 'seconds')
31+
})
1132
}
1233

1334
allprojects {
@@ -19,44 +40,32 @@ allprojects {
1940
version = '0.5.1-SNAPSHOT'
2041
}
2142

22-
project.ext {
23-
// NOTE: when updating Protobuf dependency, please check that KnownTypes.addStandardProtobufTypes() method
24-
// is updated with new Message types introduced in the new version of Protobuf.
25-
PROTOBUF_VERSION = '3.0.0-beta-3';
26-
PROTOBUF_DEPENDENCY = "com.google.protobuf:protoc:${project.PROTOBUF_VERSION}";
27-
MAVEN_REPOSITORY_URL = 'http://maven.teamdev.com/repository/spine';
28-
SPINE_PROTOBUF_PLUGIN_VERSION = "1.4.8"
29-
GRPC_VERSION = '0.14.0'
30-
SLF4J_VERSION = '1.7.21'
31-
}
43+
subprojects {
3244

33-
def repositoryUserName = null;
34-
def repositoryUserPassword = null;
35-
final Properties properties = new Properties();
36-
final File credentialsFile = file(CREDENTIALS_LOCATION);
37-
if (credentialsFile.exists()) {
38-
properties.load(credentialsFile.newDataInputStream());
39-
repositoryUserName = properties.getProperty("user.name");
40-
repositoryUserPassword = properties.getProperty("user.password");
41-
}
45+
project.ext {
46+
SRC_DIR = "$projectDir/src"
47+
GEN_DIR = "$projectDir/generated"
4248

43-
subprojects {
49+
GEN_MAIN_JAVA_DIR = "$GEN_DIR/main/java"
50+
GEN_TEST_JAVA_DIR = "$GEN_DIR/test/java"
4451

45-
buildscript {
46-
repositories {
47-
mavenLocal()
48-
maven {
49-
url = project.MAVEN_REPOSITORY_URL
50-
}
51-
mavenCentral()
52-
}
52+
GEN_MAIN_GRPC_DIR = "$GEN_DIR/main/grpc"
53+
GEN_TEST_GRPC_DIR = "$GEN_DIR/test/grpc"
54+
55+
GEN_MAIN_SPINE_DIR = "$GEN_DIR/main/spine"
56+
57+
TEST_ARTIFACTS_PLUGIN_PATH = '../gradle/plugins/test-artifacts.gradle'
58+
GEN_DESCRIPTOR_SET_PLUGIN_PATH = '../gradle/plugins/generate-descriptor-set.gradle'
59+
GRPC_PLUGIN_PATH = '../gradle/plugins/grpc.gradle'
60+
JACOCO_PLUGIN_PATH = '../gradle/plugins/jacoco.gradle'
5361
}
5462

55-
apply plugin: 'java';
56-
apply plugin: 'com.google.protobuf';
63+
apply plugin: 'java'
64+
apply plugin: 'com.google.protobuf'
65+
apply from: JACOCO_PLUGIN_PATH
5766

58-
sourceCompatibility = 1.7;
59-
targetCompatibility = 1.7;
67+
sourceCompatibility = 1.7
68+
targetCompatibility = 1.7
6069

6170
// Set Java home to point to JDK7 in gradle.properties file.
6271
//
@@ -68,7 +77,6 @@ subprojects {
6877
repositories {
6978
mavenLocal()
7079
mavenCentral()
71-
jcenter()
7280
}
7381

7482
dependencies {
@@ -88,24 +96,19 @@ subprojects {
8896
exclude(module: 'hamcrest-core')
8997
}
9098
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
91-
testCompile 'org.mockito:mockito-core:2.+'
99+
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.+'
92100
}
93101

94-
final String SRC_ROOT_DIR = "$projectDir/src";
95-
final String GEN_ROOT_DIR = "$projectDir/generated";
96-
final String GEN_MAIN_JAVA_DIR = "${GEN_ROOT_DIR}/main/java";
97-
final String GEN_TEST_JAVA_DIR = "${GEN_ROOT_DIR}/test/java";
98-
99102
sourceSets {
100103
main {
101-
proto.srcDirs = ["${SRC_ROOT_DIR}/main/proto"]
102-
java.srcDirs = [GEN_MAIN_JAVA_DIR, "${SRC_ROOT_DIR}/main/java"]
103-
resources.srcDirs = ["${GEN_ROOT_DIR}/main/resources"]
104+
proto.srcDirs = ["$SRC_DIR/main/proto"]
105+
java.srcDirs = [GEN_MAIN_JAVA_DIR, "$SRC_DIR/main/java"]
106+
resources.srcDirs = ["$GEN_DIR/main/resources"]
104107
}
105108
test {
106-
proto.srcDirs = ["${SRC_ROOT_DIR}/test/proto"]
107-
java.srcDirs = [GEN_TEST_JAVA_DIR, "${SRC_ROOT_DIR}/test/java"]
108-
resources.srcDirs = ["${GEN_ROOT_DIR}/test/resources"]
109+
proto.srcDirs = ["$SRC_DIR/test/proto"]
110+
java.srcDirs = [GEN_TEST_JAVA_DIR, "$SRC_DIR/test/java"]
111+
resources.srcDirs = ["$GEN_DIR/test/resources"]
109112
}
110113
}
111114

@@ -115,7 +118,7 @@ subprojects {
115118
artifact = "io.grpc:protoc-gen-grpc-java:$GRPC_VERSION"
116119
}
117120
}
118-
generatedFilesBaseDir = GEN_ROOT_DIR
121+
generatedFilesBaseDir = GEN_DIR
119122
protoc {
120123
artifact = project.PROTOBUF_DEPENDENCY
121124
}
@@ -131,15 +134,6 @@ subprojects {
131134
classifier "test"
132135
}
133136

134-
jacocoTestReport {
135-
reports {
136-
xml.enabled = true
137-
html.enabled = true
138-
}
139-
}
140-
141-
check.dependsOn jacocoTestReport
142-
143137
idea.module.iml {
144138
beforeMerged { final module ->
145139
module.dependencies.clear()
@@ -150,8 +144,8 @@ subprojects {
150144
}
151145

152146
idea.module {
153-
generatedSourceDirs += file(GEN_MAIN_JAVA_DIR);
154-
testSourceDirs += file(GEN_TEST_JAVA_DIR);
147+
generatedSourceDirs += file(GEN_MAIN_JAVA_DIR)
148+
testSourceDirs += file(GEN_TEST_JAVA_DIR)
155149
}
156150
}
157151

@@ -166,59 +160,4 @@ idea.project.ipr {
166160
}
167161
}
168162

169-
task readPublishingCredentials << {
170-
if (repositoryUserName == null || repositoryUserPassword == null) {
171-
throw new InvalidUserDataException("Please set up valid credentials. " +
172-
"Credentials should be set in ${CREDENTIALS_LOCATION} file in the project\'s root.");
173-
}
174-
println "Publishing build as ${repositoryUserName}";
175-
}
176-
177-
task publish << {}
178-
179-
void dependPublish(final Project project) {
180-
final Set<Task> credentialsTasks = getTasksByName("readPublishingCredentials", false);
181-
project.getTasksByName("publish", false).each { final task ->
182-
task.dependsOn credentialsTasks;
183-
}
184-
publish.dependsOn project.getTasksByName("publish", false);
185-
}
186-
187-
// Artifacts to publish
188-
final def publishingProjects = ["client", "server", "values", "testutil"];
189-
190-
publishingProjects.each {
191-
project(":$it") { final currentProject ->
192-
193-
apply plugin: 'maven-publish';
194-
195-
publishing {
196-
publications {
197-
mavenJava(MavenPublication) {
198-
groupId = "${group}";
199-
artifactId = "${currentProject.name}";
200-
version = "${currentProject.version}";
201-
202-
from components.java;
203-
204-
artifact sourceJar;
205-
artifact testOutputJar;
206-
}
207-
}
208-
}
209-
210-
publishing {
211-
repositories {
212-
maven {
213-
credentials {
214-
username = "${repositoryUserName}";
215-
password = "${repositoryUserPassword}";
216-
}
217-
url = project.MAVEN_REPOSITORY_URL;
218-
}
219-
}
220-
}
221-
222-
dependPublish(project);
223-
}
224-
}
163+
apply from: PUBLISH_PLUGIN_PATH

client/build.gradle

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,14 @@
1-
buildscript {
2-
configurations.all {
3-
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
4-
}
5-
dependencies {
6-
classpath group: 'org.spine3.tools', name: 'protobuf-plugin', version: "$SPINE_PROTOBUF_PLUGIN_VERSION", changing: true
7-
}
8-
}
91

102
dependencies {
11-
compile group: 'io.grpc', name: 'grpc-okhttp', version: "$GRPC_VERSION"
12-
compile group: 'io.grpc', name: 'grpc-protobuf', version: "$GRPC_VERSION"
13-
compile group: 'io.grpc', name: 'grpc-stub', version: "$GRPC_VERSION"
3+
compile group: 'io.grpc', name: 'grpc-okhttp', version: GRPC_VERSION
4+
compile group: 'io.grpc', name: 'grpc-protobuf', version: GRPC_VERSION
5+
compile group: 'io.grpc', name: 'grpc-stub', version: GRPC_VERSION
146

15-
testCompile project(path: ":testutil", configuration: 'testArtifacts')
7+
testCompile project(path: ":testutil")
168
}
179

18-
apply plugin: 'org.spine3.tools.protobuf-plugin';
10+
apply from: GRPC_PLUGIN_PATH
1911

20-
sourceSets {
21-
main {
22-
proto {
23-
srcDirs += ["${project(":client").projectDir}/src/main/proto"]
24-
}
25-
java {
26-
srcDir "$projectDir/generated/main/grpc"
27-
}
28-
}
29-
}
12+
apply from: TEST_ARTIFACTS_PLUGIN_PATH
3013

31-
protobuf {
32-
generateProtoTasks {
33-
all().each { final task ->
34-
task.plugins {
35-
grpc {}
36-
}
37-
}
38-
}
39-
}
40-
41-
// required for usage of test classes from other modules
42-
configurations {
43-
testArtifacts.extendsFrom testRuntime
44-
}
45-
46-
task testJar(type: Jar) {
47-
classifier "test"
48-
from sourceSets.test.output
49-
}
50-
51-
artifacts {
52-
testArtifacts testJar
53-
}
14+
apply plugin: SPINE_PROTOBUF_PLUGIN_ID

examples/build.gradle

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
1-
buildscript {
2-
configurations.all {
3-
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
4-
}
5-
dependencies {
6-
classpath group: 'org.spine3.tools', name: 'protobuf-plugin', version: "$SPINE_PROTOBUF_PLUGIN_VERSION", changing: true
7-
}
8-
}
91

102
dependencies {
11-
testCompile group: 'junit', name: 'junit', version: '4.12'
12-
testCompile project(path: ":client", configuration: 'testArtifacts')
13-
testCompile project(path: ":server", configuration: 'testArtifacts')
14-
15-
compile project(path: ':client');
16-
compile project(path: ':server');
3+
compile project(path: ':client')
4+
compile project(path: ':server')
175
}
186

19-
sourceSets {
20-
main {
21-
java {
22-
srcDir "$projectDir/generated/main/spine"
23-
}
24-
}
25-
}
7+
sourceSets.main.java.srcDir GEN_MAIN_SPINE_DIR
268

27-
protobuf {
28-
generateProtoTasks {
29-
all().each { final task ->
30-
task.plugins {
31-
task.generateDescriptorSet = true
32-
task.descriptorSetOptions.path = "${projectDir}/build/descriptors/${task.sourceSet.name}.desc"
33-
task.descriptorSetOptions.includeImports = true
34-
}
35-
}
36-
}
37-
}
9+
apply from: GEN_DESCRIPTOR_SET_PLUGIN_PATH
3810

39-
apply plugin: 'org.spine3.tools.protobuf-plugin';
11+
apply plugin: SPINE_PROTOBUF_PLUGIN_ID
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
// Apply this script if it is needed to generate Protobuf descriptor set (special files with `.desc` extension).
22+
// These files may be needed by Gradle plugins to obtain descriptors of `.proto` files of this project.
23+
protobuf {
24+
generateProtoTasks {
25+
all().each { final task ->
26+
task.plugins {
27+
task.generateDescriptorSet = true
28+
task.descriptorSetOptions.path = "$projectDir/build/descriptors/${task.sourceSet.name}.desc"
29+
task.descriptorSetOptions.includeImports = true
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)