Maven多个配置文件:我可以指定大多数依赖项概况无关的



如何在maven文件中指定很小的配置文件差异?我不想为每个不同配置文件复制几乎相同的依赖关系。

在生产环境中,可以使用" openshift"的配置文件。这样,SpringBoot应用程序在OpenShift RedHat环境中运行良好。对于本地发展,我需要略有不同的依赖性。

例如:依赖关系中仅有的2个差异带有评论。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <-- **** Part 1: next lines only in PROFILE 'openshift' -->
   <exclusions>
       <exclusion>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
       </exclusion>
    </exclusions>
    <-- till here -->
 </dependency>
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
       <-- ***** Part 2: next lines only in PROFILE 'openshift' -->
       <scope>provided</scope>
       <-- till here -->
  </dependency>

有没有办法仅标记此行不同的配置文件?

问题2:我应该使用默认配置文件,还是可以标记指定配置文件的行?

我的目标是使一些针对环境的依赖关系。大多数依赖项应该仅在Maven文件中一次。

是的,这是可能的。

您可以在Maven中指定大多数部分独立于配置文件。以及一个较小的剖面概况取决于(对于情况)。下面的Maven文件证明了这一点。

  • 如果您使用-p non_openshift运行回声是:(1)****外面配置文件***** *****和(2)**** profile non_openshift ****。

  • 如果您使用-p OpenShift运行,则回声是:(1)****外面profile ***** *****和(2)**** profile openshift ****。

这是maven文件。我使用了两种类型的" echo'-plugins",因此它们不会互相干扰。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>nl.deholtmans</groupId>
    <artifactId>maven_profile_test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <profiles>
        <profile>
            <id>not_openshift</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>*** Profile: NOT openshift *** </echo>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>openshift</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>*** Profile: openshift *** </echo>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
            </plugins>
            </build>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <groupId>com.soebes.maven.plugins</groupId>
                <artifactId>echo-maven-plugin</artifactId>
                <version>0.3.0</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>echo</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <echos>
                        <echo>**** OUTSIDE PROFILE ***** </echo>
                    </echos>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

最新更新