且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Maven 2构建一个可运行的jar

更新时间:2021-09-21 02:22:07

最简单的方法是使用 maven-assembly-plugin 和预定义的 jar-with-dependencies 描述符。您还需要为此超级jar生成带有主类条目的清单。下面的代码段显示了如何配置程序集插件:

The easiest way to do this would be to create an assembly using the maven-assembly-plugin and the predefined jar-with-dependencies descriptor. You'll also need to generate a manifest with a main-class entry for this uber jar. The snippet below shows how to configure the assembly plugin to do so:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

然后,要生成程序集,只需运行:

Then, to generate the assembly, just run:

mvn assembly:assembly

如果你想生成程序集作为构建的一部分,只需将程序集:单个 mojo绑定到程序包阶段:

If you want to generate the assembly as part of your build, simply bind the assembly:single mojo to the package phase:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

只需运行:

mvn package