23

スコープを提供したものを除いて、Maven アセンブリ プラグインを使用して依存関係のある jar を構築しようとしています。

jar-with-dependencies を assembly.xml ファイルにコピーし、pom での使用を構成しました。ここに参照用があります:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
  <id>injectable-jar</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
    </fileSet>
  </fileSets>
</assembly>

スコープを に設定するとprovided、不要なものを正確に含む jar を作成できることがわかりましたが、その逆の動作を取得する方法がわかりません

4

4 に答える 4

22

これは少し不格好ですが、maven-dependency-plugin を使用してすべての依存関係をプロジェクトにコピー/アンパックし、アセンブリ プラグインを使用してパッケージ化を行うことができます。

copy-dependenciesとゴールのunpack-dependencies両方に、依存関係を省略するために設定できるオプションのexcludeScopeプロパティがあります。provided以下の構成は、すべての依存関係を target/lib にコピーします。アセンブリ プラグイン記述子を変更して、fileSetを使用してこれらの jar を含めることができます。

更新:これをテストして、動作することを確認しました。アセンブリ プラグインをパッケージ フェーズにバインドするための構成と、アセンブリ記述子への関連する変更を追加しました。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <excludeScope>provided</excludeScope>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-4</version>
  <executions>
    <execution>
      <id>jar-with-deps</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/my-assembly.xml</descriptor>
    </descriptors>
  </configuration>
</plugin>

記述子の fileSet セクションは次のmy-assemblyようになります。

<assembly>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/lib</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.*</include>
      </includes>
    </fileSet>
  </fileSets>
...

</assembly>
于 2009-09-22T11:53:13.077 に答える
4

理論的には、タグ「ignoreNonCompile」と「excludeScope」が役立つはずですが、必ずしも適切に機能するとは限らないことに注意してください。

maven3 と maven-dependency-plugin 2.4 では、1 つの解決策は次のとおりです。

<configuration>
<excludeArtifactIds>junit,mockito-all</excludeArtifactIds>
<excludeTransitive>true</excludeTransitive>
</configuration>
于 2012-07-31T20:44:27.053 に答える
0

最新の Maven (私は Maven 3.0 でテストしていました) では、これは期待どおりに動作するように見えますが、いくつかの注意事項があります。

要求されたスコープ (dependencySet 内) には、次の定義に基づく追加のスコープが含まれる場合があります

したがって、コンパイル スコープを要求すると、コンパイルと提供の両方が取得されます。ただし、ランタイム スコープを要求する場合は、コンパイルとランタイムを取得する必要があります (ただし、提供されません)。

于 2011-02-04T00:06:24.550 に答える
0

これは古い投稿ですが、maven-dependency-plugin には、「provided」または必要なスコープに設定できる「excludeScope」オプションが追加されました。

http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#excludeScope

例えば、

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <excludeScope>provided</excludeScope>
            </configuration>
        </execution>
    </executions>
</plugin>
于 2016-10-21T12:41:53.107 に答える