编译Maven项目时将文本文件移动到目标文件夹中 [英] Move a text file into target folder when compiling a Maven project

查看:737
本文介绍了编译Maven项目时将文本文件移动到目标文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对问题的版本稍有不同我最近做的. 我在 Netbeans 7.3 下有一个 Maven 项目,该项目没有任何build.xml文件来配置构建选项,而我可以使用pom.xml来导入其他图书馆.现在,我有一个文本文件(假设为textfile.txt)存储在 Netbeans 7.3 的项目文件夹中,例如

I have a slight different version of the question that I made recently. I have a Maven project under Netbeans 7.3, which doesn't have any build.xml file to configure building options, while there is the pom.xml that I use to import other libraries. Now, I have a text file (let's say textfile.txt) stored in the project folder in Netbeans 7.3, e.g.

project folder
  textfile.txt
  src
    package
    package.subpackage
      MyClass.java

编译时,我得到一个target文件夹,其中放置了 jar 文件,例如

When I compile I get a target folder where the jar file is put in, e.g.

project folder
  textfile.txt
  target
    classes
    generated-sources
    ....etc
    test-classes
    MyProject.jar
  src
    package
    package.subpackage
      MyClass.java

在编译Maven项目时,如何使textfile.txt文件复制到目标文件夹下?

How can I make the file textfile.txt being copied under target folder when I compile the Maven project?

推荐答案

第一种方法是将文件放入src/main/resources,这是用于存储编译资源的文件夹,即jar文件中包含的资源(例如,图标的图片.

A first way is to put the files into src/main/resources that is the folder devoted to store the complied resources, i.e. the resources included into the jar file (e.g. images for the icons).

如果需要使配置文件与jar一起分发,但要与jar分开,则必须编辑pom.xml文件.可能的答案是在<plugins></plugins>标签之间添加以下插件.

If you need to make the config file to be distributed with the jar, but separated by it, you must edit the pom.xml file. A possible answer is the to add the following plugin between the <plugins> and </plugins> tags.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>Using env.test.properties</echo>
                    <copy file="textfile.txt" tofile="${basedir}/target/textfile.txt"/>
                    </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>

此外,您可以在此处您还可以使用专用插件将所有资源从输入"目录导入到目标内部的输出"目录,例如:

Moreover, as you can read here you can also import all the resources from an "input" directory to an "output" directory inside target by using the dedicated plugin, e.g.:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.1</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/output</outputDirectory>
               <resources>          
                    <resource>
                        <directory>input</directory>
                        <filtering>true</filtering>
                    </resource>
               </resources>              
            </configuration>            
        </execution>
     </executions>
</plugin>

这篇关于编译Maven项目时将文本文件移动到目标文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