Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions library-best-practices/JLBP-20.md
Original file line number Diff line number Diff line change
@@ -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

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.

Might want to link to Stephen Colebourne's discussion of module naming best practices, and even recommend more strongly that we follow them (Replace "often has the same name" with "should have the same name", etc.).

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.

changed to "should" as suggested

@garrettjonesgoogle What do you think about linking to othe non-normative references?

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.

@elharo I don't have a fundamental problem with it.

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
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>com.google.api.client</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
```

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
}
}
```
1 change: 1 addition & 0 deletions library-best-practices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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