简体   繁体   English

如何在Maven插件中进行文件复制?

[英]How to do file copy in maven plugin?

Now I'm writing a maven dependency A, and using this dependency in project B. The file structures of both are: 现在,我正在编写一个Maven依赖项A,并在项目B中使用此依赖项。两者的文件结构为:

Dependency A: 依赖关系A:

在此处输入图片说明

Project B: 项目B:

在此处输入图片说明

What I'm trying to do is when I run mvn spring-boot:run in Project B, the test.txt in A's src/main/resources/files will be copied into /WebContent in Project B. 我想做的是在项目B中运行mvn spring-boot:run时,将A的src / main / resources / files中test.txt复制到项目B中的/ WebContent中。

What I have tried is writing following code in Dependency A: 我试过的是在Dependency A中编写以下代码:

        final File test = new File("files/test.txt");
        final File dest = new File("WebContent");
        try {
            FileUtils.copyFileToDirectory(test, dest);
        } catch (final IOException e) {
            e.printStackTrace();
        }

But When I do mvn spring-boot:run it said the file doesn't exist. 但是当我执行mvn spring-boot:run时,它说该文件不存在。

Though I wouldn't suggest copying from one project to another. 尽管我不建议从一个项目复制到另一个项目。 Resources even if are same in two projects should be present in each as their own resource to keep it intact. 即使两个项目中的资源相同,每个资源也应作为自己的资源存在,以保持其完整性。 Also, considering that you are looking for a solution with maven to do so. 另外,考虑到您正在寻找使用maven的解决方案。

Multi-module maven projects can have resources bundled at a common place using the remote resource plugin - 多模块Maven项目可以使用远程资源插件在公共位置捆绑资源-

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

Source - Maven Remote Resources 来源 -Maven远程资源


To copy a resource within a module or to a target directory which are not a part of default layout or in build/resources, you can also use Maven Resources Plugin as 要将资源复制到默认布局或构建/资源中不包含的模块内或目标目录中,您还可以将Maven资源插件用作

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <!-- here the phase you need -->
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
          <resources>          
            <resource>
              <directory>src/non-packaged-resources</directory>
              <filtering>true</filtering>
            </resource>
          </resources>              
        </configuration>            
      </execution>
    </executions>
</plugin>

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

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