成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Maven Web項目部署到Tomcat

開發 后端
通過Maven來搭建項目是越來越多人的選擇,我也就湊了一下熱鬧,用maven來搭建了項目,發現還挺好用,但是也遇到了很多問題,下面記錄一下Web項目部署到Tomcat下的問題。

通過Maven來搭建項目是越來越多人的選擇,我也就湊了一下熱鬧,用maven來搭建了項目,發現還挺好用,但是也遇到了很多問題,下面記錄一下Web項目部署到Tomcat下的問題。

1、普通的WEB項目,就是雖然是用maven搭建的,但是沒有使用profiles.xml文件來配置參數。這樣的項目可以通過以下的方式進行部署:

直接mvn clean package -DskipTests,進行打包,

1) 然后在可以把war包拷到tomcat目錄下的Webapp目錄下

2)修改tomcat目錄下的conf目錄下的server.xml文件,在Host標簽之間添加如下一句話:

  1. <Context docBase="D:\IdeaProjects\Test\example\example-web\target\example- web" reloadable="false" path=""/> 

2、使用profiles.xml配置了默認參數,而在web的配置文件中使用到了這些參數,這個時候使用命令打包的時候要指定你要使用哪一個profiles id來裝配你的項目,命令如下mvn clean package -P development ,其中-p是指啟用哪一個profiles id。然后下面部署到tomcat的方法和上面的就一樣了

使用maven的話推薦一個IDE工具 Intellij IDEA,他可以直接通過視圖話的方式進行指定profiles id。

下面轉一篇文章,講profile的

Profiles是maven的一個很關鍵的術語:profile是用來定義一些在build lifecycle中使用的environmental variations,profile可以設置成在不同的環境下激活不同的profile(例如:不同的OS激活不同的profile,不同的JVM激活不同的profile,不同的dabase激活不同的profile等等)。

定義Profiles

你可以把profiles定義在4個地方:

1、%M2_HOME%/conf/settings.xml,這是針對該部電腦的所有user的profiles,是global profiles,它會影響所有的maven project build

2、<your -home-directory>/.m2/settings.xml,這是針對per user的profiles,是user級的profiles,它會影響當前user的所有maven project build

3、定義在pom.xml文件里面,這是僅針對該project的profiles,是project級的profiles

4、profiles.xml,它和pom.xml在同一個目錄下,也是project級的profiles,使用profiles.xml的目的是希望把profiles的設置從pom.xml里抽離出來設置。

定義在這4個地方的profiles中,涉及范圍越窄的profiles會覆蓋范圍越寬的profiles。即:定義在pom.xml里profiles會覆蓋profiles.xml的,profiles.xml的會覆蓋<your -home-directory>/.m2/settings.xml的,<your -home-directory>/.m2/settings.xml的會覆蓋%M2_HOME%/conf/settings.xml的。

不過請注意:設置在pom.xml里的profiles是最最推薦的,因為pom.xml會被deploy到repository里,所以pom.xml里的profiles才會available for subsequent builds originating from the repository or as transitive dependencies。而settings.xml和profiles.xml里定義的profiles不會被deploy到repository,則有諸多限制,因此,只有下面幾個profiles能夠在settings.xml和profiles.xml里定義:

repositories

pluginRepositories

properties 

其他類型的profiles必須在pom.xml里定義(上面3個profiles也可以在pom.xml里定義)。

Pom.xml能夠定義的profiles包括:

  1. <repositories> 
  2.  
  3. <pluginRepositories> 
  4.  
  5. <dependencies> 
  6.  
  7. <plugins> 
  8.  
  9. <properties> (not actually available in the main POM, but used behind the scenes)  
  10.  
  11. <modules> 
  12.  
  13. <reporting> 
  14.  
  15. <dependencyManagement> 
  16.  
  17. <distributionManagement> 
  18.  
  19. a subset of the <build> element, which consists of:  
  20.  
  21. <defaultGoal> 
  22.  
  23. <resources> 
  24.  
  25. <testResources> 
  26.  
  27. <finalName> 

2、激活Profiles

激活profiles有下列幾種方式:

Explicitly

Through Maven settings

Based on environment variables

OS settings

Present or missing files

1)通過mvn命令的-P參數來顯示激活profiles,該參數值是profile id list(之間用逗號連接)。如:

  1. mvn groupId:artifactId:goal -P profileId-1,profileId-2 

2) 通過在settings.xml里設置<activeProfiles> element來激活(當然<profiles>也必須在settings.xml里定義)

  1. <settings> 
  2. ...  
  3. <profiles> 
  4. <profile> 
  5. <id>profile1</id> 
  6. ...  
  7. </profile> 
  8. </profiles> 
  9. <activeProfiles> 
  10. <activeProfile>profile-1</activeProfile> 
  11. </activeProfiles> 
  12. ...  
  13. </settings> 

