简体   繁体   中英

How to produce a executable jar with maven that contains a subset of dependencies?

I have a maven SWT project that has multiple dependencies: dependency 1, dependency 2, dependency 3,...., dependency n.

I want to make an executable jar that contains only the project and dependency 1 and dependency 2 but the other dependencies will be copied on the /lib directory.

Here is my assembly config :

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-5</version>
  <dependencies>
    <dependency>
      <groupId>com.company</groupId>
      <artifactId>dependency1</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.company</groupId>
      <artifactId>dependency2</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
  <configuration>
    <descriptors>
      <descriptor>mvnDescript.xml</descriptor>
    </descriptors>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <mainClass>${project.build.mainClass}</mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <id>make-my-jar-with-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

and the mvnDescript.xml :

<assembly
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <formats>
    <format>dir</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <unpack>false</unpack>
      <includes>
        <include>${artifact}</include>
      </includes>
    </dependencySet>
    <dependencySet>
      <outputDirectory>/lib</outputDirectory>
      <unpack>false</unpack>
      <excludes>
        <exclude>${artifact}</exclude>
      </excludes>
    </dependencySet>
  </dependencySets>
</assembly>

To create such an executable jar you should understand that you defined the dependencies as elements for the classpath of the maven-assembly-plugin which is not what you want to do. Your project should define those dependencies instead. And of course you should use an up-to-date version of the plugin.

  <dependencies>
    <dependency>
      <groupId>com.company</groupId>
      <artifactId>dependency1</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.company</groupId>
      <artifactId>dependency2</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.4</version>
  <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <mainClass>${project.build.mainClass}</mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <id>jar-with-deps</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

You can use the maven shade plugin to create a maven project that contains the code, and dependencies 1 and 2.

Then import that 'uber' jar module as a dependency into a different WAR module as dependency, and add the other dependencies there.

The end result will the that the project code and dependencies 1 and 2 will be in WEB-INF/lib/jar-with-dep1-and-2.jar, together with the other jars dependency3.jar, dependency4.jar, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM