Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 942
mat_boy Avatar asked May 04 '13 08:05

mat_boy


People also ask

What is target file in maven project?

The target folder is the maven default output folder. When a project is build or packaged, all the content of the sources, resources and web files will be put inside of it, it will be used for construct the artifacts and for run tests. You can delete all the target folder content with mvn clean command.

Where is target folder in maven?

The maven targets are located in your project config/ folder.

What is the maven command to compile all files including the text files?

mvn compiler:compile This command compiles the java source classes of the maven project.

What is the way to compile the maven application?

To build a Maven project via the command line, you use the mvn command from the command line. The command must be executed in the directory which contains the relevant pom file. You pass the build life cycle, phase or goal as parameter to this command.


2 Answers

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> 
like image 132
JeanValjean Avatar answered Oct 19 '22 23:10

JeanValjean


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> 
like image 42
Pavel Uvarov Avatar answered Oct 20 '22 00:10

Pavel Uvarov