首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >编译Maven项目时将文本文件移动到目标文件夹中

编译Maven项目时将文本文件移动到目标文件夹中
EN

Stack Overflow用户
提问于 2013-05-04 16:44:23
回答 4查看 86.8K关注 0票数 38

我最近做了一个稍微不同的the question版本。我在Netbeans 7.3下有一个Maven项目,它没有任何用于配置构建选项的build.xml文件,而有我用来导入其他库的pom.xml。现在,我在Netbeans 7.3的项目文件夹中存储了一个文本文件(假设是textfile.txt),例如

代码语言:javascript
复制
project folder
  textfile.txt
  src
    package
    package.subpackage
      MyClass.java

当我编译时,我得到了一个target文件夹,jar文件放在这个文件夹中,例如

代码语言:javascript
复制
project folder
  textfile.txt
  target
    classes
    generated-sources
    ....etc
    test-classes
    MyProject.jar
  src
    package
    package.subpackage
      MyClass.java

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-05-04 22:48:51

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

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

代码语言:javascript
复制
<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>

此外,由于您可以读取here,因此您还可以通过使用专用插件将所有资源从“输入”目录导入到目标中的“输出”目录,例如:

代码语言:javascript
复制
<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>
票数 50
EN

Stack Overflow用户

发布于 2015-06-02 21:12:15

最简单的方法是使用一些我所知道的资源(有关资源配置的其他信息可以在这里找到:https://maven.apache.org/plugins/maven-resources-plugin/):

代码语言:javascript
复制
<build>
  <plugins>
    <!-- your plugins, including or not maven-resource-plugin -->
  </plugins>
  <resources>
    <resource>
      <filtering>true</filtering><!-- if it is neccessary -->
      <directory>${project.basedir}</directory><!-- from -->
      <targetPath>${project.build.directory}</targetPath><!-- to -->
      <includes><!-- what -->
        <include>textfile.txt</include>
      </includes>
    </resource>
  </resources>
</build>
票数 34
EN

Stack Overflow用户

发布于 2018-07-15 07:48:25

在构建我的设置文件和更新生产环境时,这对我很有效:

代码语言:javascript
复制
<build>
<resources>  
   <resource>
       <directory>${project.basedir}</directory><!-- from -->
       <targetPath>${project.build.directory}</targetPath><!-- to -->
       <includes><!-- what -->
          <include>**/*.properties</include>
       </includes>
    </resource> 
</resources>
</build>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16372374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档