Skip to content

Commit 0c3c22d

Browse files
authored
Make shadowJar less onerous (#475)
* Make shadowJar less onerous A couple of improvements to build velocity here: - use the output files of the generate licenses task. This means that shadowJar can use the cache (can remove upToDateWhen=false). - SmokeTest is only blocking on publish to sonatype. - SmokeTest times out after 20 seconds (e.g. in case of bad OAuth credentials). - Avoid failing the build if signing isn't configured - check for errors in SmokeTest (this was a bug).
1 parent 6b594cc commit 0c3c22d

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

google-ads/build.gradle

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,10 @@ task generateLicenses(type: nl.javadude.gradle.plugins.license.DownloadLicenses)
114114
description = "Copies the license report into the third_party directory"
115115

116116
includeProjectDependencies = true
117+
117118
// Works around issue where the plugin looks at the deprecated 'compile'
118119
// configuration by default.
119120
dependencyConfiguration = 'runtimeClasspath'
120-
121-
// Defines variables for the expected output files.
122-
def outputDirectory = tasks.downloadLicenses.outputs.files.files.find()
123-
def outputFilePattern = outputDirectory.absoluteFile.path + File.separator + "dependency-license."
124-
def licenses = ["html", "json", "xml"].collect({ file(outputFilePattern + it) })
125-
126-
// Exports the output file names for use elsewhere.
127-
ext {
128-
licenseFiles = licenses
129-
}
130121
}
131122

132123
task verifyLicenses() {
@@ -135,7 +126,7 @@ task verifyLicenses() {
135126

136127
doLast {
137128
// Reads the dependency-license.json file generated by generateLicenses.
138-
def inputFile = tasks.generateLicenses.licenseFiles.find { it.path.endsWith(".json") }
129+
def inputFile = tasks.generateLicenses.outputs.files.files.find { it.path.endsWith(".json") }
139130
def json = new JsonSlurper().parseText(inputFile.text)
140131
// Gets all the licenses except those for ourselves (which aren't set yet).
141132
def licenses = json
@@ -183,7 +174,10 @@ task copyLicenses(type: Copy) {
183174
dependsOn 'generateLicenses'
184175
mustRunAfter 'createThirdPartyDirectory'
185176

186-
from tasks.generateLicenses.licenseFiles
177+
def inputFiles = tasks.generateLicenses.outputs.files.asFileTree.files
178+
.findAll { it.path.contains "dependency-license"}
179+
180+
from inputFiles
187181
into tasks.generateThirdPartyDirectory.thirdPartyDir
188182
// Rename files to LICENSES.*
189183
eachFile {
@@ -197,10 +191,10 @@ task copyLicenses(type: Copy) {
197191
// Fails the build if we didn't copy everything. Gradle will fail silently if
198192
// it cannot copy one of the inputs which is dangerous when you're dealing
199193
// with legal obligations.
200-
if (tasks.generateLicenses.licenseFiles.size() != inputs.sourceFiles.size()) {
194+
if (inputFiles.size() != inputs.sourceFiles.size()) {
201195
throw new GradleException(
202196
"Failed to locate files to copy from. Expected: "
203-
+ tasks.generateLicenses.licenseFiles
197+
+ inputFiles
204198
+ " but was: " + inputs.sourceFiles.toList())
205199
}
206200
}
@@ -275,15 +269,12 @@ shadowJar {
275269
// Removes the default -all classifier.
276270
archiveClassifier = ''
277271

278-
// Requires that this task is always run and does not build from the cache.
279-
outputs.upToDateWhen { false }
280-
281272
// Updates the META-INF/services directory with the services from dependencies
282273
mergeServiceFiles()
283274

284275
// Includes the license report with the shadow jar.
285276
dependsOn 'generateLicenses'
286-
from tasks.generateLicenses.licenseFiles
277+
from tasks.generateLicenses.outputs
287278

288279
finalizedBy 'testShadowJar'
289280
}
@@ -296,17 +287,25 @@ task testShadowJar {
296287

297288
doLast {
298289
javaexec {
299-
mainClass = "com.google.ads.googleads.lib.SmokeTest"
290+
// Runs the JUnit main method.
291+
mainClass = "org.junit.runner.JUnitCore"
292+
293+
// Passes the class name of the test to run.
294+
args "com.google.ads.googleads.lib.SmokeTest"
295+
300296
// Uses a special configuration to run the smoke tests. Allows us to
301297
// replace the google-ads dependency with the shaded artifact so we can
302298
// test it. Otherwise we'd just run end up running the smoke tests on
303299
// the normal jar twice.
304300
classpath = project.configurations.shadeTest +
305-
// Uses the compiler output directly, rather than getting the
306-
// dependencies from the configuration (since this would include
307-
// the artifact we're trying to ignore).
308-
files(sourceSets.functionalTest.output.classesDirs.files,
309-
shadowJar.outputs.files)
301+
// Uses the compiler output directly, rather than getting the
302+
// dependencies from the configuration (since this would include
303+
// the artifact we're trying to ignore).
304+
files(sourceSets.functionalTest.output.classesDirs.files,
305+
shadowJar.outputs.files)
306+
307+
// Allows the smoke tests to pass unless we're publishing to sonatype.
308+
ignoreExitValue = !gradle.taskGraph.hasTask("publishMavenPublicationToSonatypeRepository")
310309
}
311310
}
312311
}
@@ -397,6 +396,16 @@ signing {
397396
sign publishing.publications.shadow
398397
}
399398

399+
// Disables signing tasks except on sonatype deploy. Avoids failing build for
400+
// users without GPG configured.
401+
gradle.taskGraph.whenReady {
402+
if (gradle.taskGraph.allTasks
403+
.findAll { it.name.matches("publish.+ToSonatypeRepository") }
404+
.isEmpty()) {
405+
tasks.findByName("signShadowPublication").enabled = false
406+
}
407+
}
408+
400409
// Provide a helpful error message for missing sonatype credentials.
401410
tasks.findAll {
402411
if (it.name.matches("publish.+ToSonatypeRepository")) {

google-ads/src/test/functional/java/com/google/ads/googleads/lib/SmokeTest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import com.google.api.gax.grpc.GrpcCallContext;
2727
import java.io.IOException;
2828
import java.util.List;
29+
import java.util.concurrent.TimeUnit;
2930
import java.util.stream.Collectors;
3031
import java.util.stream.StreamSupport;
32+
import org.junit.Rule;
3133
import org.junit.Test;
32-
import org.junit.runner.JUnitCore;
34+
import org.junit.rules.Timeout;
3335
import org.junit.runner.RunWith;
3436
import org.junit.runners.JUnit4;
3537
import org.threeten.bp.Duration;
@@ -44,15 +46,7 @@
4446
@RunWith(JUnit4.class)
4547
public class SmokeTest {
4648

47-
/**
48-
* Provides a main method to run these tests. Useful for testing artifacts such as shadow jar
49-
* which require modifying the runtime classpath.
50-
*/
51-
public static void main(String[] args) {
52-
System.out.println("Running SmokeTest");
53-
JUnitCore.runClasses(SmokeTest.class);
54-
System.out.println("Successfully completed SmokeTest");
55-
}
49+
@Rule public Timeout timeout = new Timeout(20_000, TimeUnit.MILLISECONDS);
5650

5751
@Test
5852
public void ensureCanReadAllAvailableCustomersCampaigns() throws IOException {

0 commit comments

Comments
 (0)