如何使用Maven创建一个没有依赖关系的可执行jar?

时间:2011-11-30 12:17:06

标签: java maven pom.xml

我想将它打包在一个可执行的jar中以便分发。我需要一个可执行文件,例如main.jar,所有依赖项都在libs / * .jar

如何在没有预先包含在依赖库的情况下制作maven可执行jar?

How can I create an executable JAR with dependencies using Maven?中,有一条注释在2010年12月1日10:46回答 AndréAronsen,但那个根本不起作用(失败的s.a.descriptorRef未设置)。

2 个答案:

答案 0 :(得分:11)

我更喜欢这个有点修改过的解决方案。创建具有类路径集的可执行jar,并将所有依赖项复制到给定目录。

您不需要任何其他文件。

在安装阶段正在复制依赖关系。

<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>

我更新了答案,只复制了here - copying the right dependencies所描述的运行时依赖项。测试和提供的依赖项被排除在外。

  

事实证明,如果要从测试范围和提供的范围中排除依赖关系,则需要排除提供的范围并包括运行时范围。

答案 1 :(得分:10)

你可以在一定程度上达到这个目的。

首先,您可以通过适当配置executable jar来创建maven jar plugin

然后,您将使用maven程序集插件创建一个依赖jar的依赖项,不包括项目jar 。为此,您将创建一个描述符文件,比如src/main/assembly/descriptor.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>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>

在你的项目中使用它。

<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>

最终会得到两个罐子 - 一个是由项目创建的可执行jar,另一个是由程序集插件创建的jar-with-dependencies。

相关问题