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?
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.
The maven targets are located in your project config/ folder.
mvn compiler:compile This command compiles the java source classes of the maven project.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With