在maven中将文件从一个项目复制到另一个项目

Gan*_*row 17 java maven-2 maven

我正在研究一个多模块项目.我们在少数其他模块中使用来自一个模块的appCtx.xml.

目前的问题是它们并不总是彼此同步.

当有人修改文件并且项目构建时,会发生这种情况,执行此操作的人可能会忘记复制到另一个模块,这会导致问题.

如何将src/main/resources中的appCtx.xml从项目A复制到项目B中的src/main/resources?

Mat*_*ell 44

您可以使用maven资源插件执行此操作:copy-resources,类似于:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-appCtx</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/src/blahhere</outputDirectory>
                <overwrite>true</overwrite>
                <resources>
                    <resource>
                        <directory>../other_project/src/blah/blah</directory>
                        <includes>
                            <include>appCtx.xml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

这将复制来自一个项目(位于同一源树上)的文件,作为generate-resources阶段的一部分.您可以根据自己的需要进行调整.

如果项目不是一次性构建,那么从一个项目复制到另一个项目可能会导致不稳定的构建,但上述内容将适用于始终构建在一起的项目.