gpt4 book ai didi

maven - 编译Maven项目时将文本文件移动到目标文件夹中

转载 作者:行者123 更新时间:2023-12-03 09:15:23 25 4
gpt4 key购买 nike

我的 the question 版本略有不同我最近做的。
我有一个 Maven 项目下 Netbeans 7.3 ,它没有任何 build.xml文件来配置构建选项,而有 pom.xml我用来导入其他库。现在,我有一个文本文件(比如 textfile.txt )存储在 Netbeans 7.3 的项目文件夹中,例如

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文件复制到目标文件夹下?

最佳答案

第一种方法是将文件放入 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>

关于maven - 编译Maven项目时将文本文件移动到目标文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com