我最近做了一个稍微不同的the question版本。我在Netbeans 7.3下有一个Maven项目,它没有任何用于配置构建选项的build.xml
文件,而有我用来导入其他库的pom.xml
。现在,我在Netbeans 7.3的项目文件夹中存储了一个文本文件(假设是textfile.txt
),例如
project folder
textfile.txt
src
package
package.subpackage
MyClass.java
当我编译时,我得到了一个target
文件夹,jar文件放在这个文件夹中,例如
project folder
textfile.txt
target
classes
generated-sources
....etc
test-classes
MyProject.jar
src
package
package.subpackage
MyClass.java
如何在编译Maven项目时将文件textfile.txt复制到目标文件夹下?
发布于 2013-05-04 22:48:51
第一种方法是将文件放入src/main/resources
,这是专门用于存储编译后的资源的文件夹,即包含在jar文件中的资源(例如,图标的图像)。
如果您需要使配置文件与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,因此您还可以通过使用专用插件将所有资源从“输入”目录导入到目标中的“输出”目录,例如:
<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>
发布于 2015-06-02 21:12:15
最简单的方法是使用一些我所知道的资源(有关资源配置的其他信息可以在这里找到: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>
发布于 2018-07-15 07:48:25
在构建我的设置文件和更新生产环境时,这对我很有效:
<build>
<resources>
<resource>
<directory>${project.basedir}</directory><!-- from -->
<targetPath>${project.build.directory}</targetPath><!-- to -->
<includes><!-- what -->
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
https://stackoverflow.com/questions/16372374
复制相似问题