37

Maven の親プロジェクトのモジュール間でリソースを共有する方法はありますか? たとえば、マルチモジュール Maven プロジェクトのすべてのモジュールに対して 1 つの log4j.properties ファイルを指定したいと考えています。

通常、私はEclipse IDEを使用して、一般的なプロジェクトを選択して親プロジェクトを作成し、pom. srcこれにより、フォルダなどのない「クリーンな」プロジェクト構造が作成されます。この場合、そのような共有リソースをどこに置くべきか疑問に思います。

EDIT1 : 共通リソースを親プロジェクトに配置したいと思います。

4

6 に答える 6

34

「jar」をパッケージ化する追加の「ベース」モジュール (プロジェクト) を 1 つ作成し、src/main/resources. 次に、他のモジュールをそのプロジェクトに依存させます。これで、クラスパスに共通のリソースが表示されます。

于 2013-07-05T15:49:13.650 に答える
2

別の方法として、プロジェクトのルート pom に次のように入力します。

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <!-- don't propagate to child poms -->
            <!-- this will only execute in root pom -->
            <inherited>false</inherited>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <!-- don't add classifier -->
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    <plugins>

そして、assembly.xml の例

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>resources</id>

    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>${project.basedir}/resources/</directory>
            <outputDirectory/>
            <useDefaultExcludes>true</useDefaultExcludes>
            <includes>
                <include>**</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

アセンブリ プラグインはアーティファクトを生成し、それを現在のリアクターにアタッチするため、インストールおよびデプロイされます。

いいえ、同じ pom で標準の依存関係イベントとして使用できます。

重要なのは、生成されたアーティファクトを使用する別のプラグインの前に、アセンブリ (適切なフェーズ) をトリガーすることです。

例えば。ルート pom に含めることができます。次の構成がすべてのモジュールに伝播されます。

              <plugin>
                <artifactId>some-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>goal</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>your.project.groupid</groupId>
                        <artifactI>your.project.artifactId</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

このメソッドはプロジェクトで確認できます: https://github.com/s4u/pgp-keys-map resourcesディレクトリはすべてのモジュールで共有されています。

于 2020-03-07T23:06:39.983 に答える
1

はい、それは可能な解決策のようです。しかし、親プロジェクトはすべての共通の依存関係と子モジュールのMaven構成を指定するため、親プロジェクトでこれらのリソースを(追加のモジュールを導入せずに)指定できるかどうかに興味がありました。親プロジェクトが最も適した場所だと思います共通リソースにも。

パッケージ タイプpomの場合、共有リソースを管理するためにゴールパッケージが指定されている場合、 pom ファイルのビルドセクションに次の (チェック フォルダー) を追加するだけです。

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
       <executions>
           <execution>
               <id>copy-config-files</id>
               <phase>package</phase>
               <goals>
                   <goal>copy-resources</goal>
               </goals>
               <configuration>
                   <outputDirectory>${project.build.directory}/logconfig</outputDirectory>
                   <resources>
                       <resource>
                        <filtering>false</filtering>
                        <directory>${project.basedir}/src/main/resources</directory>
                       </resource>
                   </resources>
               </configuration>
        </execution>
       </executions>
    </plugin>
于 2016-11-18T10:15:22.460 に答える