简体   繁体   English

如何使用maven生成包含依赖项子集的可执行jar?

[英]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. 我有一个Maven SWT项目,它具有多个依赖项:依赖项1,依赖项2,依赖项3,....,依赖项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. 我想制作一个仅包含项目,依赖项1和依赖项2的可执行jar,但其他依赖项将复制到/ lib目录中。

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 : 和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. 要创建这样的可执行jar,您应该理解,您已将依赖项定义为maven-assembly-plugin的类路径的元素,这不是您想要执行的操作。 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. 您可以使用maven shade插件创建一个包含代码以及依赖项1和2的maven项目。

Then import that 'uber' jar module as a dependency into a different WAR module as dependency, and add the other dependencies there. 然后将“超级” jar模块作为依赖项导入到另一个WAR模块作为依赖项,然后在其中添加其他依赖项。

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. 最终结果是,项目代码以及依赖项1和2将与其他jar依赖项3.jar,dependency4.jar等一起位于WEB-INF / lib / jar-with-dep1-and-2.jar中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何生成具有所有 maven 依赖项的可执行 jar 文件? - How to produce an executable jar file with all maven dependencies? Maven 可执行文件 JAR 与依赖项 - Maven Executable JAR with dependencies 如何使用 maven 和系统依赖项创建可执行的 jar? - How to create executable jar with maven and system dependencies? 如何使用Maven创建一个没有依赖关系的可执行jar? - How can I create an executable jar without dependencies using Maven? 如何使用 Maven 创建具有依赖关系的可执行 JAR? - How can I create an executable JAR with dependencies using Maven? Maven:生成具有所有依赖项的可执行jar文件 - Maven: Generating an executable jar file with all the dependencies 可执行 jar 具有依赖关系和 dll 依赖关系使用 maven - Executable jar with dependencies and dll dependency using maven maven-shade-plugin:如何将所有依赖项和自定义本地jar添加到可执行jar? - maven-shade-plugin : how add all dependencies and custom local jar to executable jar? 如何使用Maven在jar的子文件夹中创建具有依赖项的可执行jar? - How can I create an executable jar with dependencies in sub folder in jar using Maven? 使用Maven将Java代码和依赖项组合到一个jar(可执行jar)中 - Combine java code and dependencies into one jar (executable jar) using maven
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM