如何将文件从父 POM 复制到 Maven 项目中

How to copy file from Parent POM into Maven Project

我想要一个包含两个文件的 Parent Maven 项目。

src/license/LICENSE.txt src/license/NOTICE.txt

当我构建我的主项目时,将其作为父 POM 引用,我希望这些文件包含在生成的 JAR 中。

我知道我可以使用...

<build>
    <resources>
        <resource>
            <targetPath>META-INF</targetPath>
            <directory>src/license</directory>
            <includes>
                <include>NOTICE.txt</include>
                <include>LICENSE.txt</include>
            </includes>
        </resource>
    </resources>    
</build>

但这只适用于文件位于主项目中的情况,而不是位于父项目中的情况。

我找到了解决方法。

首先,创建一个新的存储库来保存您要复制的项目。

它的 POM 应该包含这个(GAV 详细信息是 com.jeff:base-pom-license)...

<build>
    <plugins>
        <plugin>
            <artifactId>maven-remote-resources-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>bundle</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>**/*.txt</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

然后在你的 Base pom 添加这个...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-remote-resources-plugin</artifactId>
            <version>1.5</version>
            <configuration>
                <resourceBundles>
                    <resourceBundle>com.jeff:base-pom-license:${project.version}</resourceBundle>
                </resourceBundles>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>       

这将导致您在新 Maven 存储库中的文件包含在所有扩展基本 pom 的地方。