Maven插件实现的autoconfig机制(转)

时间:2023-03-10 01:52:32
Maven插件实现的autoconfig机制(转)

autoconfig这种机制在软件开发和发布的过程中是非常方便也是非常必要的一种动态替换配置信息的一种手段,一种很贴切的比喻:这个就像在windows下面安装一个软件时,我们按照安装向导给我们弹出提示填写信息一样(这些信息就是一些定制化的信息)。

Maven的强大插件机制,可以和autoconfig机制结合起来,发挥巨大的威力。

实际项目中,基本都是在deploy下面实现配置文件的读取和替换的。这里,其实就是利用了一个maven-autoconf-plugin插件实现的这个功能。具体deploy下面pom.xml的配置片段如下:

  1. <profiles>
  2. <!-- Dev profile will configure all files and copy them to target/dev, for testing purpose -->
  3. <profile>
  4. <id>dev</id>
  5. <activation>
  6. <property>
  7. <name>env</name>
  8. <value>!release</value>
  9. </property>
  10. </activation>
  11. <build>
  12. <plugins>
  13. <!-- do auto config for integration test -->
  14. <plugin>
  15. <groupId>com.alibaba.maven.plugins</groupId>
  16. <artifactId>maven-autoconf-plugin</artifactId>
  17. <version>0.3-alpha-9</version>
  18. <executions>
  19. <execution>
  20. <phase>pre-integration-test</phase>
  21. <goals>
  22. <goal>config</goal>
  23. </goals>
  24. </execution>
  25. </executions>
  26. <configuration>
  27. <destFiles>
  28. <destFile>${project.basedir}</destFile>
  29. </destFiles>
  30. <includeDescriptorPatterns>
  31. <!-- intl-site flavor -->
  32. <includeDescriptorPattern>autoconf/auto-config.xml</includeDescriptorPattern>
  33. <!-- china-site flavor -->
  34. <includeDescriptorPattern>conf/META-INF/autoconf/auto-config.xml</includeDescriptorPattern>
  35. </includeDescriptorPatterns>
  36. <includePackagePatterns>
  37. <includePackagePattern>**/*.war</includePackagePattern>
  38. </includePackagePatterns>
  39. </configuration>
  40. </plugin>
  41. </plugins>
  42. </build>
  43. </profile>
  44. </profiles>

这里,又一次应用了COC的思想,插件会去扫描autoconf/auto-config.xml或者conf/META-INF/autoconf/auto-config.xml。

目前这个插件还比较鸡肋,不过基本功能都是有的,详细的介绍,可以直接参考他的官方介绍:http://repo.alibaba-inc.com/mvn/internal/snapshots/sites/maven-autoconf-plugin/config-mojo.html

常用的几个属性列举如下:

  • includeDescriptorPatterns:autoconfig所要扫描的描述文件的匹配路径。
  • userProp:指定用户实际使用配置信息的配置文件的路径,如:-DuserProp=/home/abc/properties.txt

本文来自于:http://hittyt.iteye.com/blog/889158

相关文章