Maven 资源不复制文件

发布于 2024-12-24 16:33:29 字数 1898 浏览 3 评论 0原文

我想使用 maven 替换 war 文件中的一些 *.properties 文件。

因此,我在资源文件夹中创建了文件夹 dev、test 和 prod。现在我只想在执行相应的配置文件时在我的 war 文件的资源文件夹路径中使用这些文件夹之一。结果是 maven 也复制了所有其他文件夹,因此这些文件夹在类路径中是双倍的。这是我的配置:

<profile>
        <id>dev</id>

        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <id>copy-dev-resources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>

                                <overwrite>true</overwrite>

                                <outputDirectory>${basedir}/target/classes</outputDirectory>
                                <resources>
                                    <resource>
                                        <!-- source -->
                                        <directory>src/main/resources/dev</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

现在如何配置 Maven,它只将文件夹 src/main/resources/dev 的内容复制到 target/classes?

谢谢你的帮助

I want to use maven for replacing some of my *.properties files in my war file.

Therefore I created folders dev, test and prod in my resources folder. Now I only want to have one of these folders used in the resources folder path in my war file when executing the corresponding profile. The result is that maven is copying all other folders as well so those are double in the classpath. Here is my configuration:

<profile>
        <id>dev</id>

        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <id>copy-dev-resources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>

                                <overwrite>true</overwrite>

                                <outputDirectory>${basedir}/target/classes</outputDirectory>
                                <resources>
                                    <resource>
                                        <!-- source -->
                                        <directory>src/main/resources/dev</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

How do I configure Maven now that its only copying the contents of folder src/main/resources/dev to target/classes?

Thank you for you help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灰色世界里的红玫瑰 2024-12-31 16:33:29

默认情况下,Maven 会将“src/main/resources”中的所有文件复制到输出文件夹,因此在您当前的配置中,它可能会创建“dev”、“test”和“prod”文件夹及其内容,然后另外复制开发资源没有“dev”前缀。

我建议保留默认资源文件夹,仅将其用于与配置文件无关的资源。然后,您可以在您的配置文件中配置其他资源文件夹:

<profile>
    <id>dev</id>
    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources-dev</directory>
            </resource>
        </resources>
    </build>
</profile>

还应该可以使用配置文件中定义的属性在公共构建部分中进行配置:

<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources-${projectStage}</directory>
        </resource>
    </resources>
</build>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <projectStage>dev</projectStage>
        </properties>
    </profile>
</profiles>

