使用Maven构建可执行的jar文件?

130

我正在尝试使用Maven为一个名为"logmanager"的小型家庭项目生成可执行的jar文件,就像这样:

如何使用Maven创建带有依赖关系的可执行JAR文件?

我将那里显示的代码片段添加到pom.xml中,并运行mvn assembly:assembly。它在logmanager/target文件夹中生成了两个jar文件:logmanager-0.1.0.jar和logmanager-0.1.0-jar-with-dependencies.jar。当我双击第一个jar文件时出现错误:

Could not find the main class: com.gorkwobble.logmanager.LogManager. Program will exit.

当我双击jar-with-dependencies.jar文件时,出现了一个略微不同的错误:

Failed to load Main-Class manifest attribute from: C:\EclipseProjects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar

我复制并粘贴了路径和类名,并在 POM 中检查了拼写。我的主类从 Eclipse 的启动配置中可以正常启动。有人能帮我找出为什么我的 JAR 文件不能运行吗?还有,为什么一开始就有两个 JAR 文件?如果您需要更多信息,请告诉我。

以下是完整的 pom.xml,供参考:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gorkwobble</groupId>
  <artifactId>logmanager</artifactId>
  <name>LogManager</name>
  <version>0.1.0</version>
  <description>Systematically renames specified log files on a scheduled basis. Designed to help manage MUSHClient logging and prevent long, continuous log files.</description>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <!-- nothing here -->
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-4</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.gorkwobble.logmanager.LogManager</mainClass>
                </manifest>
              </archive>
            </configuration>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
    </plugins>
  </build>
  <dependencies>
    <!-- commons-lang -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency> 

    <!-- Quartz scheduler -->
    <dependency>
        <groupId>opensymphony</groupId>
        <artifactId>quartz</artifactId>
        <version>1.6.3</version>
    </dependency>
    <!-- Quartz 1.6.0 depends on commons collections -->
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.1</version>
    </dependency>
    <!-- Quartz 1.6.0 depends on commons logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1</version>
    </dependency>
    <!-- Quartz 1.6.0 requires JTA in non J2EE environments -->
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
      <scope>runtime</scope>
    </dependency>

    <!-- junitx test assertions -->
    <dependency>
        <groupId>junit-addons</groupId>
        <artifactId>junit-addons</artifactId>
        <version>1.4</version>
        <scope>test</scope>
    </dependency>

    <!-- junit dependency; FIXME: make this a separate POM -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.1</version>
    </dependency>

  </dependencies>
  <dependencyManagement>
  </dependencyManagement>
</project>
4个回答

249

实际上,我认为你提到的问题中给出的答案是错误的更新-20101106:有人修复了,答案指的是编辑之前版本的答案),这部分解释了为什么你会遇到麻烦。


它在logmanager/target目录下生成了两个JAR文件:logmanager-0.1.0.jar和logmanager-0.1.0-jar-with-dependencies.jar。

第一个是在package阶段通过jar:jar生成的logmanager模块的JAR文件(因为该模块的打包类型为jar)。第二个是由assembly:assembly生成的汇编文件,应该包含当前模块及其依赖项的类(如果你使用的是描述符jar-with-dependencies)。

当我双击第一个JAR时,我遇到了一个错误:

Could not find the main class: com.gorkwobble.logmanager.LogManager. Program will exit.
如果你按照引用链接中提出的配置进行了设置,那么你就已经配置了jar插件生成可执行文件。大致像这样:
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>com.gorkwobble.logmanager.LogManager</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>

因此,logmanager-0.1.0.jar确实是可执行的,但是,原因有两个:1.这不是您想要的(因为它没有所有依赖项),2.它不包含com.gorkwobble.logmanager.LogManager(这就是错误提示的内容,请检查jar包的内容)。

当我双击jar-with-dependencies.jar时,出现了稍微不同的错误:

Failed to load Main-Class manifest attribute from: C:\EclipseProjects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar
再次强调,如果按照建议配置了assembly插件,你就会得到像这样的东西:
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>

