With Maven, how can I build a distributable that has my project's jar and all of the dependent jars?

前端 未结 3 1313

I have a project (of type \'jar\') that (obviously) builds a jar. But that project has many dependencies. I\'d like Maven to build a \"package\" or \"assembly\" that cont

相关标签:
3条回答
  • 2020-12-01 04:14

    I have used the maven assembly plugin to packages everything in one single jar. you can find info here http://maven.apache.org/plugins/maven-assembly-plugin/ http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

    HTH.

    0 讨论(0)
  • 2020-12-01 04:20

    For a single module, I'd use an assembly looking like the following one (src/assembly/bin.xml):

    <assembly>
      <id>bin</id>
      <formats>
        <format>tar.gz</format>
        <format>tar.bz2</format>
        <format>zip</format>
      </formats>
      <dependencySets>
        <dependencySet>
          <unpack>false</unpack>
          <scope>runtime</scope>
          <outputDirectory>lib</outputDirectory>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <directory>src/main/command</directory>
          <outputDirectory>bin</outputDirectory>
          <includes>
            <include>*.sh</include>
            <include>*.bat</include>
          </includes>
        </fileSet>
      </fileSets>
    </assembly>
    

    To use this assembly, add the following configuration to your pom.xml:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptors>
            <descriptor>src/assembly/bin.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    

    In this sample, start/stop scripts located under src/main/command and are bundled into bin, dependencies are bundled into lib. Customize it to suit your needs.

    0 讨论(0)
  • 2020-12-01 04:33

    Here is my solution to create a distributable .zip (or .tar.gz / .tar.bz2) including all dependencies in a lib folder. It will:

    1. Create a jar with a Manifest including the dependencies of the lib directory as the classpath and the main class to run when executing the jar.
    2. Copy all dependent jars to the target/lib directory.
    3. Create the distributable `zip with the main jar and all dependent jars of the lib directory.

    Excerpt from pom.xml:

    <!-- create distributable -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>full.path.to.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>attached</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/resources/dist.xml</descriptor>
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    dist.xml:

    <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>bin</id>
        <formats>
            <format>zip</format>
            <format>tar.gz</format>
        </formats>
        <fileSets>
            <fileSet>
                <directory>${project.basedir}</directory>
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>README*</include>
                    <include>LICENSE*</include>
                    <include>NOTICE*</include>
                </includes>
            </fileSet>
            <fileSet>
                <directory>${project.build.directory}</directory>
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
            <fileSet>
                <directory>${project.build.directory}/lib</directory>
                <outputDirectory>lib</outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
            <fileSet>
                <directory>${project.build.directory}/site</directory>
                <outputDirectory>docs</outputDirectory>
            </fileSet>
        </fileSets>
    </assembly>
    

    The dist.xml was derived from the bin descriptor format here: http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#bin

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