【问题标题】:Move a text file into target folder when compiling a Maven project编译 Maven 项目时将文本文件移动到目标文件夹
【发布时间】:2013-04-28 15:38:35
【问题描述】:

我最近制作的the question 版本略有不同。 我在 Netbeans 7.3 下有一个 Maven 项目,它没有任何 build.xml 文件来配置构建选项,而我使用 pom.xml 来导入其他图书馆。现在,我有一个文本文件(比如说textfile.txt)存储在 Netbeans 7.3 的项目文件夹中,例如

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

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

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

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

【问题讨论】:

    标签: maven build-automation netbeans-7


    【解决方案1】:

    为了构建我的设置文件和更新生产,这对我有用:

    <build>
    <resources>  
       <resource>
           <directory>${project.basedir}</directory><!-- from -->
           <targetPath>${project.build.directory}</targetPath><!-- to -->
           <includes><!-- what -->
              <include>**/*.properties</include>
           </includes>
        </resource> 
    </resources>
    </build>
    

    【讨论】:

    • 只有当你明确调用mvn resources:resources时才有效,对吧?
    • @djangofan 不,它一直有效。它是 POM 构建描述符的一部分,而不是插件。但是,它会更改来自此 POM 的任何构建的默认 Maven 行为。
    【解决方案2】:

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

    <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>
    

    【讨论】:

    • 如何确保 textfile.txt 也被复制到 jar 文件中?
    • 我明白了。谢谢。
    • 这太简单了。谢谢!
    • 这应该是接受的答案,真的是最简单的方法。
    • 我可以设置目标列表吗?就我而言,我需要复制到多个目标。
    【解决方案3】:

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

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

    <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,您还可以使用专用插件将所有资源从“输入”目录导入目标内部的“输出”目录,例如:

    <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>
    

    【讨论】:

    • 谢谢!我不确定第一个插件是否正确使用,但工作正常。
    • 太棒了,这很有帮助,谢谢JeanValjean!
    【解决方案4】:

    要完全控制构建的输出,您可以使用“Apache Maven 程序集插件”。您可以定义几乎所有内容(格式、子文件夹...)。

    用于 Maven 的程序集插件主要用于允许用户将项目输出及其依赖项、模块、站点文档和其他文件聚合到一个可分发的存档中。

    More info

    你必须在你的 pom 文件中安装插件:

        <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/yourassembly.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>
    </plugins>
    
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>x.x</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/yourassembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- append to the packaging phase. -->
                        <goals>
                            <goal>single</goal> <!-- goals == mojos -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
    

    在您的情况下,描述符“yourassembly.xml”如下:

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
      <id>yourassembly</id>
      <formats>
        <format>tar.gz</format>
        <format>dir</format>
      </formats>
      <fileSets>
    
        <fileSet>
          <directory>${project.basedir}</directory>
          <includes>
            <include>README*</include>
            <include>LICENSE*</include>
            <include>NOTICE*</include>
          </includes>
          <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    
        <fileSet>
          <directory>${project.build.directory}</directory>
          <outputDirectory>/</outputDirectory>
          <includes>
            <include>*.jar</include>
          </includes>
        </fileSet>
    
        <fileSet>
          <directory>${basedir}</directory>
          <outputDirectory>/</outputDirectory>
          <includes>
            <include>textfile.txt</include>
          </includes>
        </fileSet>
    
        <dependencySets>
            <dependencySet>
                <outputDirectory>lib</outputDirectory>
                <unpack>false</unpack>
            </dependencySet>
        </dependencySets>
    
      </fileSets>
    
    </assembly>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多