Repository navigation
Conversation
|
Thanks very much for this. |
|
We have published a blog post on the topic of how libraries can profit from Gradle Module Metadata and used Guava as an example: https://blog.gradle.org/guava It basically covers what I have written in the PR description in better words. :) While working on the post, I also wrote a Gradle plugin that adds the additional metadata to the existing Guava versions. So whoever is interested in trying this out today can use the plugin in a Gradle build: https://plugins.gradle.org/plugin/de.jjohannes.missing-metadata-guava ( @cgdecker As I got back to this topic, I thought about how we could add test coverage to this PR. I added an "integration test" that resolves the locally installed Guava SNAPSHOT version with Gradle and tests all different variations of classpathes. It tests against Gradle 5 (using POM metadata) and Gradle 6 (using GMM). This way, we make sure that both metadata files are in sync and that we only have the expected differences in the resolution result. |
cpovirk
left a comment
There was a problem hiding this comment.
Thanks again for this, and sorry that we have let it sit so long. I have a couple small questions, but we should probably err on the side of merging this soon, and we can always make changes later.
| } | ||
| } | ||
| val guavaVersion = if (name.startsWith("jre")) { | ||
| "HEAD-jre-SNAPSHOT" |
There was a problem hiding this comment.
It sounds like we may need to update this manually (or tweak set_version to update it with sed or something) to test against a version that we're about to release? (And similarly at the top of the file.)
That should be fine. I just want to check my understanding (and invite you to let me know if there are other things we should do that I've overlooked).
There was a problem hiding this comment.
You are right. I missed this.
I fixed it by extracting the version directly from the pom.xml file.
| <variant.checkerframework>"checker-qual"</variant.checkerframework> | ||
| <otherVariant.version>HEAD-android-SNAPSHOT</otherVariant.version> | ||
| <otherVariant.jvmVersion>6</otherVariant.jvmVersion> | ||
| <otherVariant.checkerframework>"checker-compat-qual", "version": { "requires": "2.5.5" }</otherVariant.checkerframework> |
There was a problem hiding this comment.
Similarly: Do we need to specify the version number here? I see that it's omitted for checker-qual above.
There was a problem hiding this comment.
The reason why the version is required here is that the version of checker-compat-qual is not managed in the <dependencyManagement> block below. It's only defined directly in android/pom.xml.
I think the better solution is to add it to the block as well. I made that change: 70eaaf7.
Let me know if that is fine.
Note: The version is then defined in two places, here and in android/pom.xml. I am not sure why there are versions in android/pom.xml at all. The android pom.xml also uses this pom as parent. Maybe all the versions can be removed from android/pom.xml?
|
Thanks for taking the time to get back to this @cpovirk. I rebased this PR to latest From my perspective this can be merged as it is. The result of Gradle resolving a dependency to Guava is now pinned down in the integration test. That also tests the behaviour when resolving without Gradle Metadata. Which is a good addition to Guava's test coverage in general to make sure that changes to dependencies in pom files do not have unexpected side effects. What remains to be done is to (automatically) execute the integration test by calling |
cpovirk
left a comment
There was a problem hiding this comment.
Thank you again, this time for picking this up so quickly after our silence.
(Rewinding: I am going to ponder your earlier question about whether to move our annotation dependencies to compile-only scope. You can leave things as you have them, and I will tweak it if we want to be more conservative.)
What remains to be done is to (automatically) execute the integration test by calling
util/gradle_integration_tests.sh. I am not sure where to put this best. (Should it become part of the travis configuration?) This can be done as a follow up.
I would assume Travis as well. A follow-up sounds perfectly fine.
| <maven-source-plugin.version>3.2.0</maven-source-plugin.version> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <module.status>integration</module.status> | ||
| <module.parentVersion>HEAD-jre-SNAPSHOT</module.parentVersion> |
There was a problem hiding this comment.
In your most recent round of comments, you said:
I am not sure why there are versions in
android/pom.xmlat all. The androidpom.xmlalso uses this pom as parent. Maybe all the versions can be removed fromandroid/pom.xml?
I don't think that's the case: I see no <parent> element in android/pom.xml.
There is certainly a decent argument that we should have a common parent for both flavors; that's just not how we have it set up at present.
Regardless, I like the idea of having the version of checker-compat-qual defined in both flavors' pom.xml files, since that lets the Gradle files refer to it consistently.
But: Given that the Android flavor has its own parent POM, should the line here refer to HEAD-android-SNAPSHOT?
There was a problem hiding this comment.
I don't think that's the case: I see no element in android/pom.xml.
Uh, my bad. Yes the POM does not have the parent. And while it could be possible it does make sense that there is no common parent as each flavor is a separate maven build.
I don't know why I did this, but this parentVersion property is actually not needed at all and the version of the parent simply has to match the version of the variant. Then the checker-compat-qual version is also there.
So forget most of what I said earlier.
I fixed this. Thanks for clarifying and asking the right questions.
There was a problem hiding this comment.
Ah, thanks, I was still not seeing the implications of this :) All the more reason for the Gradle metadata to simplify things for users!
|
I went ahead and added the integration test as a step to travis to make sure it is working before we merge. Looks good as far a I can tell. At least it did run on this PR and passed. Please feel free to merge. And do further changes later if required (I am happy to help, just ping me). As a note regarding
You are most likely aware: this is this long-standing feature request in Maven that was never picked up. |
|
Sorry, I have one more question, which I should have asked long ago. You said in your original message that Gradle will automatically choose But... do Android users do this? It sounds like Would we need to do something different in order to detect Android compiles (presumably by detecting something set by the Android Gradle Plugin)? [edit: I'm not clear on whether the following links are asking about something similar: https://discuss.gradle.org/t/how-to-detect-if-a-plugin-was-not-applied/28995, https://stackoverflow.com/q/46204642/28465] |
|
(Maybe Android Gradle plugin should declare an attribute? I could try talking to that team if you think it would be helpful. Historically, we haven't been in touch with them a lot, but I'd like to think they'd be interested in this.) |
|
@cpovirk thanks for doing all the cleanup on this PR. Looks like I missed the formatting rules. Very good concern about Android. I did not think of this. We definitely have to address that. So there are basically two conditions to select the
Android already sets additional attributes and we probably make use of that. I have some knowledge there. I didn't get around to look into this today, but will do on Monday. I can also get feedback from the Android Gradle plugin team. We are in touch. |
|
Android does not care about the
Unless I missed something, we'll have to use While thinking about this, I also realised that we have to make sure that users can still make a different choice if, for some reason, they do not want to follow the recommended way:
Such a selection is possible by setting attributes directly on the dependency (or a dependency constraint) to Guava. Which will either select the desired variant or, if there are other (transitive) dependencies to Guava, lead to a situation where both variants are selected. The latter case leads to a resolution error which you can resolve by adding a capability resolution. I added test cases for this to make sure it is actually working. The hope is that most users won't have to deal with this. But it needs to be possible to somehow "opt-out" of the default selection. I pushed the proposed change and extended the integration test to also run against Android projects. I'll ask some colleagues and the Android Gradle plugin team about their thoughts on this. |
cpovirk
left a comment
There was a problem hiding this comment.
(RE: style guide: I'm not sure that our internal style guide, which requires 2-space indents, is actually available publicly -- just the Android one that requires 4-space indents :))
| } | ||
|
|
||
| if (name.contains("Java6Constraint")) { | ||
| dependencies { |
There was a problem hiding this comment.
It looks like maybe this first block (dependencies - constraints) is the main block users need to override the version of Guava chosen by default?
As for the configurations.all block below: Is that the part you mentioned that can override the version of Guava chosen for a transitive dependency? It looks like it's necessary for the current tests to pass. Granted, I think that's because of the dependencies on guava and its "alternatives" (like google-collections) rather than any "normal" transitive dependencies on it. (Still, I'm not sure if it's worth going to any trouble to test further than that.)
(And is the second block alone always technically sufficient? I would of course advise using only the first block if possible. But if someone has to use the second, does the first still serve a purpose? I experimented a little, but I think I'm getting confused because of the "alternatives" again.)
Finally, thank you: An override sounds like an important thing to have.
There was a problem hiding this comment.
To clarify:
- If you do not mention Guava at all in your build script, and just get it transitively, there is nothing you need to do as long as you are fine with the default varian selection.
- If you declare a dependency to Guava directly, there is nothing you need to do as long as you are fine with the default varian selection.
- If you want to select a different variant than the default, you can dot that, differently to what is shown in the test, directly in the dependency. E.g.:
dependencies {
api("com.google.guava:guava:30.0+") {
attributes { attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 6) }
}
}
This might just work or if there is another transitive dependency, which does not explicitly request jvm=6, you can get a conflict because Gradle selects different variants for the different dependencies. Then you need the resolutionStrategy.capabilitiesResolution rule to solve the conflict. In a normal build script that looks less complicated. It's only that I needed to use reflection in this test so that it can also compile with Gradle 5.
4. You can also create the situation above just in your local build script by having multiple dependencies or a dependency + a dependency constraint, on Guava in your build script where not all of them request the same attributes. (That's the case in this test.)
With the Gradle Metadata, we'll make sure that (1) and (2) are done correctly. At the moment the default is often wrong when conflicts occur (e.g. right now 29-jr and 30-android will conflict resolve to 30-android for a Java project). When the default works correct, the hope is that an override is not needed very often. While right now many users have to do it, because the default behaviour resolves to the wrong version/variant.
There was a problem hiding this comment.
Now I'm thinking about what to put in the release notes. It sounds like we can mostly just say that things will get better automatically :) But in the interest of helping users who do encounter any problems:
If users have a transitive dependency on google-collections or listenablefuture in addition to some dependency on Guava proper, then they'll need to something along the lines of these instructions, which is similar to what you demonstrate above but without the need for reflection.
Do I have that right? I think I'm repeating what you said back to you, but obviously it's been forever. Sorry again.
(That behavior is an improvement over having google-collections also on the classpath. It's a more mixed bag for listenablefuture: Instead of having the conflict with that automatically resolved in a hacky way (which IIRC upset Android Studio builds until they put in a hack of their own for us), users have to do it explicitly.)
|
@cpovirk I talked to the Android Gradle Plugin team team and we think the I would suggest we hold off merging until that discussion resolved. I'll get back to you once this moved forward, in a few days. |
|
@cpovirk sorry it took us a bit. We have discussed this topic at Gradle and with the Android Gradle Plugin team. We agreed that there is a general use case interesting for many library authors and that we need a new JVM ecosystem specific standard attribute that Gradle and AGP recognise. Here is a proposal for that. And a spike implementation demonstrating how it would work. As soon as there is agreement, this will be added and I'll update this PR once we have a nightly build that supports it. |
|
Sorry for my silence on this. I still intend to get back to it, but it may take a while. |
To be conservative, this commit does not removes most annotation artifacts from Gradle's runtime classpath, only j2objc-annotations. The other artifacts contain at least some annotations with RUNTIME visibility (IIRC). (Even this change could theoretically affect users who assume that they can read CLASS-retention annotations (of which j2objc-annotations has some) from bytecode and find them in the runtime classpath. But that seems unlikely, especially for j2objc annotations.) We may consider being more aggressive in the future. For now, this particular commit addresses #2824 for Gradle users just a tiny bit, and it helps with the problem that prompted #6567. Fixes #3683 RELNOTES=Added [Gradle Module Metadata](https://docs.gradle.org/current/userguide/publishing_gradle_module_metadata.html). If you use Gradle 6 or higher, Gradle can automatically intelligently resolve conflicts between `guava-android` and `guava-jre`, among [other benefits](#3683). PiperOrigin-RevId: 337348979
Small follow up to #3683 See: #6604 (comment) Fixes #6605 RELNOTES=n/a PiperOrigin-RevId: 544337005
Small follow up to #3683 See: #6604 (comment) Fixes #6605 RELNOTES=n/a PiperOrigin-RevId: 544384609
Hello again from Gradle 👋
The following is a suggestion open for discussion. I provide this as a PR, instead of an issue only, as a proof of concept to aid the discussion.
Background
With the release of Gradle 6, Gradle fully supports a new metadata format called Gradle Module Metadata (GMM) - docs and spec. Such a metadata (
.module) file can be published in addition to a.pomfile and Gradle 6 will use it to obtain additional information about a library, its artifacts and transitive dependencies which can not be represented in the pom format. The GMM format has been under development at Gradle since about two years. With the release of Gradle 6, we are now at a point where it is ready to be adopted in larger scale. That's why we have been looking at popular JVM libraries, like Guava, to evaluate if Gradle users depending on a library would profit if the library would publish GMM.Why should Guava care?
There are a couple of things Guava struggles with to express in their poms. Many of which have already been discussed at length here in other issues. I set up a repository with samples demonstrating these use cases. In detail, GMM offers modeling concepts, and with that solutions, for the following:
Multiple variants in one component
Use case with description: guava-jdk-variants
At the core of Gradle 6 dependency management and GMM is the notion that a component can have multiple, and arbitrary many, variants. A concept Gradle has been adopting since 3.x internally and which is heavily used already by all Gradle JVM plugins to distinguish between runtime and compile time variants and to a larger extend, for example, by the Android build system or by Kotlin Native.
Guava publishes two prominent variants -
androidandjre. It is solved in probably the most elegant way possible with POM by encoding the variant name in the version number. Still this often causes issues as built tools do not know that the versions also present variants.Each variant in GMM is identified by attributes. These attributes are used by Gradle to select the best fitting variant. Gradle defines a number of default attributes for JVM libraries. One of them is
org.gradle.jvm.version. In the presented solution, we set the attribute to6for theandroidvariants and to8for thejrevariants. Gradle, knowing about the attribute, can now use that information. If you do in your build:Gradle will automatically select
android, if you set it to version 8 or higher, Gradle will selectjre. If you set it to a lower version, Gradle will fail.It does not matter if you depend on Guava directly or if it is brought in transitively. The central idea behind the variant-aware mechanism is that dependencies are defined between modules, but the variant selection is done when the dependencies are resolved based on the current context. For example, Gradle can say "give me everything I need to compile this library for Java 6" or "give me everything I need to run this application with Java 9".
Note: The presented solution keeps the way how Guava is publishing two "versions" at once. Only for Gradle now both versions are identical (both provide all variants). So in Gradle it would not matter if
com.google.guava:guava:29.0-jreorcom.google.guava:guava:29.0-androidis in your dependency graph, as both provide the same variants. For Maven users, nothing changes as the poms are no different.Compile-only API dependencies / annotation processor libraries on runtime classpath
Use case with description: guava-compile-only-dependencies
This addresses what is being discussed in #2824. With the solution presented here, you would get the following classpathes:
It can be discussed if that is what is desired, but the essential bit is that you can have things on the compile classpath but not on the runtime classpath (not possible with poms currently).
A component/variant can provide multiple capabilities
Use case 1: guava-listenablefuture-conflict
Use case 2: guava-vs-google-collections
Another concept introduced with variants is capabilities. A capability is essentially an identifier for a "feature" that is implemented by a component. This allows you to express that:
To be more concrete, each component has a default capability corresponding to its GAV coordinates:
com.google.guava:guavahas thecom.google.guava:guavacapabilitycom.google.collections:google-collectionshas thecom.google.collections:google-collectionscapabilitycom.google.guava:listenablefuturehas thecom.google.guava:listenablefuturecapabilityNow we can say that
guavaalso has thecom.google.collections:google-collectionsandcom.google.guava:listenablefuturecapabilities. Gradle will then not allow Guava together with these modules on the classpath and offers simple options to resolve the conflict. The9999.0-empty-to-avoid-conflict-with-guavaworkaround is then no longer needed to avoid undesired old versions ofcom.google.guava:listenablefutureon the classpath.Note: since the pom is not changed, the workaround will stay in place for Maven.
How does publishing GMM work with Maven builds?
Usually, we would recommend using Gradle 6 as your build system if your library wants to publish individual variants or other GMM specifics, as it is not easy to manually set this up in a Maven build. However, the Guava build is already setup to do it - by combining two Maven builds. So everything is already in place, the only thing is that certain metadata information cannot be expressed/published in the poms.
As you can see in this PR, I added the
.modulefile to the repo. (In Gradle, these files are generated during publishing as are the poms.) Since it mostly contains the additional information and Guava does not have many dependencies (and also does not want to add more), the file is of reasonable size to be maintained manually along the poms. Since the naming of themodulefile is following Maven conventions, it can be treated and published as an additional artifact by Maven - as you can see in this PR.If you consider adopting this solution, I am happy to provide some kind of integration test (maybe based on the samples linked above) that validates that the
modulefile is valid and that the information does not contradict the information in the pom files.Further details
Sorry for this rather long text here. My goal is to illustrating the use cases and provide enough detail for a basic understanding of why we think many Gradle users would profit from having Guava publish Gradle Module Metadata. If you are interested in more details, here are a few links to sections in the Gradle user manual:
We also recently did a Webinar on publishing that covers GMM:
https://gradle.com/blog/dependency-management-with-gradle-part-3-publishing-and-release-strategies/ (you can directly jump to 'Section 5 - Metadata' in the video)
And others have started to publish
modulefiles to central, so that is working :)https://repo1.maven.org/maven2/org/junit/jupiter/junit-jupiter-api/5.6.0-M1/junit-jupiter-api-5.6.0-M1.module
I am happy to answer any questions on the topic and help working out a solution if there is interest in this on your side.