代码之家  ›  专栏  ›  技术社区  ›  RMorrisey

用maven构建可执行jar?

  •  117
  • RMorrisey  · 技术社区  · 16 年前

    我正在尝试使用maven为一个名为“logmanager”的小型家庭项目生成一个可执行jar,如下所示:

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

    我将这里显示的代码片段添加到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.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 回复  |  直到 8 年前
        1
  •  247
  •   Community Mohan Dere    6 年前

    事实上,我认为 question 你提到的只是 更新-20101106: 有人修好了, 答案是指 version preceding the edit


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

    package 逐步 jar:jar (因为模块的包装类型为 jar assembly:assembly 并且应该包含来自当前模块及其依赖项的类(如果使用描述符 jar-with-dependencies

    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 可执行文件,这也不是您想要的。


    因此,我的建议是删除 configuration 元素,并按如下方式配置maven assembly插件:

      <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 包裹 这样你就不用跑了 组装:组装 mvn install 并且该组件将在标准构建期间生产。

    mvn clean install . 然后,将cd放入 target 目录,然后重试:

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

    文件及其相关部分 pom.xml (插件配置部分)。另外,请张贴以下结果:

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

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    
        2
  •  16
  •   mike    10 年前

    答案 帕斯卡Thivent 我也帮了忙。 但是 如果您在 <pluginManagement> mvn install .

    <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>
    
        3
  •  5
  •   leonidv    15 年前

    如果不希望在包上执行程序集目标,可以使用下一个命令:

    mvn package assembly:single
    

    在这里 是关键字。

        4
  •  -1
  •   dolly doll    10 年前

    推荐文章