Packaging a jar into a dist dir with separated external resources and dependencies

后端 未结 2 1409
粉色の甜心
粉色の甜心 2020-12-25 14:43

Here\'s what I\'m trying to achieve - a dist directory (or a zip file) that looks like this:

dist/
|-- application-1.0.jar
|-- conf/
    |-- app         


        
相关标签:
2条回答
  • 2020-12-25 15:24

    You need to use two plugins to accomplish this: maven-jar-plugin and maven-assembly-plugin.

    Useful pom.xml samples:

    • how make JAR executable and set manifest classpath
    • how to exclude files from JAR file
    • assembly plugin usage

    (I would recommend you to separate the user-editable properties files to separate directory, but it is a matter of taste.)

    Sample assembly configuration, to get you started:

    <assembly>
      <id>dist</id>
      <formats>
        <format>zip</format>
      </formats>
      <includeBaseDirectory>true</includeBaseDirectory>
      <baseDirectory>dist</baseDirectory>
      <dependencySets>
        <dependencySet>
          <outputDirectory>lib</outputDirectory>
          <scope>runtime</scope>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <directory>src/conf</directory>
          <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
          <directory>src/run</directory>
          <outputDirectory></outputDirectory>
          <excludes>
            <exclude>*.sh</exclude>
          </excludes>
        </fileSet>
      </fileSets>
      <files>
        <file>
          <source>src/run/run.sh</source>
          <outputDirectory></outputDirectory>
          <fileMode>0755</fileMode>
        </file>
      </files>
    </assembly>
    
    0 讨论(0)
  • 2020-12-25 15:26

    The solution using the assembly plugin has a few parts:

    • The pom includes configuring the jar plugin (maven-jar-plugin), and configuring the assembly plugin (maven-assembly-plugin).
    • During maven's packaging phase, the jar plugin is called to construct the application jar.
    • Then the assembly plugin is run, and combines the constructed jar, resources and dependencies into a zip file as defined by the assembly file (distribution-zip.xml).

    In the pom, configure the plugins:

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <archive>
                        <!-- Make an executable jar, adjust classpath entries-->
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>./lib/</classpathPrefix>
                            <mainClass>com.acme.KillerApp</mainClass>
                        </manifest>
                        <!--Resources will be placed under conf/-->
                        <manifestEntries>
                            <Class-Path>./conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--exclude the properties file from the archive-->
                    <excludes>
                        <exclude>*.properties</exclude>
                    </excludes>
                </configuration>
            </plugin>
    
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <descriptors>
                        <descriptor>${basedir}/assembly/distribution-zip.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    ...
    

    The contents of the assembly file distribution-zip.xml (with thanks to Neeme Praks) combines the created jar, resources and dependencies:

    <assembly>
        <id>dist</id>
        <formats>
            <format>zip</format>
        </formats>
    
        <includeBaseDirectory>true</includeBaseDirectory>
    
        <dependencySets>
            <dependencySet>
                <!--Include runtime dependencies-->
                <outputDirectory>lib</outputDirectory>
                <scope>runtime</scope>
            </dependencySet>
        </dependencySets>
    
        <fileSets>
            <fileSet>
                <!--Get the generated application jar-->
                <directory>${project.build.directory}</directory>
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
            <fileSet>
                <!--Get application resources-->
                <directory>src/main/resources</directory>
                <outputDirectory>conf</outputDirectory>
            </fileSet>
            <fileSet>
                <!--Get misc user files-->
                <directory>${project.basedir}</directory>
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>README*</include>
                    <include>LICENSE*</include>
                    <include>NOTICE*</include>
                </includes>
            </fileSet>       
        </fileSets>
    </assembly>
    

    The resulting distributable zip file is created like target/killer-app-1.0-dist.zip!

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