Mavenで実行可能jarを構築していますか?
-
06-07-2019 - |
質問
<!> quot; logmanager <!> quotという小さなホームプロジェクト用の実行可能jarを生成しようとしています。次のようにmavenを使用します:
実行可能JARを作成する方法Mavenを使用して依存関係がある場合
そこに示されているスニペットをpom.xmlに追加し、mvn assembly:assemblyを実行しました。 logmanager / targetに2つの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ファイルが実行されない理由を理解するのを手伝ってもらえますか?また、最初に2つの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:誰かがそれを修正しました、 this の答えは編集前のバージョン)。これは、少なくとも部分的に、トラブルに遭遇した理由を説明しています。
logmanager / targetに、logmanager-0.1.0.jarとlogmanager-0.1.0-jar-with-dependencies.jarの2つのjarファイルを生成します。
最初のものは、package
フェーズ中にjar:jar
によって生成されたlogmanagerモジュールのJARです(モジュールにはjar
タイプのパッケージがあるため)。 2番目は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
をmvn install
フェーズにバインドしているので、もうmvn clean install
を実行する必要はありません。 target
を実行するだけで、標準ビルド中にアセンブリが生成されます。
したがって、pom.xmlを上記の設定で更新し、pom.xml
を実行してください。次に、<=>ディレクトリーにcdして、再試行します。
java -jar logmanager-0.1.0-jar-with-dependencies.jar
エラーが発生した場合は、質問を更新して<=>ファイルの内容と<=>の関連部分(プラグイン構成部分)を投稿してください。次の結果も投稿してください:
java -cp logmanager-0.1.0-jar-with-dependencies.jar com.gorkwobble.logmanager.LogManager
コマンドラインで正常に機能していることを実証するために(Eclipseが何を言っているかに関係なく)。
EDIT: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 build、maven clean、maven generate resource、およびmaven installを指定します。jarファイルが自動的に生成されます。