How can I create an executable jar without dependencies using Maven?

后端 未结 2 1049
旧时难觅i
旧时难觅i 2020-12-30 03:32

I want to package it not in a single executable jar for distribution. I need an executable to be something like main.jar and all dependencies to be in libs/*.jar

How

相关标签:
2条回答
  • 2020-12-30 03:48

    You can achieve this to a certain extent.

    Firstly, you would create an executable jar by configuring maven jar plugin suitably.

    You would then use maven assembly plugin to create a jar-with-dependencies, excluding your project jar. To do this, you would create a descriptor file, say 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 your project like this.

    <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>
            [...]
          </plugin>
        </plugins>
      </build>
    </project>
    

    You will end up getting two jars - one the executable jar created by your project and the other the jar-with-dependencies created by the assembly plugin.

    0 讨论(0)
  • 2020-12-30 04:00

    I prefer this a bit modified solution. Create Your executable jar with classpath set and copy all dependencies to given directory.

    You don't need any additional files.

    Dependencies are being copied during install phase.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>fully.qualified.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!-- optional -->
                            <!-- exclude copying test and provided dependencies -->
                            <includeScope>runtime</includeScope>
                            <excludeScope>provided</excludeScope>
                            <!-- optional -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    I updated the answer to copy only runtime dependencies as described here - copying the right dependencies. Test and provided dependencies are excluded.

    It turns out that if you want to exclude dependencies from both the test and provided scopes, you need to exclude the provided scope and include the runtime scope.

    0 讨论(0)
提交回复
热议问题