團隊在開發過程當中用的是maven項目,因爲對maven的一些打包流程以及相關參數配置不是太瞭解,所以應你們的需求作一下maven的講解,爲了避免誤導你們,看了不少相關資料,本身也實驗了一下,就把本身以爲對你們有用的東西列出來,作了一些簡單的介紹,起到拋磚引玉的做用,但願能對你們能有幫助。html
Maven是一個Java語言編寫的開源項目管理工具,是Apache軟件基金會的頂級項目。主要用於項目構建,依賴管理,項目信息管理。java
Maven這個單詞來自於意第緒語(猶太語),意爲知識的積累,最初在Jakata Turbine項目中用來簡化構建過程。當時有一些項目(有各自Ant build文件),僅有細微的差異,而JAR文件都由CVS來維護。因而但願有一種標準化的方式構建項目,一個清晰的方式定義項目的組成,一個容易的方式發佈項目的信息,以及一種簡單的方式在多個項目中共享JARs。git
1. maven不只是構建工具,它仍是依賴管理工具和項目管理工具,提供了中央倉庫,可以幫咱們自動下載構件。github
2.爲了解決的依賴的增多,版本不一致,版本衝突,依賴臃腫等問題,它經過一個座標系統來精確地定位每個構件(artifact)。web
3.還能幫助咱們分散在各個角落的項目信息,包括項目描述,開發者列表,版本控制系統,許可證,缺陷管理系統地址。apache
4.maven還爲全世界的java開發者提供了一個免費的中央倉庫,在其中幾乎能夠找到任何的流行開源軟件。經過衍生工具(Nexus),咱們還能對其進行快速搜索tomcat
5.maven對於目錄結構有要求,約定優於配置,用戶在項目間切換就省去了學習成本。服務器
6.跨平臺,是聲明式的,沒有依賴管理。mybatis
有些公司都不提供外網給項目組人員,所以就不能使用maven訪問遠程的倉庫地址,因此頗有必要在局域網裏找一臺有外網權限的機器,搭建nexus私服,而後開發人員連到這臺私服上,這樣的話就能夠經過這臺搭建了nexus私服的電腦訪問maven的遠程倉庫。 app
Maven庫:
http://repo2.maven.org/maven2/
Maven依賴查詢:
maven的倉庫只有兩大類:1.本地倉庫 2.遠程倉庫,在遠程倉庫中又分紅了3種:2.1 中央倉庫 2.2 私服 2.3 其它公共庫;如圖:
Maven在本地存儲構件的地方。
中央倉庫是默認的遠程倉庫,maven在安裝的時候,自帶的就是中央倉庫的配置。
Maven私服的 個特性:
1.節省本身的外網帶寬:減小重複請求形成的外網帶寬消耗
2.加速Maven構件:若是項目配置了不少外部遠程倉庫的時候,構建速度就會大大下降
3.部署第三方構件:有些構件沒法從外部倉庫得到的時候,咱們能夠把這些構件部署到內部倉庫(私服)中,供內部maven項目使用
4.提升穩定性,加強控制:Internet不穩定的時候,maven構建也會變的不穩定,一些私服軟件還提供了其餘的功能
5.下降中央倉庫的負荷:maven中央倉庫被請求的數量是巨大的,配置私服也能夠大大下降中央倉庫的壓力
當前主流的maven私服:
1.Apache的Archiva
2.JFrog的Artifactory
3.Sonatype的Nexus
詳細介紹地址:http://www.cnblogs.com/quanyongan/archive/2013/04/24/3037589.html
http://blog.csdn.net/clj198606061111/article/details/52200928
在安裝安裝nexus若是出現這種狀況:
以管理員身份運行cmd.exe便可。
安裝教程:http://jingyan.baidu.com/article/4f7d5712a1306c1a21192746.html
詳細介紹地址:http://blog.csdn.net/u014527058/article/details/50906343
1 <?xml version="1.0" encoding="UTF-8"?> 2 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> 5 6 7 <proxies> 8 9 </proxies> 10 11 12 <servers> 13 14 <!--用來實現熱部署 --> 15 <server> 16 <id>tomcat7x</id> 17 <username>admin</username> 18 <password>admin</password> 19 </server> 20 <!--用來發布項目到nexus的相對應的發佈目錄下 ,這裏的id要和eclipse中配置的同樣--> 21 <server> 22 <id>user-release</id> 23 <username>deployment</username> 24 <password>deployment123</password> 25 </server> 26 <!--用來發布項目到nexus的相對應的快照目錄下 ,這裏的id要和eclipse中配置的同樣--> 27 <server> 28 <id>user-snapshots</id> 29 <username>deployment</username> 30 <password>deployment123</password> 31 </server> 32 </servers> 33 34 <mirrors> 35 36 <mirror> 37 <id>public</id> 38 <mirrorOf>*</mirrorOf> 39 <name>Human Readable Name for this Mirror.</name> 40 <!--<url>http://repo.maven.apache.org/maven2/</url>--> 41 <url>http://maven私服ip地址:8081/nexus/content/groups/public/</url> 42 </mirror> 43 44 </mirrors> 45 46 47 <profiles> 48 49 <profile> 50 <id>nexus</id> 51 <repositories> 52 <repository> 53 <id>central</id> 54 <url>http://central</url> 55 <releases><enabled>true</enabled></releases> 56 <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots> 57 </repository> 58 </repositories> 59 <pluginRepositories> 60 <pluginRepository> 61 <id>central</id> 62 <url>http://central</url> 63 <releases><enabled>true</enabled></releases> 64 <snapshots><enabled>true</enabled></snapshots> 65 </pluginRepository> 66 </pluginRepositories> 67 </profile> 68 69 <profile> 70 <id>jdk-1.7</id> 71 <activation> 72 <activeByDefault>true</activeByDefault> 73 <jdk>1.7</jdk> 74 </activation> 75 <properties> 76 <maven.compiler.source>1.7</maven.compiler.source> 77 <maven.compiler.target>1.7</maven.compiler.target> 78 <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion> 79 </properties> 80 </profile> 81 82 </profiles> 83 84 <activeProfiles> 85 <activeProfile>nexus</activeProfile> 86 </activeProfiles> 87 </settings>
省略,這些在網上一搜一大堆,這裏就再也不囉嗦了。
知識驛站(具體瞭解地址:http://blog.csdn.net/cnhk1225/article/details/51189467):
maven中的倉庫分爲兩種,snapshot快照倉庫和release發佈倉庫。snapshot快照倉庫用於保存開發過程當中的不穩定版本,release正式倉庫則是用來保存穩定的發行版本。定義一個組件/模塊爲快照版本,只須要在pom文件中在該模塊的版本號後加上-SNAPSHOT便可(注意這裏必須是大寫)
http://www.cnblogs.com/dennyzhangdd/p/5831112.html
1 <?xml version="1.0"?> 2 <project 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>組織名</groupId> 8 <artifactId>父工程名</artifactId> 9 <version>版本號</version> 10 </parent> 11 <artifactId>子工程名</artifactId> 12 <packaging>war</packaging> 13 <dependencies> 14 15 </dependencies> 16 <profiles> 17 <!-- 開發/測試環境,默認激活 --> 18 <profile> 19 <id>dev</id> 20 <properties> 21 <env>dev</env> 22 <tomcat.host>遠程tomcat的地址</tomcat.host> 23 <tomcat.port>${tomcatPort}</tomcat.port> 24 <tomcat.username>admin</tomcat.username> 25 <tomcat.password>admin</tomcat.password> 26 </properties> 27 <activation> 28 <activeByDefault>true</activeByDefault><!--默認啓用的是dev環境配置--> 29 </activation> 30 </profile> 31 <!-- 生產環境 --> 32 <profile> 33 <id>prod</id> 34 <properties> 35 <env>prod</env> 36 <tomcat.host>遠程服務器ip地址</tomcat.host> 37 <tomcat.port>${tomcatPort}</tomcat.port> 38 <tomcat.username>admin</tomcat.username> 39 <tomcat.password>admin</tomcat.password> 40 </properties> 41 </profile> 42 <profile> 43 <id>qa</id> 44 <properties> 45 <env>qa</env> 46 <tomcat.host>遠程服務器ip地址</tomcat.host> 47 <tomcat.port>${tomcatPort}</tomcat.port> 48 <tomcat.username>admin</tomcat.username> 49 <tomcat.password>admin</tomcat.password> 50 </properties> 51 </profile> 52 </profiles> 53 54 <build> 55 <finalName>ucf-personal</finalName> 56 <plugins> 57 <!-- 用來支持maven自帶的命令 --> 58 <plugin> 59 <groupId>org.apache.maven.plugins</groupId> 60 <artifactId>maven-compiler-plugin</artifactId> 61 <version>3.1</version> 62 <configuration> 63 <source>1.7</source> 64 <target>1.7</target> 65 </configuration> 66 </plugin> 67 <!-- 用cargo插件來實現項目自動遠程熱部署 --> 68 <plugin> 69 <groupId>org.codehaus.cargo</groupId> 70 <artifactId>cargo-maven2-plugin</artifactId> 71 <version>1.4.9</version> 72 <configuration> 73 <container> 74 <containerId>tomcat7x</containerId> 75 <type>remote</type> 76 </container> 77 <configuration> 78 <type>runtime</type> 79 <properties> 80 <cargo.remote.uri>http://${tomcat.host}:${tomcat.port}/manager/text</cargo.remote.uri> 81 <cargo.remote.username>${tomcat.username}</cargo.remote.username> 82 <cargo.remote.password>${tomcat.password}</cargo.remote.password> 83 </properties> 84 </configuration> 85 </configuration> 86 </plugin> 87 </plugins> 88 <!-- 根據構建環境的改變(生產或測試)來得到相應的值 --> 89 <filters> <!-- 指定使用的 filter --> 90 <filter>src/main/resources/filter/${env}.properties</filter> 91 </filters> 92 <resources> 93 <resource> <!-- 配置須要被替換的資源文件路徑, db.properties 應該在 src/main/resource 目錄下 --> 94 <directory>src/main/resources</directory> 95 <filtering>true</filtering> <!-- 是否使用過濾器 --> 96 </resource> 97 </resources> 98 </build> 99 <properties> 100 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 101 </properties> 102 <distributionManagement> 103 <repository> 104 <id>releases</id> 105 <name>releases</name> 106 <url>http://遠程服務器ip地址:8081/nexus/content/repositories/releases/</url> 107 </repository> 108 <snapshotRepository> 109 <id>snapshots</id> 110 <name>snapshots</name> 111 <url>http://遠程服務器ip地址:8081/nexus/content/repositories/snapshots/</url> 112 </snapshotRepository> 113 </distributionManagement> 114 </project>
<profile>
的<properties>
標籤中添加與web.xml中相對應的${cas.serverLoginUrl
}的key做爲標籤,<cas.serverLoginUrl>http://10.1.1.1</cas.serverLoginUrl>
1 <profiles> 2 <profile> 3 <id>development</id> 4 <activation> 5 <activeByDefault>true</activeByDefault> 6 </activation> 7 <properties> 8 <profiles.active>dev</profiles.active> 9 <mybatis.log>ERROR</mybatis.log> 10 <log.path>/opt/dev/</log.path> 11 <cas.serverLoginUrl>http://10.1.1.1</cas.serverLoginUrl> 12 <cas.serverName>http://10.1.1.1</cas.serverName> 13 <cas.serverUrlPrefix>http://10.1.1.1</cas.serverUrlPrefix> 14 </properties> 15 </profile> 16 </profiles>
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-war-plugin</artifactId> 4 <version>2.1.1</version> 5 <configuration> 6 <webResources> 7 <resource> 8 <directory>src/main/webapp</directory> 9 <filtering>true</filtering> 10 </resource> 11 </webResources> 12 </configuration> 13 </plugin>
1 <filter> 2 <filter-name>CASFilter</filter-name> 3 <filter-class>com.ezubo.global.om.web.filter.LoginFilter</filter-class> 4 <init-param> 5 <param-name>casServerLoginUrl</param-name> 6 <param-value>${cas.serverLoginUrl}</param-value> 7 </init-param> 8 <init-param> 9 <param-name>serverName</param-name> 10 <param-value>${cas.serverName}</param-value> 11 </init-param> 12 <init-param> 13 <param-name>noFilterUrl</param-name> 14 <param-value>*.js</param-value> 15 </init-param> 16 </filter>
詳細介紹地址:https://my.oschina.net/cokolin/blog/409164
1 <?xml version="1.0"?> 2 <plugin> 3 <groupId>org.mortbay.jetty</groupId> 4 <artifactId>jetty-maven-plugin</artifactId> 5 <version>8.1.16.v20140903</version> 6 <configuration> 7 <scanIntervalSeconds>5</scanIntervalSeconds> 8 <webApp> 9 <contextPath>/ucf-company</contextPath> 10 </webApp> 11 <connectors> 12 <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> 13 <port>9080</port> 14 </connector> 15 <connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector"> 16 <port>9443</port> 17 <password>changeit</password> 18 </connector> 19 <!-- 這兩個connector的配置要改成: --> 20 <connector implementation="org.eclipse.jetty.server.bio.SocketConnector"> 21 <port>8080</port> 22 </connector> 23 <connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector"> 24 <port>8443</port> 25 <password>changeit</password> 26 </connector> 27 </connectors> 28 </configuration> 29 </plugin>
Maven的proguard插件只能混淆jar包。若是要混淆Web項目則將Web混淆的工程jar的classes替換打的war包的classes文件夾便可。
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>com.github.wvengen</groupId> 5 <artifactId>proguard-maven-plugin</artifactId> 6 <version>2.0.7</version> 7 <executions> 8 <execution> 9 <phase>package</phase> 10 <goals> 11 <goal>proguard</goal> 12 </goals> 13 </execution> 14 </executions> 15 <configuration> 16 <attach>true</attach> 17 <attachArtifactClassifier>pg</attachArtifactClassifier> 18 <!-- attach 的做用是在 install 與 deploy 時將生成的 pg 文件也安裝與部署 --> 19 <options> <!-- 詳細配置方式參考 ProGuard 官方文檔 --> 20 <!--<option>-dontobfuscate</option>--> 21 <option>-ignorewarnings</option> <!--忽略全部告警--> 22 <option>-dontshrink</option> <!--不作 shrink --> 23 <option>-dontoptimize</option> <!--不作 optimize --> 24 <option>-dontskipnonpubliclibraryclasses</option> 25 <option>-dontskipnonpubliclibraryclassmembers</option> 26 27 <option>-repackageclasses com.ucfgroup.framework.web.app.pg</option> 28 <!--平行包結構(重構包層次),全部混淆的類放在 pg 包下--> 29 30 <!-- 如下爲 Keep,哪些內容保持不變,由於有一些內容混淆後(a,b,c)致使反射或按類名字符串相關的操做失效 --> 31 32 <option>-keep class **.package-info</option> 33 <!--保持包註解類--> 34 35 <option>-keepattributes Signature</option> 36 <!--JAXB NEED,具體緣由不明,不加會致使 JAXB 出異常,若是不使用 JAXB 根據須要修改--> 37 <!-- Jaxb requires generics to be available to perform xml parsing and without this option ProGuard was not retaining that information after obfuscation. That was causing the exception above. --> 38 39 <option>-keepattributes SourceFile,LineNumberTable,*Annotation*</option> 40 <!--保持源碼名與行號(異常時有明確的棧信息),註解(默認會過濾掉全部註解,會影響框架的註解)--> 41 42 <option>-keep class SSRuntimeEasyJava.** { *;}</option> 43 <!--保持 Bean 類,(因爲不少框架會對 Bean 中的內容作反射處理,請根據本身的業務調整) --> 44 45 </options> 46 <outjar>${project.build.finalName}-pg</outjar> 47 <libs> 48 <lib>${java.home}/lib/rt.jar</lib> 49 </libs> 50 51 </configuration> 52 </plugin> 53 </plugins> 54 </build>
研究過程出現的問題:
若是沒有<injar></injar>會報以下錯誤信息:
1 [ERROR] Failed to execute goal com.github.wvengen:proguard-maven-plugin:2.0.7:proguard (default) on project ucf-company: Obfuscation failed (result=1) -> [Help 1] 2 [ERROR] 3 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 4 [ERROR] Re-run Maven using the -X switch to enable full debug logging. 5 [ERROR] 6 [ERROR] For more information about the errors and possible solutions, please read the following articles: 7 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
具體實現參照地址:
http://www.cnblogs.com/yanduanduan/p/5340060.html
http://blog.csdn.net/wltj920/article/details/48970869
1 <profiles> 2 <!-- 開發/測試環境,默認激活 --> 3 <profile> 4 <id>dev</id> 5 <properties> 6 <env>dev</env> 7 </properties> 8 <activation> 9 <activeByDefault>true</activeByDefault> 10 <!--默認啓用的是dev環境配置--> 11 </activation> 12 </profile> 13 <!-- 生產環境 --> 14 <profile> 15 <id>prod</id> 16 <properties> 17 <env>prod</env> 18 </properties> 19 </profile> 20 <!-- qa環境 --> 21 <profile> 22 <id>qa</id> 23 <properties> 24 <env>qa</env> 25 </properties> 26 </profile> 27 </profiles> 28 <build> 29 <filters> 30 <!-- 指定使用的 filter --> 31 <filter>src/main/resources/filter/${env}.properties</filter> 32 </filters> 33 <resources> 34 <resource> 35 <!-- 配置須要被替換的資源文件路徑, db.properties 應該在 src/main/resource 目錄下 --> 36 <directory>src/main/resources</directory> 37 <filtering>true</filtering> 38 <!-- 是否使用過濾器 --> 39 </resource> 40 </resources> 41 </build>
用項目層次的劃分替代包層次的劃分能給咱們帶來以下好處:
一個簡單的Maven模塊結構是這樣的:
---- app-parent
|-- pom.xml (pom)
|
|-- app-util
| |-- pom.xml (jar)
|
|-- app-dao
| |-- pom.xml (jar)
|
|-- app-service
| |-- pom.xml (jar)
|
|-- app-web
|-- pom.xml (war)
建立Maven多模塊項目參照地址:http://www.cnblogs.com/huboking/p/4351921.html
mvn help:system 自動在本用戶下建立 ~/.m2/repository
mvn clean compile 清理編譯
mvn clean test 清理測試
mvn clean package 清理打包
mvn clean install 清理將打包好的jar存入 本地倉庫 注意是本地倉庫
mvn archetype:generate 使用Archetype生成項目骨架
mvn clean deploy 根據pom中的配置信息將項目發佈到遠程倉庫中
進行maven clean、update,而後進行工程clean、tomcat clean,若是尚未去掉則說明是工程代碼有問題,不屬於工程環境的問題。
產生緣由:
由於一些maven管理的jar沒能正確下載。
先查看buildpath,找問題的jar包,以後再一個個解決
對這些有問題的jar,是由於當前的maven倉庫沒法找到,因此能夠經過以下方法添加能找到相應jar的maven倉庫:
1.在google中直接搜索相應jar,好比:jmxri-1.2.1.jar
2.在搜索結果中找到有這個jar的maven倉庫,並加入到setting.xml中。
3.從新對項目進行 右鍵 - maven - update dependencies。
tomcat-users.xml,至少要添加manager,manager-gui,manager-script,manager-jmx,manager-status,添加權限
<tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <!--admin角色沒有的要注意添上--> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="admin" password="admin" roles="admin,manager,tomcat,manager-gui,manager-script,manager-jmx,manager-status"/> <!--你用哪一個用戶作管理員,則須要分配相應的角色,我給admin用戶分配了admin和manager連個角色--> </tomcat-users>
注意:當解壓的項目已存在時,會部署不成功,須要在context.xml中再Context標籤中加入antiResourceLocking="true" 就能夠了。
新版無需修改maven的settings.xml文件,網上不少誤導別人的
mvn cargo:redeploy
<!-- 遠程部署 ,tomcat必須啓動-->
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.remote.uri>http://${tomcat.host}:${tomcat.port}/manager/text</cargo.remote.uri> <cargo.remote.username>${tomcat.username}</cargo.remote.username> <cargo.remote.password>${tomcat.password}</cargo.remote.password> </properties> </configuration> </configuration> </plugin>
遇到的錯誤:
cargo-maven2-plugin:1.4.9:redeploy failed: Failed to undeploy
緣由:
是antiResourceLocking屬性引發的,參考以下:http://stackoverflow.com/questions/14873219/cannot-undeploy-a-web-app-completely-in-tomcat-7
修改context.xml文件<Context antiResourceLocking="true">
如上圖所說便可實現maven打包時傳多個參數的需求