Skip to content

Add initial version of Gradle build files#403

Merged
AndrewMBurke merged 10 commits intomasterfrom
add_gradle_build
Mar 18, 2021
Merged

Add initial version of Gradle build files#403
AndrewMBurke merged 10 commits intomasterfrom
add_gradle_build

Conversation

@AndrewMBurke
Copy link
Copy Markdown
Contributor

This is part of the effort to address long build times for the Java library, such as is raised in #346.

@AndrewMBurke AndrewMBurke added the enhancement New feature or request label Feb 23, 2021
@AndrewMBurke AndrewMBurke self-assigned this Feb 23, 2021
Comment thread google-ads-examples/build.gradle Outdated

description = 'Google Ads API client library for Java examples'

public class ExampleRunnerTask extends JavaExec {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this class only needed to allow the user to specify without the com.google.... prefix? If so is there perhaps an easier way to do this? The gradle build files are so pretty, it's a shame to put java boilerplate in them ;-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed the best way to address the fact that this project has multiple "Main" classes.

The better, more Gradle-y way would be to have a single main class and to build the subproject as a Java application. Then you could just call ./gradlew run --args.... This would, of course, involve some refactoring of the project.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to the buildSrc directory to simplify the build file here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing so would then apply the task to all subprojects, i.e. calling ./gradlew runExample ... would run this code for the annotation processor, library, examples, and migration examples subprojects. You could instead call ./gradlew google-ads-examples:runExample ..., but that seems like an extra inconvenience when the task only applies to the one subproject.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See related response to Josh's question below.

Comment thread buildSrc/build.gradle
Comment thread gradle.properties
Comment thread google-ads-examples/build.gradle Outdated

description = 'Google Ads API client library for Java examples'

public class ExampleRunnerTask extends JavaExec {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to have this for the migration examples project as well, without duplication?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I didn't explicitly test those. I'll adjust and test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The migration examples should of course still be runnable from IntelliJ.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will test moving the task code into the top-level build files, then filtering for which subproject to apply them to.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restructured; task code is now in buildSrc, and there are now two executable tasks: runExample, as before, and runMigrationExample.

dependencies {
implementation 'com.squareup:javapoet:1.11.1'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'com.google.api:gax:1.50.1'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does gax need to be here, given it's in java-conventions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually added this in only to match the dependencies from the Maven POM files:

. This one is 1.50.1, while the rest are 1.60.1.

This line can be removed if this subproject doesn't need to be pegged to 1.50.1.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The effective version here for users will be the one from the main project (since it's shallower in the dependency hierarchy). We should just remove this (so we're on 1.60.1).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread buildSrc/build.gradle
Comment thread buildSrc/build.gradle
dependencies {
implementation 'com.squareup:javapoet:1.11.1'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'com.google.api:gax:1.50.1'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The effective version here for users will be the one from the main project (since it's shallower in the dependency hierarchy). We should just remove this (so we're on 1.60.1).

Comment thread google-ads-examples/build.gradle Outdated
@TaskAction
@Override
public void exec() {
if (!(exampleArguments?.trim())) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We (sadly) need to support Java 8 which doesn't have the ? operator. Can you convert this to an explicit null check (sorry!)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually Groovy code, not Java. It compiles fine in Ubuntu with Java 8 as the only available JDK.

Copy link
Copy Markdown
Contributor

@nwbirnie nwbirnie Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you specify javac -source 8 -target 8 does this still compile?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a flag to target Java 1.8 compatibility of the output, just to be safe.

Not sure why would we use javac here?

Comment thread google-ads-examples/build.gradle Outdated

description = 'Google Ads API client library for Java examples'

public class ExampleRunnerTask extends JavaExec {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to the buildSrc directory to simplify the build file here?

Comment thread google-ads-examples/build.gradle Outdated
main = basePackage + this.exampleArguments
}
// Otherwise, separate the input and set the arguments to pass to the main class.
else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run google-java-format on the java code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually Groovy code; I'll apply Java style to it though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformatted, please check.

Comment thread gradle.properties
Copy link
Copy Markdown
Contributor

@nwbirnie nwbirnie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of nits, otherwise LGTM. Nice job!

task runMigrationExample(type: ExampleRunnerTask) {
classpath = sourceSets.main.runtimeClasspath
basePackage = 'com.google.ads.googleads.migration.'
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra whitespace

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread google-ads/build.gradle Outdated
testImplementation 'io.grpc:grpc-context:1.30.0'
testImplementation 'com.google.truth:truth:0.27'
testImplementation 'com.google.auto.value:auto-value-annotations:1.7.3'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra whitespace

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread buildSrc/build.gradle
@@ -0,0 +1,26 @@
/*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more: please add build directories and .gradle to .gitignore.

image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@nwbirnie
Copy link
Copy Markdown
Contributor

Friendly ping for @jradcliff :)

Copy link
Copy Markdown
Member

@jradcliff jradcliff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, on my Linux box:

  • a build from scratch took just 2m59s
  • after that (with the daemon running), clean build took 2s to 12s

That's quite an impressive improvement.

@AndrewMBurke AndrewMBurke merged commit 71816b9 into master Mar 18, 2021
@AndrewMBurke AndrewMBurke deleted the add_gradle_build branch March 18, 2021 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants