使用maven构建可执行文件jar?

问题描述:

我试图产生所谓的“日志管理”使用maven家用小型项目可执行的JAR,就像这样:使用maven构建可执行文件jar?

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

我说有证明pom.xml中的片段,跑MVN组装:组装。它在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-与-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> 

其实,我认为,在你所提到的question给出的答案是刚刚错误更新 - 20101106:有人修好了,答案指的是version preceding the edit),这至少部分解释了为什么你遇到麻烦。


它产生在日志管理/靶两个jar文件:日志管理-0.1.0.jar,和日志管理-0.1.0-罐与 - dependencies.jar。

第一个是(因为该模块具有jar型包装)由jar:jar期间package阶段生成的日志管理模块的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时会出现一个稍微不同的错误。罐子:

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-插件删除configuration元素和配置Maven的组装插件这样的:

<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:singlepackage阶段,所以你不必再运行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 

证明它是在命令行中工作正常(不管什么日食说的)。

编辑:对于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时,我从代码中得到一堆编译错误,并说在源1.3中不支持注释等。我使用jdk1.6,它在eclipse中编译;我不确定1.3是如何引入的。也许在pom片段中的一个库版本是一个较旧的版本? – RMorrisey 2009-11-29 07:46:54

+0

谢谢!我通过了1.3的问题。我还必须将junit4依赖项添加到我的POM中。我正在解决其他问题;如果我卡住了,我会再次发布!如果我运行jar,我会将其标记为答案。我目前的POM在上面的问题中更新。 – RMorrisey 2009-11-29 21:12:11

+0

有没有什么办法从生成的jar文件中排除资源? – 2010-11-03 17:24:18

如果你不希望在包执行汇编的目标,你可以使用一个命令:

mvn package assembly:single 

这里是关键字。

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

谢谢迈克,它帮助了我。最初我的软件包是在不使用的情况下生成的。但是Eclipse在pom.xml中提供了一些错误“maven - 生命周期中未涉及的插件执行”。这分散了我的注意力。所以为了解决这个问题,我添加了,现在eclipse错误消失了,但是我的包停止了构建。你上面的pom片段已经为我工作。 :) – shashaDenovo 2015-07-01 18:33:08

+2

这很有用..当使用时,最佳答案将不起作用。 – ininprsr 2015-07-20 12:09:31

右键单击该项目,并给予maven构建,maven clean,maven生成资源和maven install.jar文件会自动生成。