其實也是最近才看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>