Introduction
Whilst building a demo application showcasing google AutoService, I encountered the below error:
|
|
Searching through the internet, most solutions recommended upgrading the Google Guava version but that did not resolve the problem.
The main cause of this error, was putting the wrong Autoservice
dependency in the classpath.
If your POM file has the below dependency, then you are most likely get this error.
|
|
In this guide, you will solve the compilation error when using AutoService on a maven
project.
When you’re finished, you’ll be able to build jar files with the right
META-INF/services
information.
Prerequisites
Before you begin this guide:
- Read up the difference between a maven dependency and plugin
- This Stackoverflow Post answer this question
- Read Sonatype’s book Maven: A Complete Reference
Step 1 — Check what is in your dependency tree
We need to establish what we have in our dependency tree, because maven might be pulling in a version of the jar as a transitive dependency which might not work with your project
Run the following command to see the jar file in your tree:
|
|
if the below are missing then follow step 2;
com.google.auto.service:auto-service:jar:<version>:compile
com.google.auto.service:auto-service-annotations:jar:<version>:compile
Step 2 — Add AutoService Annotation Dependency to POM file
Add the following compile-time dependency to your classpath so the project can compile and pull in the relevant classes.
|
|
Run the below to ensure your project compiles:
|
|
Now run the package command to generate the jar file in your target directory
|
|
Spy the content of the Jar file to view the generated AutoService metadata
|
|
You should not get any results.
Developers sometimes, forget to include the right metadata instructions when creating the jar file and maven will not raise any error.
This is the exact problem Autoservice
solves for us, removing the need to tell maven of all your metadata rule.
The next step will walk you through the process of adding the processor.
Step 3 — Tell Maven to generate the Metadata
Add the following configuration to the compiler plugin
|
|
Now run the package again to generate the jar file in your target directory
|
|
Spy the content of the Jar file again, to view the generated metadata
|
|
Assuming our processor class is org.ihq.MyAutoclass
, this time the output should resemble below
|
|
Conclusion
You’ve set up Google AutoService
with Maven
to autogenerate metadata without compile error(s).