简体   繁体   English

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

[英]Copying file from one project to another in maven

I'm working on a multi-module project.我正在从事一个多模块项目。 We're using the appCtx.xml from one module in few other modules.我们在其他几个模块中使用一个模块中的 appCtx.xml。

Current issue is that they're not always in sync with each other.当前的问题是它们并不总是彼此同步。

It happens when someone modifies the file and the project builds, the person doing that can forget to copy to another module and it causes issues.当有人修改文件并构建项目时,就会发生这种情况,这样做的人可能会忘记复制到另一个模块,这会导致问题。

How do I copy appCtx.xml inside src/main/resources from project A to src/main/resources in project B?如何将项目 A 的 src/main/resources 中的 appCtx.xml 复制到项目 B 的 src/main/resources 中?

You can do this with the maven resources plugin: copy-resources , something like: 您可以使用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>

This copies a file from one project (colocated on the same source tree) as part of the generate-resources phase. 这将复制来自一个项目(位于同一源树上)的文件,作为generate-resources阶段的一部分。 You can adapt this to your needs. 您可以根据自己的需要进行调整。

This copying from one project to another may cause unstable builds if the projects aren't all built at once, but the above will work for projects which are always built together. 如果项目不是一次性构建,那么从一个项目复制到另一个项目可能会导致不稳定的构建,但上述内容将适用于始终构建在一起的项目。

Another, perhaps more updated way, is to use maven-dependency-plugin with unpack goal and fileMappers to rename the destination of the unpacked files.另一种可能更更新的方法是使用带有unpack目标maven-dependency-pluginfileMappers来重命名解包文件的目标。

This official documentation with an example helped me a lot: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-filemapper.html这个带有示例的官方文档对我帮助很大: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-filemapper.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM