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

2023-12-10 21:03:01

TomcatMavenJava

2011-12-28 14:32:17

eclipsetomcat

2014-05-20 10:09:54

Intellij IDMaven Web

2011-11-30 15:18:06

JavaJBossJ2EE

2009-07-17 13:17:48

部署Eclipse R

2014-05-30 14:06:46

2023-10-04 17:31:21

项目部署软件包

2015-08-27 14:56:36

SSIS部署项目部署包部署

2009-07-31 09:41:39

ASP.NET MVCIIS版本变化

2018-11-07 09:01:13

Tomcat部署方式

2015-01-26 09:57:47

GradleMaven Centr

2024-04-01 09:01:20

NextjsAntd5.0管理后台系统

2024-01-10 11:56:51

SpringBootshell脚本命令

2021-06-26 08:15:21

Spring BooJar 项目

2009-07-17 17:00:13

在JRuby下将RoR
点赞
收藏

51CTO技术栈公众号