其實(shí)也是最近才看Maven, 以前都是用ant+ivy, 對(duì)于輕量級(jí)的項(xiàng)目來說足夠了, 而且非常靈活.
看了看Maven, 約定.... 現(xiàn)在編程都說約定, 約定是挺好, 問題是超出約定的事情太多了, 到頭來還要依賴其他東西, 真不想用maven啊.
以前我們開發(fā)環(huán)境和生產(chǎn)環(huán)境的配置文件都是單獨(dú)分開目錄存放的, ant腳本搞個(gè)變量就自動(dòng)打包不同的文件了. 我覺得管理起來也很容易, 所以看到很多maven為解決開發(fā)/生產(chǎn)環(huán)境的方案真是不太理解啊:
1. 什么 ${your.configuration}, 和spring的東西都差不多了, 配置寫到腳本里去了,....這叫啥配置...
2. 調(diào)用ant....
還是根據(jù)我以前的思路:
src/main目錄下原來有 java, resources, 我新建幾個(gè)目錄: resources-dev, resources-test, resources-prod.
resources --- 一些共享的配置文件, 一般不需要修改的
resources-dev --- 開發(fā)環(huán)境下用的配置文件, 和resources目錄下文件沒有重合.
resources-prod --- 生產(chǎn)環(huán)境下用的配置文件, 和resources目錄下文件沒有重合.
本機(jī)開發(fā)的時(shí)候設(shè)置源碼目錄為 java, resources, resources-dev, 可以直接開發(fā)調(diào)試.
編譯的時(shí)候希望maven根據(jù)不同環(huán)境, 打包不同的目錄下的文件, 我們利用maven的profile和build-helper-maven-plugin插件就可以實(shí)現(xiàn).
<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>
(文章后面有簡(jiǎn)化版本)
自己根據(jù)實(shí)際的部署環(huán)境修改吧, 運(yùn)行 mvn package -P test 就可以打包了.
反正很簡(jiǎn)單啦. 一大堆xml, 有用的沒幾句....太羅嗦了
另:目前開發(fā)用resin, intellij idea 9, 調(diào)試時(shí)webapp在開發(fā)目錄webapp下就地開發(fā), 所以我的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>
這部分是公用的, 當(dāng)然dev時(shí)候也不用打包.... 所以........ 可以用于本機(jī)開發(fā), 打包到test, production等環(huán)境.
總之部署上maven不如ant靈活, ant寫一套腳本一般來說也很少修改了.... 想改特容易. maven想復(fù)制個(gè)目錄都要想法....
______________________________________________
簡(jiǎn)化后的版本:
<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>