Pét*_*rök 16 profile maven-2 activation
我有一个多模块Maven2项目,它构建一个Web应用程序.应用程序连接到后端服务器和数据库.在我们的环境中部署了多个服务器实例,并且还有多个后端和数据库实例用于开发,UAT,生产等.因此,实际上,每个应用程序配置都需要以下3个坐标:
我正致力于统一和自动化应用程序配置.将这些不同的配置表示为Maven中的配置文件是很容易和明显的.然后,我可以通过激活每个组中的一个配置文件来创建特定配置,例如
mvn -Pserver.Server1,backend.prod,db.uat clean install
Run Code Online (Sandbox Code Playgroud)
键入和容易出错有点繁琐 - 如果特定服务器配置错误连接到错误的数据库,价格可能会很高.解决此问题的一种显而易见的方法是将所有有用的配置文件组合放入脚本文件中.
但我认为通过直接从服务器配置文件激活必要的后端和数据库配置文件,我可以比这更聪明.服务器配置文件位于主pom中,例如
<profile>
<id>server.myserver</id>
<properties>
<jboss.home>D:\Programs\jboss-4.2.1.GA</jboss.home>
<server.name>NightlyBuild</server.name>
<hosttobind>192.168.1.100</hosttobind>
<servlet.port>8080</servlet.port>
...
<db>dev02</db>
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)
并且后端和DB配置文件位于Config子模块的pom中,例如
<profile>
<id>db.dev02</id>
<activation>
<property>
<name>db</name>
<value>dev02</value>
</property>
</activation>
<properties>
<jdbc.address>jdbc:oracle:thin:@192.168.0.101:1521:dbdev02</jdbc.address>
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)
因此在理论上,由于server.myserver配置文件将db属性设置为dev02,这应该触发db.dev02子pom 中的配置文件的激活.但是,这不会发生.(如果两个轮廓在同一个pom中,也不是btw).如果我从命令行设置属性
mvn -Ddb=dev02 help:active-profiles
Run Code Online (Sandbox Code Playgroud)
然后,配置文件被激活,显然我没有拼错任何东西.
我忽略了什么吗?有没有其他方法可以使这项工作?
我看到存在类似的问题:我可以让一个maven配置文件激活另一个吗?
但是,恕我直言,这不是重复 - 我看到我的方法不起作用,我想了解原因.(我已阅读参考资料,但我可能忽略了一些明显的事情).
Bre*_*ter 19
该功能根本不存在.属性激活器使用传入的属性,而不是由配置文件设置的任何内容(否则,如果没有更复杂的逻辑,它将无法知道激活它们的顺序).
您使用的具有相同属性的解决方案可以激活您想要一起执行的操作,这是最佳解决方案.我意识到这可能并不总是令人满意 - 在这种情况下,您可以做的就是尽可能简化各个配置文件,以便您可以在命令行上以您希望的方式组合它们,而不会在它们之间复制内容.
涉及此功能的问题是:https ://issues.apache.org/jira/browse/MNG-3309
涉及属性激活的问题是:https://issues.apache.org/jira/browse/MNG-2276
Brett提到的MNG-2276问题在maven 3.x中已解决,因此现在允许您在settings.xml中定义属性以触发pom中的配置文件。这是一个例子:
在settings.xml中:
<profile>
<id>localDist</id>
<activation>
<property><name>localDist</name></property>
</activation>
<properties>
<doReleaseTasks>true</doReleaseTasks>
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)
在您的pom中(或更好的是,在您的父pom中):
<profile>
<id>doReleaseTasks</id>
<activation>
<property><name>doReleaseTasks</name></property>
</activation>
<build>
<plugins>
... mvn -DlocalDist will activate these plugins
</plugins>
</build>
</profile>
Run Code Online (Sandbox Code Playgroud)
使用强制执行器插件强制mvn 3.0或更高版本的好主意:
<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-maven</id>
<goals> <goal>enforce</goal> </goals>
<configuration>
<rules>
<requireMavenVersion>
<version>[3.0,)</version>
<message>
*** Maven 3.x required to allow cascading profiles to be activated in settings.xml (MNG-2276)
</message>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)