Move a text file into target folder when compiling

2020-01-30 12:37发布

问题:

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

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

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

回答1:

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

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>


回答2:

The simplest way, to use some resource as I know (additional information about resources configuration you can find here: 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>


回答3:

To have an absolute control of the output of your build you can use the "Apache Maven Assembly Plugin". You can define pretty much everything (format, subfolders...).

The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.

More info

You have to install the plugin in your pom file:

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

In your case the descriptor "yourassembly.xml" would be as follows:

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


回答4:

For building my setting files and updating production this worked for me:

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