Maven资源未复制文件

问题描述 投票:7回答:4

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

因此,我在资源文件夹中创建了文件夹dev,test和prod。现在,我只想在执行相应的配置文件时在war文件中的resources文件夹路径中使用这些文件夹之一。结果是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的内容复制到目标/类?

谢谢您的帮助

maven-2
4个回答
9
投票

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>

2
投票

为此,存在maven目录结构中的“主要”部分。与其在资源目录中创建目录,不如在src目录中创建更多目录。这是默认行为,已经通过在maven生命周期的测试阶段让测试资源覆盖主要资源来实现。

您的结构应如下所示:

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

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

为了实现这一点,您必须按照我们在问题中已经执行的方式为开发人员配置文件配置maven-resources-plugin

而且,您还要考虑:在使用dev配置文件进行构建时运行单元测试时,应该使用什么版本的属性文件?


0
投票

我在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>

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


0
投票

添加到先前的答案中,您还可以使用maven复制资源将文件从自定义路径复制到特定目录。(尽管不建议使用,理想情况下是]]

<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>
© www.soinside.com 2019 - 2024. All rights reserved.