It's been a while since I posted, so here's a quick and dirty code by example:
If you want to make a standalone jar (i.e., one with no dependencies) executable, here's how you do it:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.yourcompany.YourClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
If you have dependencies, you will probably want to bundle them into a single jar, otherwise making it executable is not helpful. You can use the assembly plugin to do it like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.yourcompany.YourClass</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
A few comments:
-
I didn't specify plugin versions in the example. You should always specify a plugin version, but you should do it in the pluginManagement section instead. See this post for how and why.
-
Notice the assembly plugin configuration has an execution, while the jar plugin does not. This is because the jar plugin will run automatically based on the packaging, which is assumed to be "jar" in this example. The assembly plugin needs an execution to "bind" it to the life cycle and here we tell it which goal to run and which phase to run in.
-
Notice also that the jar plugin is configured in pluginManagement. This is my personal best practice, and I explain why here.
See more Maven how-tos and best practices here.
Brian Fox, CTO and co-founder of Sonatype, is a Governing Board Member for the Open Source Security Foundation (OpenSSF), a Governing Board Member for the Fintech Open Source Foundation (FINOS), a member of the Monetary Authority of Singapore Cyber and Technology Resilience Experts (CTREX) Panel, a member of the Apache Software Foundation and former Chair of the Apache Maven project. Working with OpenSSF, Brian helped create The Open Source Consumption Manifesto, urging organizations to elevate awareness of open source usage. He also chaired efforts to provide official responses to requests for information from the The Office of the National Cybersecurity Directorate (ONCD) and the Cybersecurity and Infrastructure Security Agency (CISA). Within the Atlantic Council's Open Source Policy Network, Brian actively helps shape cybersecurity strategy, offering valuable insights on critical documents, such as ONCD's recent National Cyber Security Strategy. Brian has over 20 years of experience driving the vision behind, as well as developing and leading the development of software for organizations ranging from startups to large enterprises. Brian is a frequent speaker at national and regional events including Java User Groups and other security and development-related conferences.
Explore All Posts by Brian FoxTags
Try Nexus Repository Free Today
Sonatype Nexus Repository is the world’s most trusted artifact repository manager. Experience the difference and download Community Edition for free.