MAVEN:如何为开发和生产环境建立不同的配置文件 --我的简洁方案

其实也是最近才看Maven, 以前都是用ant+ivy, 对于轻量级的项目来说足够了, 而且非常灵活.

看了看Maven, 约定.... 现在编程都说约定, 约定是挺好, 问题是超出约定的事情太多了, 到头来还要依赖其他东西, 真不想用maven啊.

以前我们开发环境和生产环境的配置文件都是单独分开目录存放的, ant脚本搞个变量就自动打包不同的文件了. 我觉得管理起来也很容易, 所以看到很多maven为解决开发/生产环境的方案真是不太理解啊:
  1. 什么 ${your.configuration}, 和spring的东西都差不多了, 配置写到脚本里去了,....这叫啥配置...
  2. 调用ant....

还是根据我以前的思路:

 src/main目录下原来有 java, resources, 我新建几个目录: resources-dev, resources-test, resources-prod.

resources --- 一些共享的配置文件, 一般不需要修改的
resources-dev --- 开发环境下用的配置文件, 和resources目录下文件没有重合.
resources-prod --- 生产环境下用的配置文件, 和resources目录下文件没有重合.


本机开发的时候设置源码目录为 java, resources, resources-dev, 可以直接开发调试.

编译的时候希望maven根据不同环境, 打包不同的目录下的文件, 我们利用maven的profile和build-helper-maven-plugin插件就可以实现.

    <profiles>
        
<profile>
            
<id>dev</id>
            
<build>
                
<plugins>
                    
<plugin>
                        
<groupId>org.codehaus.mojo</groupId>
                        
<artifactId>build-helper-maven-plugin</artifactId>
                        
<version>1.5</version>
                        
<executions>
                            
<execution>
                                
<id>add-resource</id>
                                
<phase>generate-resources</phase>
                                
<goals>
                                    
<goal>add-resource</goal>
                                
</goals>
                                
<configuration>
                                    
<resources>
                                        
<resource>
                                            
<directory>src/main/resources_dev</directory>
                                        
</resource>
                                    
</resources>
                                
</configuration>
                            
</execution>
                        
</executions>
                    
</plugin>
                    
<plugin>
                        
<artifactId>maven-war-plugin</artifactId>
                        
<executions>
                            
<execution>
                                
<phase>package</phase>
                                
<goals>
                                    
<goal>war</goal>
                                
</goals>
                                
<configuration>
                                    
<classifier>dev</classifier>
                                
</configuration>
                            
</execution>
                        
</executions>
                    
</plugin>

                
</plugins>
            
</build>
        
</profile>
        
<profile>
            
<id>test</id>
            
<build>
                
<plugins>
                    
<plugin>
                        
<groupId>org.codehaus.mojo</groupId>
                        
<artifactId>build-helper-maven-plugin</artifactId>
                        
<version>1.5</version>
                        
<executions>
                            
<execution>
                                
<id>add-resource</id>
                                
<phase>generate-resources</phase>
                                
<goals>
                                    
<goal>add-resource</goal>
                                
</goals>
                                
<configuration>
                                    
<resources>
                                        
<resource>
                                            
<directory>src/main/resources_test</directory>
                                        
</resource>
                                    
</resources>
                                
</configuration>
                            
</execution>
                        
</executions>
                    
</plugin>
                    
<plugin>
                        
<groupId>org.apache.maven.plugins</groupId>
                        
<artifactId>maven-war-plugin</artifactId>
                        
<executions>
                            
<execution>
                                
<phase>package</phase>
                                
<goals>
                                    
<goal>war</goal>
                                
</goals>
                                
<configuration>
                                    
<classifier>test</classifier>
                                
</configuration>
                            
</execution>
                        
</executions>
                    
</plugin>
                
</plugins>
            
</build>
        
</profile>
    
</profiles>

(文章后面有简化版本)

自己根据实际的部署环境修改吧, 运行 mvn package -P test 就可以打包了.


反正很简单啦. 一大堆xml, 有用的没几句....太罗嗦了


