使用maven构建可执行jar的问题 [英] Problem building executable jar with maven

查看:126
本文介绍了使用maven构建可执行jar的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



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

 找不到主类:com.gorkwobble.logmanager .LogManager。程序将退出。 

当我双击jar-with-dependencies.jar时,略有不同的错误:

 无法从以下位置加载Main-Class清单属性:C:\EclipseProjects\logmanager\target\logmanager-0.1.0- jar-with-dependencies.jar 

我复制并粘贴了路径和类名,并检查了拼写POM。我的主要类从eclipse启动配置启动。有人可以帮我弄清楚为什么我的jar文件不会运行?另外,为什么有两罐罐开始呢?如果您需要更多信息,请告知我。



谢谢!



 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns: xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>按计划系统地重命名指定的日志文件。旨在帮助管理MUSHClient日志记录并防止长时间连续的日志文件。< / description>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-jar-plugin< / artifactId>
< version> 2.2< / version>
<! - 这里没有 - >
< / 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>
<执行>
< 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>
<依赖关系>
<! - commons-lang - >
<依赖关系>
< groupId> commons-lang< / groupId>
< artifactId> commons-lang< / artifactId>
< version> 2.4< / version>
< / dependency>

<! - Quartz调度程序 - >
<依赖关系>
< groupId> opensymphony< / groupId>
< artifactId> quartz< / artifactId>
< version> 1.6.3< / version>
< / dependency>
<! - Quartz 1.6.0取决于公共集合 - >
<依赖关系>
< groupId> commons-collections< / groupId>
< artifactId> commons-collections< / artifactId>
< version> 3.1< / version>
< / dependency>
<! - Quartz 1.6.0取决于公共记录 - >
<依赖关系>
< groupId> commons-logging< / groupId>
< artifactId> commons-logging< / artifactId>
< version> 1.1< / version>
< / dependency>
<! - Quartz 1.6.0需要非J2EE环境中的JTA - >
<依赖关系>
< groupId> javax.transaction< / groupId>
< artifactId> jta< / artifactId>
< version> 1.1< / version>
< scope>运行时< / scope>
< / dependency>

<! - junitx测试断言 - >
<依赖关系>
< groupId> junit-addons< / groupId>
< artifactId> junit-addons< / artifactId>
< version> 1.4< / version>
< scope> test< / scope>
< / dependency>

<! - junit依赖; FIXME:使它成为一个单独的POM - >
<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.1< / version>
< / dependency>

< / dependencies>
< dependencyManagement>
< / dependencyManagement>
< / project>


解决方案

其实我认为,你提到的href =https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven>问题只是 更新 - 20101106:有人修正,这个回答是指编辑前的版本),这至少部分解释为什么你遇到麻烦。







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


第一个是在阶段中由$ code生成的logmanager模块的JAR > jar:jar (因为该模块具有类型为 jar 的包装)。第二个是由 assembly:assembly 生成的程序集,并且应该包含当前模块及其依赖项的类(如果使用描述符 jar-依赖关系)。


当我双击第一个jar时,我得到一个错误: p>

 找不到主类:com.gorkwobble.logmanager.LogManager。程序将退出。 


如果您应用建议的链接配置作为参考,您配置了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 确实可执行,但是这不是你想要的(因为它没有所有的依赖关系)和2.它不包含 com.gorkwobble.logmanager.LogManager (这是错误的说法,检查jar的内容)。


当我双击jar时有一个稍微不同的错误-with-dependencies.jar:

 无法从以下位置加载Main-Class清单属性: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 包含一个主类条目(它可能与logmanager-0.1.0中的MANIFEST.MF不一样。罐)。这个jar实际上是不是可执行文件,这不是你想要的。






我的建议是从maven-jar-plugin中删除配置元素,并配置maven-assembly-plugin,如下所示:

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-jar-plugin< / artifactId>
< version> 2.2< / version>
<! - 这里没有 - >
< / 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>
<执行>
< execution>
< phase> package< / phase>
< goals>
< goal> single< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

当然,替换 org.sample.App 与你想要执行的类。小奖金,我已经绑定程序集:单个阶段,所以你不必运行code> assembly:assembly 只需运行 mvn install ,并在标准版本中生成程序集。



所以,请更新你的pom。 xml与上面给出的配置运行 mvn clean install 。然后,cd进入目标目录,然后重试:

  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>


I am trying to generate an executable jar for a small home project called "logmanager" using maven, just like this:

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

I added the snippet shown there to the pom.xml, and ran mvn assembly:assembly. It generates two jar files in logmanager/target: logmanager-0.1.0.jar, and logmanager-0.1.0-jar-with-dependencies.jar. I get an error when I double-click on the first jar:

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

A slightly different error when I double-click the jar-with-dependencies.jar:

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

I copied and pasted the path and classname, and checked the spelling in the POM. My main class launches fine from an eclipse launch configuration. Can someone help me figure out why my jar file won't run? Also, why are there two jars to begin with? Let me know if you need more information.

Thanks!

[Edit: my POM is now as follows:]

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

解决方案

Actually, I think that the answer given in the question you mentioned is just wrong (UPDATE - 20101106: someone fixed it, this answer refers to the version preceding the edit) and this explains, at least partially, why you run into troubles.


It generates two jar files in logmanager/target: logmanager-0.1.0.jar, and logmanager-0.1.0-jar-with-dependencies.jar.

The first one is the JAR of the logmanager module generated during the package phase by jar:jar (because the module has a packaging of type jar). The second one is the assembly generated by assembly:assembly and should contain the classes from the current module and its dependencies (if you used the descriptor jar-with-dependencies).

I get an error when I double-click on the first jar:

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

If you applied the suggested configuration of the link posted as reference, you configured the jar plugin to produce an executable artifact, something like this:

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

So logmanager-0.1.0.jar is indeed executable but 1. this is not what you want (because it doesn't have all dependencies) and 2. it doesn't contain com.gorkwobble.logmanager.LogManager (this is what the error is saying, check the content of the jar).

A slightly different error when I double-click the jar-with-dependencies.jar:

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

Again, if you configured the assembly plugin as suggested, you have something like this:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>

With this setup, logmanager-0.1.0-jar-with-dependencies.jar contains the classes from the current module and its dependencies but, according to the error, its META-INF/MANIFEST.MF doesn't contain a Main-Class entry (its likely not the same MANIFEST.MF as in logmanager-0.1.0.jar). The jar is actually not executable, which again is not what you want.


So, my suggestion would be to remove the configuration element from the maven-jar-plugin and to configure the maven-assembly-plugin like this:

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

Of course, replace org.sample.App with the class you want to have executed. Little bonus, I've bound assembly:single to the package phase so you don't have to run assembly:assembly anymore. Just run mvn install and the assembly will be produced during the standard build.

So, please update your pom.xml with the configuration given above and run mvn clean install. Then, cd into the target directory and try again:

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

If you get an error, please update your question with it and post the content of the META-INF/MANIFEST.MF file and the relevant part of your pom.xml (the plugins configuration parts). Also please post the result of:

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

to demonstrate it's working fine on the command line (regardless of what eclipse is saying).

EDIT: For Java 6, you need to configure the maven-compiler-plugin. Add this to your 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>

这篇关于使用maven构建可执行jar的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