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

时间:2013-05-04 08:44:23

标签: maven build-automation netbeans-7

我最近制作的the question版略有不同。 我在 Netbeans 7.3 下有一个 Maven 项目,该项目没有任何build.xml文件来配置构建选项,而pom.xmltextfile.txt个我用来导入其他库。现在,我有一个文本文件(假设project folder textfile.txt src package package.subpackage MyClass.java )存储在 Netbeans 7.3 的项目文件夹中,例如

target

当我编译时,我得到一个project folder textfile.txt target classes generated-sources ....etc test-classes MyProject.jar src package package.subpackage MyClass.java 文件夹,其中放入 jar 文件,例如

{{1}}

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

4 个答案:

答案 0 :(得分:37)

第一种方法是将文件放入src/main/resources,该文件夹用于存储已编译的资源,即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>

此外,正如您可以阅读here,您还可以使用专用插件将所有资源从“input”目录导入到目标内的“output”目录,例如:

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

答案 1 :(得分:22)

最简单的方法,就是我所知道的使用某些资源(有关资源配置的更多信息,请点击此处: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>

答案 2 :(得分:5)

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

  

Maven的Assembly插件主要是为了允许用户将项目输出及其依赖项,模块,站点文档和其他文件聚合到一个可分发的存档中。

doc

您必须在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>

在你的情况下,描述符&#34; yourassembly.xml&#34;如下:

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

答案 3 :(得分:2)

对于构建我的设置文件和更新生产,这对我来说很有效:

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