Home >Java >javaTutorial >JeKa: The Simplest Way to Publish on Maven Central
JeKa is a modern Java build tool focused on simplicity.
This post demonstrates how to publish to Maven Central with minimal configuration.
Prerequisite: You need an OSSRH account to publish on Maven Central.
With JeKa, you can fully configure the build by editing the jeka.properties file as follows:
jeka.version=0.11.11 jeka.java.version=17 jeka.inject.classpath=dev.jeka:nexus-plugin @nexus= @project.moduleId=com.github.djeang:vincer-dom @project.gitVersioning.enable=true # Configuration for deploying to Maven central @maven.publication.predefinedRepo=OSSRH @maven.publication.metadata.projectName=Vincer-Dom @maven.publication.metadata.projectDescription=Modern Dom manipulation library for Java @maven.publication.metadata.projectUrl=https://github.com/djeang/vincer-dom @maven.publication.metadata.projectScmUrl=https://github.com/djeang/vincer-dom.git @maven.publication.metadata.licenses=Apache License V2.0:https://www.apache.org/licenses/LICENSE-2.0.html @maven.publication.metadata.developers=djeang:djeangdev@yahoo.fr
Note that dependencies are listed in a dedicated dependencies.txt file to maintain clear separation of concerns.
To publish to Maven Central, execute: jeka project:pack maven:publish.
See a concrete example here.
Now that you know how to do it, let's explain how it works.
For better portability and reproducibility, we can declare both the Jeka and Java versions required for building. Both versions will be automatically downloaded if not already present on the host machine.
The published moduleId is specified using the @project.moduleId property.
The version can be explicitly specified using the @project.version property. Note that properties can be set in the jeka.properties file or passed as a command-line argument: -D@project.version=1.0.1.
Instead, we choose to rely on Git to infer the version using: @project.gitVersioning.enable=true. If there is no tag on the current commit, the version will be set to [branch]-SNAPSHOT; otherwise, it will be the tag-name.
@maven.publication.predefinedRepo=OSSRH instructs Jeka to publish to the pre-defined OSSRH repository. This repository is configured to publish to the OSSRH snapshot repository when the version ends with -SNAPSHOT, and to the release repository otherwise.
The repository uses the following environment variables to pass secrets:
The content of jeka.gpg.secret-key can be obtained by executing: gpg --export-secret-key --armor my-key-name.
The mandatory metadata are set using @maven.publication.metadata.xxx properties.
Note that the @maven.publication.metadata.licenses property expects a format like: [license1 name]:[license1 url],[license2 name]:[license2 url],...
For convenience, we use the Nexus plugin,
which automatically closes the staging repository without requiring manual intervention.
jeka.inject.classpath=dev.jeka:nexus-plugin instructs Jeka to fetch the plugin from Maven Central, while @nexus= activates it.
To publish, simply execute: jeka project:pack maven:publish.
This will:
To see what will be published, execute: jeka maven: info.
Fine-tuning in Jeka is generally achieved programmatically, complementing the declarative configuration from the jeka.properties file. This approach allows for highly flexible and powerful configurations with the benefits of static typing.
We can customize the dependencies mentioned in the published POM.
In the following example, we remove the com.google.guava:guava dependency and force the jfiglet dependency to have the RUNTIME scope.
jeka.version=0.11.11 jeka.java.version=17 jeka.inject.classpath=dev.jeka:nexus-plugin @nexus= @project.moduleId=com.github.djeang:vincer-dom @project.gitVersioning.enable=true # Configuration for deploying to Maven central @maven.publication.predefinedRepo=OSSRH @maven.publication.metadata.projectName=Vincer-Dom @maven.publication.metadata.projectDescription=Modern Dom manipulation library for Java @maven.publication.metadata.projectUrl=https://github.com/djeang/vincer-dom @maven.publication.metadata.projectScmUrl=https://github.com/djeang/vincer-dom.git @maven.publication.metadata.licenses=Apache License V2.0:https://www.apache.org/licenses/LICENSE-2.0.html @maven.publication.metadata.developers=djeang:djeangdev@yahoo.fr
The API enables defining additional artifacts to publish.
In the following example, two artifacts are generated at publication time:
class Build extends KBean { @Override void init() { var publication = load(MavenKBean.class).getMavenPublication(); publication.customizeDependencies(deps -> deps .minus("com.google.guava:guava") .withTransitivity("com.github.lalyos:jfiglet", JkTransitivity.RUNTIME) ); } }
To publish to a repository other than Maven Central, you can set the following properties:
class Build extends KBean { @Override void init() { var publication = load(MavenKBean.class).getMavenPublication(); publication.putArtifact(JkArtifactId.of("doc", "zip"), this::genDoc); publication.putArtifact(JkArtifactId.of("shade", "jar"), project.packaging::createShadeJar); } private void genDoc(Path targetZipFile) { // generate documentation and zip it in targetZipFile } }
Place these properties in [USER HOME]/.jeka/global.properties (instead of jeka.properties file) to keep configurations consistent across projects and avoid redundancy.
For more details, refer to the documentation.
The following is the Maven POM configuration equivalent for deploying a project to Maven Central:
jeka.repos.publish=https://my.company/myrepo # Optional properties jeka.repos.publish.username=myUsername jeka.repos.publish.password=myPassword jeka.repos.publish.headers.Authorization=Bearer:: XHrU8hHKJHJ454==67g
Jeka provides a simpler, yet powerful way to build Java software and publish artifacts to Maven Central or other repositories, with much less configuration and effort than traditional tools.
Visit the website, videos, and examples to get an idea of everything Jeka can do better.
Disclaimer: I am the author of Jeka.
The above is the detailed content of JeKa: The Simplest Way to Publish on Maven Central. For more information, please follow other related articles on the PHP Chinese website!