3

私は、最終的に実行可能なjarファイルを作成するJava Mavenプロジェクトに取り組んできました。最初は問題はありませんでしたが、依存関係も jar にコピーすることにしました。

次の (非常に役立つ) スタック オーバーフローの質問を見つけ、回答にある指示に従いました (自分のメイン クラスとターゲット バージョンを置き換えます): Maven で実行可能 jar をビルドする際の問題

これはうまく機能しましたが、2 つの jar ファイル (ldap-daemon-0.0.1-SNAPSHOT.jar と ldap-daemon-0.0.1-SNAPSHOT-jar-with-dependencies.jar) ができてしまいました。私はこれで問題ありませんが、私が知る限り、後で maven-dependency-plugin のコピー機能を使用して依存関係のある jar のコピーを実際に取得することはできません。

したがって、私が知りたいのは、次のいずれかを達成する方法です。

  • メインのビルド アーティファクトである ldap-daemon-0.0.1-SNAPSHOT.jar に依存関係を含めます。
  • maven-dependency-plugin を使用して、2 番目のビルド アーティファクト (ldap-daemon-0.0.1-SNAPSHOT-jar-with-dependencies.jar) をコピーします。

ldap-daemon のプラグイン構成は次のとおりです (パッケージ構成は「jar」です)。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>com.acuitus.ldapd.LDAPDaemonImp</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>6</source>
        <target>6</target>
    </configuration>
</plugin>

そして、結果のjarをダウンストリームプロジェクトのフォルダーにコピーしようとする私のプラグイン構成は次のとおりです。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.7</version>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.acuitus</groupId>
                        <artifactId>ldap-daemon</artifactId>
                        <version>0.0.1-SNAPSHOT</version>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>${project.build.directory}/classes/www-export</outputDirectory>
                        <destFileName>ldap-daemon.jar</destFileName>
                    </artifactItem>
                </artifactItems>
                    <outputDirectory>${project.build.directory}/wars</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
        </execution>
    </executions>
</plugin>

どんな援助も大歓迎です。御時間ありがとうございます!

4

1 に答える 1

4

ご存知のように、アセンブリ プラグインは通常の jar ファイルとすべての依存関係を含む 2 つの jar ファイルを生成します。Maven は、jdk1.6 や jdk1.7 など、同じ pom からビルドされたアーティファクトに分類子コンストラクトを使用しますが、内容が異なります。または、より一般的な例は、maven のソース コード jar ファイルです。このコンストラクトは、アセンブリ プラグインでも使用されます。コピー構成は次のようになります。

<artifactItem>
    <groupId>com.acuitus</groupId>
    <artifactId>ldap-daemon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>jar</type>
    <overWrite>true</overWrite>
    <outputDirectory>${project.build.directory}/classes/www-export</outputDirectory>
    <destFileName>ldap-daemon.jar</destFileName>
 </artifactItem>

したがって、依存関係のない通常の jar ファイルをコピーするように maven に指示します。

ただし、必要な jar ファイルは ldap-daemon-0.0.1-SNAPSHOT-jar-with-dependencies.jar です。したがって、maven が正しい jar ファイルをフェッチできるように、分類子を指定する必要があります。

<artifactItem>
    <groupId>com.acuitus</groupId>
    <artifactId>ldap-daemon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>jar</type>
    <classifier>jar-with-dependencies</classifier>
    <overWrite>true</overWrite>
    <outputDirectory>${project.build.directory}/classes/www-export</outputDirectory>
    <destFileName>ldap-daemon.jar</destFileName>
 </artifactItem>

生成された jar ファイルと使用される分類子をより詳細に制御する必要がある場合は、 maven-shade-pluginも参照することをお勧めします。

于 2013-05-08T21:51:49.400 に答える