diff --git a/library-best-practices/JLBP-20.md b/library-best-practices/JLBP-20.md new file mode 100644 index 0000000000..38d9877ece --- /dev/null +++ b/library-best-practices/JLBP-20.md @@ -0,0 +1,55 @@ +[JLBP-20] Give each jar a module name +-------------------------------------------------- + +For compatibility with the Java Platform Module System (JPMS) in Java 9 and +later, every JAR you publish should have a module name, even if the library +does not itself use modules. More precisely, the JAR manifest in +META-INF/MANIFEST.MF should have an Automatic-Module-Name field such as +this one for `com.google.http-client:google-http-client`: + +``` +Automatic-Module-Name: com.google.api.client +``` + +The module name should be globally unique and composed of +dot-separated Java identifiers. It should usually be a reversed domain name such +as commonly found in Java package names. It should have the same name as the root +package of the JAR. For example, if a JAR contains `com.google.utilities.i18n` +and `com.google.utilities.strings` then `com.google.utilities` is a good +choice for module name. However if there's a second artifact that contains +`com.google.utilities.math` and `com.google.utilities.stats`, you can't choose +the name `com.google.utilities` for both modules. + +Automatic-Module_name is similar to the OSGI Bundle-SymbolicName and should +probably have the same value as that field. + +To add an Automatic-Module-Name field to a jar using Maven, configure the +Maven jar plugin in pom.xml like so: + +```xml + + maven-jar-plugin + + + + com.google.api.client + + + + +``` + +To add an Automatic-Module-Name field using Gradle, add the following to +build.gradle: + +``` +ext.moduleName = "com.google.api.client" + +jar { + inputs.property("moduleName", moduleName) + + manifest { + attributes 'Automatic-Module-Name': moduleName + } +} +``` diff --git a/library-best-practices/README.md b/library-best-practices/README.md index 5375e37115..b8de8f4c2e 100644 --- a/library-best-practices/README.md +++ b/library-best-practices/README.md @@ -38,3 +38,4 @@ or significantly changed before the set is classified as Beta. - [JLBP-17](JLBP-17.md): Coordinate rollout of breaking changes - [JLBP-18](JLBP-18.md): Only shade dependencies as a last resort - [JLBP-19](JLBP-19.md): Place each package in only one module +- [JLBP-20](JLBP-20.md): Give each jar a module name