【问题标题】:Building executable jar with maven?用maven构建可执行jar?
【发布时间】:2010-12-21 08:01:30
【问题描述】:

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

How can I create an executable JAR with dependencies using Maven?

我将那里显示的 sn-p 添加到 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 文件无法运行吗?另外,为什么有两个罐子开始?如果您需要更多信息,请告诉我。

这里是完整的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>

【问题讨论】:

    标签: java eclipse maven-2 build-process executable-jar


    【解决方案1】:

    实际上,我认为您提到的question 中给出的答案只是错误UPDATE - 20101106:有人修复了它,这个 em> 答案指的是version preceding the edit),这至少部分解释了您遇到麻烦的原因。


    在logmanager/target中生成两个jar文件:logmanager-0.1.0.jar和logmanager-0.1.0-jar-with-dependencies.jar。

    第一个是jar:jarpackage阶段生成的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
    

    同样,如果您按照建议配置了程序集插件,您将拥有如下内容:

      <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。然后,cd 进入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>
    

    【讨论】:

    • 感谢您的 cmets!我按照您的指定更改了我的 pom.xml。当我运行 mvn clean install 时,我的代码中出现了一堆编译错误,说 -source 1.3 不支持注释等。我正在使用 jdk1.6,它在 eclipse 中编译;我不确定 1.3 是如何引入的。也许 pom sn-p 中的库版本之一是旧版本?
    • 谢谢!我解决了 1.3 的问题。我还必须将 junit4 依赖项添加到我的 POM 中。我正在解决其他问题;如果我被卡住了,我会再发一次!如果我让 jar 运行,我会将其标记为答案。我当前的 POM 已在上述问题中更新。
    • 有没有办法从生成的jar文件中排除资源?
    • 是否可以让生成的 jar 具有“正常”名称?
    • 我还发现这个Sonatype blog post很有用
    【解决方案2】:

    Pascal Thivent 的回答也帮助了我。 但是如果您在&lt;pluginManagement&gt;元素中管理您的插件,您必须在插件管理之外再次定义程序集,否则如果您运行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>
    

    【讨论】:

    • 谢谢迈克,它帮助了我。最初我的包是在不使用 的情况下生成的。但是 Eclipse 在 pom.xml “maven - 生命周期未涵盖插件执行”中给出了一些错误。这让我分心。所以为了解决这个问题,我添加了 ,现在 eclipse 错误消失了,但我的包停止构建。你上面的 pom sn-p 对我有用。 :)
    • 这很有用.. 使用 时,最佳答案不起作用。
    【解决方案3】:

    如果你不想在包上执行汇编目标,你可以使用下一个命令:

    mvn package assembly:single
    

    这里 package 是关键字。

    【讨论】:

      【解决方案4】:

      右键项目,给maven build,maven clean,maven generate resource,maven install。jar文件会自动生成。

      【讨论】:

        猜你喜欢
        • 2017-03-19
        • 2018-01-31
        • 2019-11-22
        • 1970-01-01
        • 2019-05-22
        • 1970-01-01
        • 2014-06-05
        • 1970-01-01
        相关资源
        最近更新 更多