Java – how to create an independent executable jar using Maven?

I want to package it in a separate executable jar for distribution I need an executable like main Jar and all dependencies are in LIBS / * Jar

How to make Maven executable jar not included in the dependent Library in advance?

How can I create an executable jar with dependencies using Maven? At 10:10 on October 1, 2004, there was a description of Andr é aronsen, but that person did not work at all (s.a.descriptorref was not set and failed)

Solution

You can achieve this to some extent

First, you will create an executable jar by configuring the Maven jar plugin appropriately

You will then create a jar dependency using the Maven assembly plug-in, excluding the project jar To do this, you will create a descriptor file, such as Src / main / assembly / descriptor XML, like this

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">>
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>false</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Use it in such projects

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/descriptor.xml</descriptor>
          </descriptors>
        </configuration>
        [...]
</project>

You will end up with two jars - one executable jar created by your project and the other jar dependency created by the assembly plug-in

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>