列在<activeProfiles>里的profiles list會在每一個project執行時被激活

3)Profiles還可以基于detect到的build environment 的state來自動激活,而不需要象上面2種方式顯式激活。這只需要在profile定義時使用<activation> element。如:

  1. <profiles> 
  2. <profile> 
  3. <activation> 
  4. <jdk>1.4</jdk> 
  5. </activation> 
  6. ...  
  7. </profile> 
  8. </profiles> 

上面的代碼表示:如果JDK version start with 1.4 (eg. "1.4.0_08", "1.4.2_07", "1.4"),該profile會被激活

  1. <profiles> 
  2. <profile> 
  3. <activation> 
  4. <property> 
  5. <name>debug</name> 
  6. </property> 
  7. </activation> 
  8. ...  
  9. </profile> 
  10. </profiles> 

上面的代碼表示:如果存在system propertie “debug”,該profile會被激活。為了激活它,輸入的命令類似于:

  1. mvn groupId:artifactId:goal –Ddebug 
  1. <profiles> 
  2. <profile> 
  3. <activation> 
  4. <property> 
  5. <name>environment</name> 
  6. <value>test</value> 
  7. </property> 
  8. </activation> 
  9. ...  
  10. </profile> 
  11. </profiles> 

上面的代碼表示:如果存在system propertie “environment”的值為test,該profile會被激活。為了激活它,輸入的命令類似于:

  1. mvn groupId:artifactId:goal -Denvironment=test 

4)Profiles還可以基于OS setting來自動激活

  1. <profiles> 
  2. <profile> 
  3. <activation> 
  4. <os> 
  5. <name>Windows XP</name> 
  6. <family>Windows</family> 
  7. <arch>x86</arch> 
  8. <version>5.1.2600</version> 
  9. </os> 
  10. </activation> 
  11. ...  
  12. </profile> 
  13. </profiles> 

上面的代碼表示:如果OS為windows xp,該profile會被激活

5)根據某個file不存在而激活profile。例如下面定義的profile是在target/generated-sources/axistools/wsdl2java/org/apache/maven不存在時激活

  1. <profiles> 
  2. <profile> 
  3. <activation> 
  4. <file> 
  5. <missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing> 
  6. </file> 
  7. </activation> 
  8. ...  
  9. </profile> 
  10. </profiles> 

使用Profiles時要注意的2個問題

第一、external properties

不是定義在pom.xml里的properties都稱為external properties。舉例說明最明了:

pom.xml:

  1. <project> 
  2. ...  
  3. <build> 
  4. <plugins> 
  5. <plugin> 
  6. <groupId>org.myco.plugins</groupId> 
  7. <artifactId>spiffy-integrationTest-plugin</artifactId> 
  8. <version>1.0</version> 
  9. <configuration> 
  10. <appserverHome>${appserver.home}</appserverHome> 
  11. </configuration> 
  12. </plugin> 
  13. ...  
  14. </plugins> 
  15. </build> 
  16. ...  
  17. </project> 

~/.m2/settings.xml

  1. <settings> 
  2. ...  
  3. <profiles> 
  4. <profile> 
  5. <id>appserverConfig</id> 
  6. <properties> 
  7. <appserver.home>/path/to/appserver</appserver.home> 
  8. </properties> 
  9. </profile> 
  10. </profiles> 
  11. <activeProfiles> 
  12. <activeProfile>appserverConfig</activeProfile> 
  13. </activeProfiles> 
  14. ...  
  15. </settings> 

當你執行該pom時,運行正常。但如果another user執行時,則運行失敗,因為無法解析${appserver.home}(這是由于該properties是定義在user級別的settings.xml)。

解決方法就是把該profile放到pom.xml里定義,但這樣做的缺點是所有使用該profile的pom.xml每個都要定義一次該profile。

最好的解決方法是:Since Maven provides good support for project inheritance, it's possible to stick this sort of configuration in the pluginManagement section of a team-level POM or similar, and simply inherit the paths

第二、pom.xml里定義的profiles不符合激活條件

依然是舉個例子:

pom.xml:

  1. <project> 
  2. ...  
  3. <profiles> 
  4. <profile> 
  5. <id>appserverConfig-dev</id> 
  6. <activation> 
  7. <property> 
  8. <name>env</name> 
  9. <value>dev</value> 
  10. </property> 
  11. </activation> 
  12. <properties> 
  13. <appserver.home>/path/to/dev/appserver</appserver.home> 
  14. </properties> 
  15. </profile> 
  16. <profile> 
  17. <id>appserverConfig-dev-2</id> 
  18. <activation> 
  19. <property> 
  20. <name>env</name> 
  21. <value>dev-2</value> 
  22. </property> 
  23. </activation> 
  24. <properties> 
  25. <appserver.home>/path/to/dev/appserver2</appserver.home> 
  26. </properties> 
  27. </profile> 
  28. </profiles> 
  29. <build> 
  30. <plugins> 
  31. <plugin> 
  32. <groupId>org.myco.plugins</groupId> 
  33. <artifactId>spiffy-integrationTest-plugin</artifactId> 
  34. <version>1.0</version> 
  35. <configuration> 
  36. <appserverHome>${appserver.home}</appserverHome> 
  37. </configuration> 
  38. </plugin> 
  39. ...  
  40. </plugins> 
  41. </build> 
  42. ...  
  43. </project> 

