我想将我的项目打包在一个可执行的JAR中进行分发。

如何使Maven项目将所有依赖JAR打包到输出JAR中?


当前回答

这篇博客文章展示了结合maven jar和maven汇编插件的另一种方法。通过博客文章中的程序集配置XML文件,还可以控制依赖项是否将被扩展或仅被收集在文件夹中并由清单中的类路径条目引用:

理想的解决方案是将jar包含在lib文件夹中,主jar的manifest.mf文件包含类路径中的所有jar。

这里描述的正是这个:使用Maven的可执行JAR文件和从属JAR文件

其他回答

我长期使用Maven汇编插件,但我找不到解决“已添加,跳过”问题的方法。现在,我正在使用另一个插件——onejar maven插件。下面是一个示例(mvn包构建JAR文件):

<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
            <configuration>
                <mainClass>com.company.MainClass</mainClass>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

您需要为该插件添加存储库:

<pluginRepositories>
    <pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
    </pluginRepository>
</pluginRepositories>

已经有数百万个答案了。我想补充一下,如果您不需要在应用程序中添加entryPoint,那么您不需要<mainClass>。例如,API可能不一定有主方法。

Maven插件配置

  <build>
    <finalName>log-enrichment</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

建筑

mvn clean compile assembly:single

验证

ll target/
total 35100
drwxrwx--- 1 root vboxsf     4096 Sep 29 16:25 ./
drwxrwx--- 1 root vboxsf     4096 Sep 29 16:25 ../
drwxrwx--- 1 root vboxsf        0 Sep 29 16:08 archive-tmp/
drwxrwx--- 1 root vboxsf        0 Sep 29 16:25 classes/
drwxrwx--- 1 root vboxsf        0 Sep 29 16:25 generated-sources/
drwxrwx--- 1 root vboxsf        0 Sep 29 16:25 generated-test-sources/
-rwxrwx--- 1 root vboxsf 35929841 Sep 29 16:10 log-enrichment-jar-with-dependencies.jar*
drwxrwx--- 1 root vboxsf        0 Sep 29 16:08 maven-status/

要从命令行本身创建可执行JAR,只需从项目路径运行以下命令:

mvn assembly:assembly

它的工作原理如下:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
        </execution>
    </executions>
</plugin>

解包必须处于生成资源阶段,否则将不作为资源包含。

好吧,这是我的解决方案。我知道它没有使用pom.xml文件。但我在NetBeans上编译和运行程序时遇到了问题,但当我尝试Java-jarMyJarFile.jar时失败了。现在,我不完全理解Maven,我想这就是为什么在NetBeans8.0.2中将我的jar文件包含在库中以将其放入jar文件时遇到了困难。我在想我以前是如何在Eclipse中使用没有Maven的JAR文件的。

Maven可以编译所有的依赖项和插件。不是NetBeans。(如果您可以获得NetBeans并能够使用java.jar来实现这一点,请告诉我如何(^.^)v)

已解决-针对Linux:通过打开终端。

Then

cd /MyRootDirectoryForMyProject

Next

mvn org.apache.maven.plugins:maven-compiler-plugin:compile

Next

mvn install

这将在目标目录中创建一个JAR文件。

MyJarFile-1.0-jar-with-dependencies.jar

Now

cd target

(您可能需要运行:chmod+x MyJarFile-1.0-jar-with-dependencies.jar)

最后

java -jar MyJarFile-1.0-jar-with-dependencies.jar

请参阅LifecyclePhaseNotFoundException。