場景:工作中的項目都是使用Maven的目錄結構,雖然能夠如期的完成開發工作,但是對於一個Maven工程的配置卻並不熟悉,這里主要介紹下Maven工程的而目錄結構。
1 標准目錄結構
src -main –bin 腳本庫 –java java源代碼文件 –resources 資源庫,會自動復制到classes目錄里 –filters 資源過濾文件 –assembly 組件的描述配置(如何打包) –config 配置文件 –webapp web應用的目錄。WEB-INF、css、js等 -test –java 單元測試java源代碼文件 –resources 測試需要用的資源庫 –filters 測試資源過濾庫 -site Site(一些文檔) target LICENSE.txt Project’s license README.txt Project’s readme
工程根目錄下就只有src和target兩個目錄
target是有存放項目構建后的文件和目錄,jar包、war包、編譯的class文件等。
target里的所有內容都是maven構建的時候生成的
參照:http://breath.iteye.com/blog/1005447
++++++++++++++++++++++++++++++++++++++++++++
1.1 Maven項目的標准目錄介紹
Maven提倡使用一個共同的標准目錄結構,使開發人員能在熟悉了一個Maven工程后,對其他的Maven工程也能清晰了解。這樣做也省去了很多設置的麻煩。
以下的文檔介紹是Maven希望的目錄結構,並且也是目錄創建工程是采用的目錄結構。Maven推薦大家盡可能的遵守這樣的目錄結構。
src/main/Java |
Application/Library sources |
src/main/resources |
Application/Library resources |
src/main/filters |
Resource filter files |
src/main/assembly |
Assembly descriptors |
src/main/config |
Configuration files |
src/main/webapps |
Web application sources |
src/test/java |
Test sources |
src/test/resources |
Test resources |
src/test/filters |
Test resource filter files |
src/site |
Site |
LICENSE.txt |
Project's license |
README.txt |
Project's readme |
在頂級目錄上是工程的描述文件pom.xml(如果使用Ant則還包括其他屬性文件,maven.xml或build.xml),另外還包括提供給最終用戶的文件,如,README.txt,LICENSE.txt等等。
頂級目錄還包括兩個子目錄:src,target。頂級目錄下可能出現的其他目錄僅僅是CVS或.svn和其他多模塊工程的工程目錄,最好不要再有其他目錄。
Target目錄是所有工程編譯構建的輸出目錄。
Src目錄包含所有工程的源碼文件,配置文件,資源文件等等。它下面的子目錄一般包含main(主要的工程源文件),test(測試文件),site(項目站點文件)。
1.2 項目構建的生命周期的介紹
Maven 2是圍繞着構建生命周期概念設計的。這意味着,構建或者發布的過程已經被清晰的定義了。
當我們使用Maven構建工程時,我們只需要了解幾個Maven定義好的命令即可,其他的工作則交給POM來完成。
以下給出Maven提供的構建生命周期列表:
validate |
validate the project is correct and all necessary information is available. |
generate-sources |
generate any source code for inclusion in compilation. |
process-sources |
process the source code, for example to filter any values. |
generate-resources |
generate resources for inclusion in the package. |
process-resources |
copy and process the resources into the destination directory, ready for packaging. |
compile |
compile the source code of the project. |
process-classes |
post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
generate-test-sources |
generate any test source code for inclusion in compilation. |
process-test-sources |
process the test source code, for example to filter any values. |
generate-test-resources |
create resources for testing. |
process-test-resources |
copy and process the resources into the test destination directory. |
test-compile |
compile the test source code into the test destination directory |
test |
run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
package |
take the compiled code and package it in its distributable format, such as a JAR. |
pre-integration-test |
perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test |
process and deploy the package if necessary into an environment where integration tests can be run. |
post-integration-test |
perform actions required after integration tests have been executed. This may including cleaning up the environment. |
verify |
run any checks to verify the package is valid and meets quality criteria. |
install |
install the package into the local repository, for use as a dependency in other projects locally. |
deploy |
done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
因此,當我們構建一個項目時,只需要了解自己希望做什么,然后執行以上對應的生命周期即可。
例如,我們希望編譯我們的工程。在命令行狀態下進入到工程的pom.xml文件所在的目錄中,使用命令:mvn compile;希望構建打包我們的工程,使用mvn package即可。
當然了,maven的構建生命周期也是可以擴展和自定義的,這里就先不做介紹了。
參照:http://hi.baidu.com/mylovechangchu/blog/item/fbda36da3644a6dfb6fd48d6.html
APACHE原址:http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
1.3 Maven工程添加新文件夾
新建立的Maven工程是沒有resources文件夾的,需要自己去創建。
步驟分為兩步:
1、在src的main和test目錄下分別建立resources文件夾
2、按照下圖所示添加即可