39

我最近提出的问题的版本略有不同。我在Netbeans 7.3下有一个Maven项目,它没有任何文件来配置构建选项,而我用它来导入其他库。现在,我有一个文本文件(比方说)存储在Netbeans 7.3的项目文件夹中,例如build.xmlpom.xmltextfile.txt

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

当我编译时,我得到一个放置jartarget文件的文件夹,例如

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

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

4

4 回答 4

51

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

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

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

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

<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>
于 2013-05-04T14:48:50.767 回答
35

最简单的方法是使用我所知道的一些资源(有关资源配置的其他信息,您可以在此处找到: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>
于 2015-06-02T13:12:15.367 回答
7

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

<build>
<resources>  
   <resource>
       <directory>${project.basedir}</directory><!-- from -->
       <targetPath>${project.build.directory}</targetPath><!-- to -->
       <includes><!-- what -->
          <include>**/*.properties</include>
       </includes>
    </resource> 
</resources>
</build>
于 2018-07-14T23:48:24.820 回答
5

要绝对控制构建的输出,您可以使用“Apache Maven Assembly Plugin”。您可以定义几乎所有内容(格式、子文件夹...)。

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

更多信息

您必须在 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>
于 2015-09-01T09:52:15.240 回答