上面定義的pom.xml定義了兩個profile:不同的”env”參數值會激活不同的profile。當執行命令:

  1. mvn -Denv=dev-2 integration-test 

就會激活profile “appserverConfig-dev-2”

當執行命令:

  1. mvn -Denv=dev integration-test 

就會激活profile “appserverConfig-dev”

而當執行命令:

  1. mvn -Denv=production integration-test 

則運行失敗,因為沒有激活任何一個profile,因此無法解析${appserver.home}。

查看build time過程中使用了哪些Profiles

執行help plugin的active-profiles goal,使用命令:

  1. mvn help:active-profiles 

例子:

對于上面的例子,如果輸入命令:

 

  1. mvn help:active-profiles -Denv=dev 

則輸出的是:

  1. The following profiles are active:  
  2. - appserverConfig-dev (source: pom) 

如果有一個profile定義在settings.xml里并使用<activeProfile>激活,那么輸入命令:

  1. mvn help:active-profiles 

則輸出的是:

  1. The following profiles are active: 
  2. - appserverConfig (source: settings.xml)

如果輸入命令:

  1. mvn help:active-profiles -P appserverConfig-dev 

那么輸出的是:

  1. The following profiles are active:  
  2. - appserverConfig-dev (source: pom)  
  3. - appserverConfig (source: settings.xml) 

原文鏈接:http://wcp88888888.iteye.com/blog/1330692

【編輯推薦】

  1. Java富客戶端平臺JavaFX:創建框架實戰
  2. Java讀取WEB應用中的資源
  3. Java中運用數組的四種排序方法
  4. Java實現實用的ZIP壓縮與解壓
  5. Java防止SQL注入的幾個途徑
責任編輯:林師授 來源: wcp88888888的博客
相關推薦

2014-10-13 09:50:11

TomcatMaven

2009-09-07 07:38:05

Myeclipse項目

2022-05-19 14:59:32

Tomcat服務器開放

2022-02-18 07:27:01

Nest項目

2021-11-05 13:35:35

Spring BooK8SJava

2014-05-20 10:09:54

Intellij IDMaven Web

2011-12-28 14:32:17

eclipsetomcat

2023-12-10 21:03:01

TomcatMavenJava

2025-04-01 09:04:18

2011-11-30 15:18:06

JavaJBossJ2EE

2009-07-17 13:17:48

部署Eclipse R

2023-10-04 17:31:21

項目部署軟件包

2014-05-30 14:06:46

2018-11-07 09:01:13

Tomcat部署方式

2015-08-27 14:56:36

SSIS部署項目部署包部署

2009-07-31 09:41:39

ASP.NET MVCIIS版本變化

2024-12-20 14:03:33

2015-01-26 09:57:47

GradleMaven Centr

2024-04-01 09:01:20

NextjsAntd5.0管理后臺系統

2009-07-17 17:00:13

在JRuby下將RoR
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产视频线观看永久免费 | 亚洲免费在线观看视频 | 久久蜜桃av一区二区天堂 | 精品久久香蕉国产线看观看亚洲 | 日韩视频在线一区二区 | 精品伊人久久 | 精品视频一二区 | 国产精品国产a | 国产一区二区 | 91精品在线播放 | 欧美一二三区 | 在线观看视频福利 | 超碰日本| 一区二区在线视频 | 亚洲一区免费 | 欧美日产国产成人免费图片 | 中文字幕精品一区 | 免费观看av | 久草在线视频中文 | 偷拍亚洲色图 | 狠狠热视频 | 精品一区二区免费视频 | 亚洲精品在线播放 | 国产www.| 欧美激情一区二区三级高清视频 | 久久中文字幕视频 | 国产一区二区三区免费观看视频 | 中文字幕一区二区三区乱码在线 | 麻豆av在线免费观看 | 97精品国产97久久久久久免费 | 天天色天天色 | 日韩久草 | 午夜小视频在线播放 | 国产一区不卡 | 久久久精品高清 | 欧美一级视频 | 成人超碰在线 | 亚洲精品视频观看 | 成人h动漫精品一区二区器材 | 91视频播放 | 日韩一区二区三区在线 |