在配置文件中使用 Maven 复制和覆盖一些资源
Copying and overwriting some resources with maven in a profile
我有两个项目,它们是从一个 maven 多项目构建的,项目 A 和项目 B。项目 B 最近上线,与项目 A 的区别很小,即它使用了几个不同的配置文件。我的想法是为 projectB 创建一个配置文件并添加额外的配置文件/替换现有的配置文件。我在 Whosebug 上尝试了几种方法,包括复制重命名插件和 mvn 依赖插件。部分问题似乎是两个项目都有一个配置名称相同的文件。
我的 current/last 方法是按以下方式使用 maven-resources-plugin:
我创建了一个额外的目录:src/main/projectbresources 和一个 projectbprofile:
<profile>
<id>projectb</id>
<activation><activeByDefault>false</activeByDefault></activation>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/projectbresources</directory>
<filtering>true</filtering>
</resource>
</resources>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
我在 src/main/resources 和 src/main/projectbresources 中都有一个同名文件,但是,无论我使用哪个配置文件构建,来自 src/main/resources 的文件始终位于目标文件夹中。仅在 projectbresources 中的其他文件被复制到 target。
根据您的陈述“项目 B 最近上线并且与项目 A 的差别很小,即它使用了几个不同的配置文件。”我会说您不知道' 需要 2 个独立的项目。您只能有一个项目,并根据激活的配置文件选择不同的配置。使用这种方法,两种情况的配置文件可以具有相同的名称但不同的内容。
您的项目结构将如下所示(作为示例):
项目根文件夹:
pom.xml
- src
-- main
--- resources-profileA
--- resources-profileB
现在,pom.xml 包含 2 个配置文件:profileA 和 profileB。每个配置文件定义它自己的 build/resources.
<profiles>
<profile>
<id>projectA</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileA</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>projectB</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileB</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
更多信息,您可以默认启用一个配置文件。这将帮助您的开发环境自动使用一个。如果您想为该配置文件使用默认的活动配置文件,您可以将资源文件夹命名为 resources(默认 maven 名称)并为第二个配置文件保留不同的名称。
更新:
为了 'easy' 并且只添加一些额外的文件,您可以使用 Maven 默认的配置文件 projectA 并只为 projectB 定义一个配置文件。
项目根文件夹变为:
pom.xml
- src
-- main
--- resources
--- resources-profileB
并且 pom.xml 将仅包含一个默认情况下处于非活动状态的配置文件:
<profiles>
<profile>
<id>projectB</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileB</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
如何为两个配置文件构建:
为无配置文件构建等同于为 projectA 构建:
mvn clean install
为 projectB 配置文件构建将忽略默认文件夹中的资源,并且将仅使用配置文件 (src/main/resources-profileB) 中定义的资源。
mvn clean install -P projectB
我有两个项目,它们是从一个 maven 多项目构建的,项目 A 和项目 B。项目 B 最近上线,与项目 A 的区别很小,即它使用了几个不同的配置文件。我的想法是为 projectB 创建一个配置文件并添加额外的配置文件/替换现有的配置文件。我在 Whosebug 上尝试了几种方法,包括复制重命名插件和 mvn 依赖插件。部分问题似乎是两个项目都有一个配置名称相同的文件。
我的 current/last 方法是按以下方式使用 maven-resources-plugin: 我创建了一个额外的目录:src/main/projectbresources 和一个 projectbprofile:
<profile>
<id>projectb</id>
<activation><activeByDefault>false</activeByDefault></activation>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/projectbresources</directory>
<filtering>true</filtering>
</resource>
</resources>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
我在 src/main/resources 和 src/main/projectbresources 中都有一个同名文件,但是,无论我使用哪个配置文件构建,来自 src/main/resources 的文件始终位于目标文件夹中。仅在 projectbresources 中的其他文件被复制到 target。
根据您的陈述“项目 B 最近上线并且与项目 A 的差别很小,即它使用了几个不同的配置文件。”我会说您不知道' 需要 2 个独立的项目。您只能有一个项目,并根据激活的配置文件选择不同的配置。使用这种方法,两种情况的配置文件可以具有相同的名称但不同的内容。
您的项目结构将如下所示(作为示例):
项目根文件夹:
pom.xml
- src
-- main
--- resources-profileA
--- resources-profileB
现在,pom.xml 包含 2 个配置文件:profileA 和 profileB。每个配置文件定义它自己的 build/resources.
<profiles>
<profile>
<id>projectA</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileA</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>projectB</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileB</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
更多信息,您可以默认启用一个配置文件。这将帮助您的开发环境自动使用一个。如果您想为该配置文件使用默认的活动配置文件,您可以将资源文件夹命名为 resources(默认 maven 名称)并为第二个配置文件保留不同的名称。
更新:
为了 'easy' 并且只添加一些额外的文件,您可以使用 Maven 默认的配置文件 projectA 并只为 projectB 定义一个配置文件。
项目根文件夹变为:
pom.xml
- src
-- main
--- resources
--- resources-profileB
并且 pom.xml 将仅包含一个默认情况下处于非活动状态的配置文件:
<profiles>
<profile>
<id>projectB</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileB</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
如何为两个配置文件构建:
为无配置文件构建等同于为 projectA 构建:
mvn clean install
为 projectB 配置文件构建将忽略默认文件夹中的资源,并且将仅使用配置文件 (src/main/resources-profileB) 中定义的资源。
mvn clean install -P projectB