另:目前开发用resin, intellij idea 9, 调试时webapp在开发目录webapp下就地开发, 所以我的maven脚本还有如下部分:

    <build>
        
<plugins>
            
<plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-war-plugin</artifactId>
                
<version>2.1</version>
                
<configuration>
                    
<warSourceExcludes>WEB-INF/lib/**,WEB-INF/classes/**,WEB-INF/work/**,WEB-INF/tmp/**</warSourceExcludes>
                    
<archive>
                      
<addMavenDescriptor>false</addMavenDescriptor>
                    
</archive>
                
</configuration>
            
</plugin>
        
</plugins>
    
</build>


这部分是公用的, 当然dev时候也不用打包.... 所以........ 可以用于本机开发, 打包到test, production等环境.



总之部署上maven不如ant灵活, ant写一套脚本一般来说也很少修改了.... 想改特容易. maven想复制个目录都要想法....


______________________________________________
简化后的版本:

    <properties>
       
<package.target>notexists</package.target>
    
</properties>
    
    
<profiles>
        
<profile>
            
<id>dev</id>
            
<properties>
                 
<package.target>dev</package.target>
            
</properties>
        
</profile>
        
<profile>
            
<id>test</id>
            
<properties>
                 
<package.target>test</package.target>
            
</properties>
        
</profile>
    
</profiles>

    
<build>
        
<plugins>
            
<plugin>
                
<groupId>org.codehaus.mojo</groupId>
                
<artifactId>build-helper-maven-plugin</artifactId>
                
<version>1.5</version>
                
<executions>
                    
<execution>
                        
<id>add-resource</id>
                        
<phase>generate-resources</phase>
                        
<goals>
                            
<goal>add-resource</goal>
                        
</goals>
                        
<configuration>
                            
<resources>
                                
<resource>
                                    
<directory>src/main/resources_${package.target}</directory>
                                
</resource>
                            
</resources>
                        
</configuration>
                    
</execution>
                
</executions>
            
</plugin>
            
<plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-war-plugin</artifactId>
                
<version>2.1</version>
                
<configuration>
                    
<warSourceExcludes>WEB-INF/lib/**,WEB-INF/classes/**,WEB-INF/work/**,WEB-INF/tmp/**</warSourceExcludes>
                    
<archive>
                      
<addMavenDescriptor>false</addMavenDescriptor>
                    
</archive>
                
</configuration>
                
<executions>
                    
<execution>
                        
<phase>package</phase>
                        
<goals>
                            
<goal>war</goal>
                        
</goals>
                        
<configuration>
                            
<classifier>${package.target}</classifier>
                        
</configuration>
                    
</execution>
                
</executions>
            
</plugin>
        
</plugins>
    
</build>



posted on 2010-10-27 22:31 Scud(飞云小侠) 阅读(11401) 评论(3)  编辑  收藏 所属分类: Java

评论

# re: MAVEN:如何为开发和生产环境建立不同的配置文件 --我的简洁方案 2010-10-28 10:35 Scud(飞云小侠)

http://jdkcn.com/entry/pack-different-package-by-environment-in-maven.html

http://hi.baidu.com/guorabbit/blog/item/a908adeff31eec1dfdfa3cbb.html

上面这2个也可以, 不过只能用于package了.

看了上面2个方案, 发现我的方案也可以简化, 就是profile里面只定义变量就可以了, 其他公用的提取出来用变量.  回复  更多评论   

# re: MAVEN:如何为开发和生产环境建立不同的配置文件 --我的简洁方案 2010-10-28 18:51 jacklondon

如何为开发和生产环境建立不同的配置文件 --我用ANT+ bat批处理环境变量  回复  更多评论   

# re: MAVEN:如何为开发和生产环境建立不同的配置文件 --我的简洁方案[未登录] 2013-09-18 16:20 过客

谢谢!很不错!已经用上了!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年9月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

公告

文章发布许可
创造共用协议:署名,非商业,保持一致

我的邮件
cnscud # gmail


常用链接

留言簿(14)

随笔分类(110)

随笔档案(102)

相册

友情链接

技术网站

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