簡體   English   中英

用Maven構建可執行jar?

[英]Building executable jar with maven?

我正在嘗試使用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。 雙擊第一個罐子時出現錯誤:

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>

實際上,我認為您提到的問題給出的答案是錯誤的UPDATE-20101106:有人對其進行了修復, 答案指的是編輯之前的版本 ),這至少部分地說明了您為什么會遇到麻煩的原因。


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

第一個是jar:jarpackage階段期間生成的logmanager模塊的jar:jar (因為該模塊的包裝類型為jar )。 第二個是由assembly:assembly assembly生成的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-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>

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>

如果不想在程序包上執行組裝目標,則可以使用下一個命令:

mvn package assembly:single

這里的是關鍵字。

右鍵單擊該項目,然后進行maven構建,maven清理,maven生成資源和maven安裝。jar文件將自動生成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM