Java 如何使用 Maven 将资源复制到 src 目标目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23289098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:41:48  来源:igfitidea点击:

How to copy resource to src target directory with Maven?

javamavenresourcescopy

提问by stankoua

I'm currently working on an existing project which has a pom.xml file with the following:

我目前正在处理一个现有项目,该项目有一个 pom.xml 文件,其中包含以下内容:

<resources>
    <resource>
        <filtering>false</filtering>
        <directory>src</directory>
        <includes>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>

I have in the base path a directory called properties which contains properties files. I want to copy when packaging all the properties files contains under properties/ in my src directory (otherwise the program will crash due to missing configuration files).

我在基本路径中有一个名为 properties 的目录,其中包含属性文件。我想在打包我的src目录下properties/下包含的所有属性文件时复制(否则程序会因为缺少配置文件而崩溃)。

So my question is:

所以我的问题是:

How can i, with Maven include resource files that are not located under src directory?

我如何使用 Maven 包含不在 src 目录下的资源文件?

I try this one but it doesn't seem to work:

我尝试了这个,但它似乎不起作用:

<resources>
    <resource>
        <filtering>false</filtering>
        <directory>src</directory>
        <includes>
            <include>**/*.properties</include>
            <include>../properties/**</include>
        </includes>
    </resource>
</resources>

Thank's for your help.

谢谢你的帮助。

采纳答案by zpontikas

If your file structure is like this: Standard Directory Layout

如果你的文件结构是这样的: 标准目录布局

enter image description here

在此处输入图片说明

Then you dont have to add the resources elemt. Maven copies by defaultall the files and folders that are located in your /src/main/resources folder to your build folder and locates them in the root of your compiled classpath files.
if you have for example a file called configuration.propertieslocated in /src/main/resources/configuration.propertiesthen when running mvn clean compilethis file will be copied to your /target/classes/configuration.propertiesSo if you remove that part the files will be located where u want them

然后你不必添加资源元素。默认情况下,Maven 会将位于 /src/main/resources 文件夹中的所有文件和文件夹复制到构建文件夹中,并将它们定位在已编译的类路径文件的根目录中。
例如,如果您有一个名为configuration.properties的文件,/src/main/resources/configuration.properties则在运行时mvn clean compile该文件将被复制到您/target/classes/configuration.properties的文件中。因此,如果您删除该部分,文件将位于您想要的位置

<resource>
    <filtering>false</filtering>
    <directory>src</directory>
    <includes>
        <include>**/*.properties</include>
    </includes>
</resource>

回答by aberes

By migrating of projects from ant to maven without changing project structure set your sourceDirectory testSourceDirectory in the build and use the maven-resource-plugin as folowing take care in wich phase you execute the goals.

通过在不改变项目结构的情况下将项目从 ant 迁移到 maven,在构建中设置您的 sourceDirectory testSourceDirectory 并使用 maven-resource-plugin 作为以下注意执行目标的哪个阶段。

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources01</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes</outputDirectory>
                        <encoding>UTF-8</encoding>
                        <resources>
                            <resource>
                                <directory>${basedir}/src</directory>
                                <includes>
                                    <include>**/*.properties</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
                <execution>
                    <id>copy-resources02</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/build/lib</outputDirectory>
                        <encoding>UTF-8</encoding>
                        <resources>
                            <resource>
                                <directory>${basedir}/target/</directory>
                                <include>*.jar</include>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

回答by 147.3k

To copy everything from source to destination, I used following

要将所有内容从源复制到目标,我使用了以下内容

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes/static/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/gui/build/</directory>
                                <includes>
                                    <include>**/*.*</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>