如果您由于某种原因无法更改当前的目录结构,那么您必须配置默认资源文件夹的排除项不复制嵌套的配置文件相关文件夹:

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>dev/**</exclude>
                    <exclude>test/**</exclude>
                    <exclude>prod/**</exclude>
                </excludes>
            </resource>
        </resources>
    </build>

Maven by default copies all files in "src/main/resources" to the output folder, so in your current configuration it would probably create "dev", "test" and "prod" folders with their contents and then additionally copy the development resources without a "dev" prefix.

I would suggest to leave the default resources folder as it is and use it only for profile-independent resources. In your profile you can then configure additional resource folders:

<profile>
    <id>dev</id>
    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources-dev</directory>
            </resource>
        </resources>
    </build>
</profile>

It should also be possible to configure this in the common build section using properties defined in the profiles:

<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources-${projectStage}</directory>
        </resource>
    </resources>
</build>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <projectStage>dev</projectStage>
        </properties>
    </profile>
</profiles>

If you for some reason can't change your current directory structure then you would have to configure exclusions for the default resource folder to not copy the nested profile-dependant folders:

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>dev/**</exclude>
                    <exclude>test/**</exclude>
                    <exclude>prod/**</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
夜夜流光相皎洁 2024-12-31 16:33:29

maven 目录结构中的“main”部分就是为了这个目的而存在的。
您应该在 src 目录中创建更多目录,而不是在资源目录中创建目录。
这是默认行为,并且已经通过在 Maven 生命周期的测试阶段让测试资源覆盖主要资源来实现。

您的结构应如下所示:

src
  main
    resources
      a.properties
      b.properties
      c.properties
  test
    resources
      b.properties
      c.properties
  dev
    resources
      b.properties

使用此结构,您的“主”目标将具有默认属性文件,测试阶段将暂时覆盖 b 和 c,开发配置文件将仅在最终版本中用另一个自定义版本覆盖 b可部署。

为了实现这一目标,您必须按照您在我们的问题中所做的方式配置maven-resources-plugin,以获取开发配置文件。

当您这样做时,还要考虑:在使用开发配置文件构建时运行单元测试时应该使用什么版本的属性文件?

The "main" part in the maven directory structure exists for this purpose.
Instead of creating directories inside the resource dir, you should create more dirs in your src directory.
This is the default behavior, and it's already implemented by letting test resources override main resources during the test phases of the maven lifecycle.

Your structure should look like this:

src
  main
    resources
      a.properties
      b.properties
      c.properties
  test
    resources
      b.properties
      c.properties
  dev
    resources
      b.properties

With this structure, your "main" target will have the default property files, the test phases would temporarily overwrite b and c, and the dev profile will only overwrite b with yet another custom version, in the final deployable.

In order to achieve this, you'd have to configure the maven-resources-plugin the way you already do in our question, for a dev profile.

And when you do, also consider: what version of property files should you use when running unit tests while building using the dev profile?

娜些时光,永不杰束 2024-12-31 16:33:29

我在maven中使用 assembly.xml 文件并使用这组代码来确保我的资源文件位于应用程序的根目录...

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

<id>distribution</id>
<formats>
    <format>tar.gz</format>
</formats>
<fileSets>
    <fileSet>
        <directory>${basedir}</directory>
        <includes>
            <include>bin/**</include>
            <include>webapps/**</include>
            <include>data/**</include>
        </includes>
    </fileSet>
    <fileSet>
        <directory>${basedir}/target</directory>
        <includes>
            <include>*.jar</include>
        </includes>
        <outputDirectory>lib</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>${basedir}/src/main/</directory>
        <includes>
            <include>resources/**</include>
        </includes>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
        <useProjectArtifact>false</useProjectArtifact>
    </dependencySet>
</dependencySets>

    </assembly>

不知道这是否是最好的方法,但这是我的一个方法可以想象。

I was using the assembly.xml file in maven and used this set of code to make sure that my resources file was at the root directory of the app...

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

<id>distribution</id>
<formats>
    <format>tar.gz</format>
</formats>
<fileSets>
    <fileSet>
        <directory>${basedir}</directory>
        <includes>
            <include>bin/**</include>
            <include>webapps/**</include>
            <include>data/**</include>
        </includes>
    </fileSet>
    <fileSet>
        <directory>${basedir}/target</directory>
        <includes>
            <include>*.jar</include>
        </includes>
        <outputDirectory>lib</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>${basedir}/src/main/</directory>
        <includes>
            <include>resources/**</include>
        </includes>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
        <useProjectArtifact>false</useProjectArtifact>
    </dependencySet>
</dependencySets>

    </assembly>

Don't know if this is the best way, but it was one I could visualize.

溺渁∝ 2024-12-31 16:33:29

添加到前面的答案中,您还可以使用 maven copy-resources 将文件从自定义路径复制到特定目录。(虽然不建议,但理想情况下

<configuration>
<!-- Destination target path -->
<outputDirectory>${basedir}/target/classes/com/db/abc/util/</outputDirectory>
<resources>
    <resource>
        <!-- source file path -->
        <directory>C:/Users/abc/Work/filepath</directory>
    </resource>
</resources>
</configuration>

Adding to the earlier answers, you can also copy file from custom path to specific directory with maven copy-resources.(though its not suggested, ideally the)

<configuration>
<!-- Destination target path -->
<outputDirectory>${basedir}/target/classes/com/db/abc/util/</outputDirectory>
<resources>
    <resource>
        <!-- source file path -->
        <directory>C:/Users/abc/Work/filepath</directory>
    </resource>
</resources>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文