通过这个设置,logmanager-0.1.0-jar-with-dependencies.jar包含当前模块它的依赖项中的类,但是根据错误信息,它的META-INF/MANIFEST.MF 不包含Main-Class条目(很可能不是与logmanager-0.1.0.jar相同的MANIFEST.MF)。实际上,该jar文件不可执行,这也不是你想要的。


所以,我的建议是从maven-jar-plugin中删除configuration元素,并像这样配置maven-assembly-plugin:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <!-- nothing here -->
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>org.sample.App</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

当然,将org.sample.App替换为您想要执行的类名。还有一个小奖励,我已经将assembly:single绑定到了package阶段,因此您不必再运行assembly:assembly了。只需运行mvn install,就会在标准构建期间生成汇编文件。

因此,请使用上面提供的配置更新您的pom.xml文件,并运行mvn clean install。然后,进入target目录并重试:

java -jar logmanager-0.1.0-jar-with-dependencies.jar

如果出现错误,请在更新问题时附上错误信息,并发布META-INF/MANIFEST.MF文件的内容以及pom.xml的相关部分(插件配置部分)。同时请发布以下结果:

java -cp logmanager-0.1.0-jar-with-dependencies.jar com.gorkwobble.logmanager.LogManager

为了证明它在命令行上工作正常(无论eclipse说什么),需要演示。

编辑:对于Java 6,您需要配置maven-compiler-plugin。将以下内容添加到您的pom.xml:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>

4
谢谢您的评论!我根据您的要求更改了我的pom.xml文件。当我运行mvn clean install时,我的代码出现大量编译错误,指出注释等不支持-source 1.3。我正在使用jdk1.6,它可以在eclipse中编译;我不确定1.3是如何引入的。也许pom片段中的某个库版本比较老? - RMorrisey
谢谢!我已经解决了1.3问题。我还不得不将junit4依赖项添加到我的POM中。我正在解决其他问题;如果我卡住了,我会再次发布!如果我成功运行jar文件,我会将其标记为答案。我的当前POM已在上面的问题中更新。 - RMorrisey
很好的回答,但是我在我的汇编中遇到了一个小问题。当我将appendAssemblyId切换为false时,我的类在输出jar文件中会被复制一次!有什么办法可以解决这个问题吗?我假设汇编插件通常会创建一个带有“-jar-with-dependencies”后缀的新jar文件,但由于我关闭了它,它会将依赖项与项目文件一起附加到现有的jar文件(使用jar:jar创建)。 - Wojciech Owczarczyk
2
生成的jar文件是否可以有一个“正常”的名称? - Daniil Shevelev
1
我也发现这篇Sonatype博客文章很有用。 - user2964500
显示剩余7条评论

17
< p > < em > Pascal Thivent 的答案也帮了我很大的忙。 < strong > 但是 ,如果您在 < code >&lt; pluginManagement&gt; 元素中管理插件,则必须再次在插件管理外部定义程序集,否则如果运行< code > mvn install ,则依赖项不会打包到 jar 中。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>main.App</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

            </plugins>

        </pluginManagement>

        <plugins> <!-- did NOT work without this  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
        </plugins>

    </build>


    <dependencies>
       <!--  dependencies commented out to shorten example -->
    </dependencies>

</project>

1
谢谢Mike,这对我很有帮助。最初我的包在没有使用<pluginManagement>的情况下生成。但是Eclipse在pom.xml中出现了一些错误“maven-Plugin execution not covered by lifecycle”,这让我分心。为了解决这个问题,我添加了<pluginManagement>,现在Eclipse的错误消失了,但是我的包停止了构建。你上面的pom片段对我很有用。 :) - shashaDenovo
2
这很有用...在使用<pluginManagement>时,顶部的答案不起作用。 - ininprsr

5
如果您不想在软件包上执行汇编目标,则可以使用以下命令:
mvn package assembly:single

这里的package是关键字。

-1

右键单击项目,选择maven build、maven clean、maven generate resource和maven install。jar文件将自动生成。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接