Move a text file into target folder when compiling a Maven project

ぐ巨炮叔叔 提交于 2019-11-28 20:18:05
JeanValjean

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

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>

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

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!