但是也遇到了很多问题,下面记录一下Web项目部署到Tomcat下的问题

1、普通的WEB项目,就是虽然是用maven搭建的,但是没有使用profiles.xml文件来配置参数。这样的项目可以通过以下的方式进行部署:

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

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

2)修改tomcat目录下的conf目录下的server.xml文件,在Host标签之间添加如下一句话:

<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包括:
<repositories> 
<pluginRepositories> 
<dependencies> 
<plugins> 
<properties> (not actually available in the main POM, but used behind the scenes) 
<modules> 
<reporting> 
<dependencyManagement> 
<distributionManagement> 
a subset of the <build> element, which consists of: 
<defaultGoal> 
<resources> 
<testResources> 
<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(之间用逗号连接)。如:
mvn groupId:artifactId:goal -P profileId-1,profileId-2
 
2)                        通过在settings.xml里设置<activeProfiles> element来激活(当然<profiles>也必须在settings.xml里定义)
<settings>
 ...
          <profiles>
 <profile>
    <id>profile1</id>
    ...
 </profile>
          </profiles>
 
 <activeProfiles>
    <activeProfile>profile-1</activeProfile>
 </activeProfiles>
 ...
</settings>
 
            列在<activeProfiles>里的profiles list会在每一个project执行时被激活
 
3)Profiles还可以基于detect到的build environment 的state来自动激活,而不需要象上面2种方式显式激活。这只需要在profile定义时使用<activation> element。如:
<profiles>
 <profile>
    <activation>
      <jdk>1.4</jdk>
    </activation>
    ...
 </profile>
</profiles>
上面的代码表示:如果JDK version start with 1.4 (eg. "1.4.0_08", "1.4.2_07", "1.4"),该profile会被激活
 
<profiles>
 <profile>
    <activation>
      <property>
        <name>debug</name>
      </property>
    </activation>
    ...
 </profile>
</profiles>
上面的代码表示:如果存在system propertie “debug”,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal –Ddebug 
 
<profiles>
 <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
 </profile>
</profiles>
上面的代码表示:如果存在system propertie “environment”的值为test,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal -Denvironment=test

4)Profiles还可以基于OS setting来自动激活
<profiles>
 <profile>
    <activation>
      <os>
      <name>Windows XP</name>
      <family>Windows</family>
      <arch>x86</arch>
      <version>5.1.2600</version>
    </os>
        </activation>
 ...
 </profile>
</profiles>
            上面的代码表示:如果OS为windows xp,该profile会被激活
 
5)根据某个file不存在而激活profile。例如下面定义的profile是在target/generated-sources/axistools/wsdl2java/org/apache/maven不存在时激活
<profiles>
 <profile>
    <activation>
      <file>
        <missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
      </file>
    </activation>
    ...
 </profile>
</profiles>
 
 
使用Profiles时要注意的2个问题
 
第一、external properties
 
不是定义在pom.xml里的properties都称为external properties。举例说明最明了:
pom.xml:
<project>
 ...
 <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home}</appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
 </build>
 ...
</project>
 
~/.m2/settings.xml
<settings>
 ...
 <profiles>
    <profile>
      <id>appserverConfig</id>
      <properties>
        <appserver.home>/path/to/appserver</appserver.home>
      </properties>
    </profile>
 </profiles>
 
 <activeProfiles>
    <activeProfile>appserverConfig</activeProfile>
 </activeProfiles>
 ...
</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:
<project>
 ...
 <profiles>
    <profile>
      <id>appserverConfig-dev</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver</appserver.home>
      </properties>
    </profile>
 
    <profile>
      <id>appserverConfig-dev-2</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev-2</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver2</appserver.home>
      </properties>
    </profile>
 </profiles>
 
 <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home}</appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
 </build>
 ...
</project>
 
上面定义的pom.xml定义了两个profile:不同的”env”参数值会激活不同的profile。当执行命令:
mvn -Denv=dev-2 integration-test
就会激活profile “appserverConfig-dev-2”
 
当执行命令:
mvn -Denv=dev integration-test
就会激活profile “appserverConfig-dev”
 
而当执行命令:
mvn -Denv=production integration-test
则运行失败,因为没有激活任何一个profile,因此无法解析${appserver.home}。
 
 
查看build time过程中使用了哪些Profiles
执行help plugin的active-profiles goal,使用命令:
            mvn help:active-profiles
 
例子:
对于上面的例子,如果输入命令:
            mvn help:active-profiles -Denv=dev
则输出的是:
The following profiles are active:
 
 - appserverConfig-dev (source: pom)
 
如果有一个profile定义在settings.xml里并使用<activeProfile>激活,那么输入命令:
            mvn help:active-profiles
则输出的是:
The following profiles are active:
 
 - appserverConfig (source: settings.xml)
 
 
如果输入命令:
            mvn help:active-profiles -P appserverConfig-dev
那么输出的是:
The following profiles are active:
 
 - appserverConfig-dev (source: pom)
 - appserverConfig (source: settings.xml)

http://www.jb51.net/article/115084.htm

Maven Web项目部署到Tomcat下问题的更多相关文章

  1. idea 创建maven web项目部署在 tomcat maven plugin中

    前提:1.安装jdk,多数系统使用jdk1.8.xxx,因此选择下载此版本的居多 2.安装Maven 3.部署到tomcat我们可以有两种方式,一种是利用tomcat插件来进行部署,另一种是下载tom ...

  2. Eclipse下编写的web项目部署到tomcat下

    之前都是用myeclipse编写web项目,编写好然后在myclipse上配置的tomcat下的webapps文件想项目复制到其他tomcat下就能运行了. 最近学习jquery,将eclipse编写 ...

  3. 在eclipse中maven web项目部署到tomcat,访问不了

    修改eclipse中tomcat发布路径后,能正常访问

  4. Eclipse导入git上的maven web项目 部署 - lpshou

    http://www.tuicool.com/articles/fqm2Qf   推酷 文章 微博 主题 站点 活动 应用 周刊 登录   Eclipse导入git上的maven web项目 部署 - ...

  5. 2016.6.21 将Eclipse中项目部署到tomcat下

    新建的web项目,各种都配置好,选择run on server之后,发现运行失败,并不能访问需要的网址.而脱离eclipse,将生成的war文件直接放到tomcat的webapp下时,可以正常访问.所 ...

  6. eclipse中将web项目部署到tomcat

    eclipse中将web项目部署到tomcat. myeclipse部署WEB项目到tomcat比较方便,但eclipse貌似默认是不会替你将web自动部署到tomcat下的.你Run as该web项 ...

  7. eclipse中maven web项目部署时缺少classes文件或者resources文件

    写这篇博客的原因 问题描述 昨天发现eclipse中maven web项目部署时缺少classes文件或者resources文件 本来以为是很常见的原因, 依次检查"Java Build P ...

  8. 将eclipse上的web项目部署到Tomcat服务器上经验总结

    1.  将Tomcat插件添加到eclipse上 Window --> Preferences --> Server --> Runtime Environment --> A ...

  9. Eclipse导入git上的maven web项目 部署

    1 Eclipse中导入Git的maven项目 方法1: (1)首先当然是拉代码. 在Eclipse里面有个Git Repositories Exploring.就是Git仓库,clone a git ...

随机推荐

  1. Android中使用adb访问SQLite的方法

    (1)打开命令提示符,输入:adb,按回车,如果得到下面一大堆命令说明(如图 1),表示adb的配置是成功的,如果提示"不是内部或外部命令,也不是可运行的程序或批处理文件",那么需 ...

  2. Kindel资源去哪里找

      .kindle这么多资源,我到底去哪里找呢?哎哟,看看这个吧:(1).子午书简:http://book.zi5.me/,好像最近挂了,所以去作者介绍的那个地方下载吧(2).mlook:mLook ...

  3. llvm 编译

    https://llvm.org/docs/GettingStarted.html https://clang.llvm.org/get_started.html https://llvm.org/d ...

  4. 开发指南专题五:JEECG微云高速开发平台代码生成器

    开发指南专题五:JEECG微云高速开发平台代码生成器 1.1. Maven开发环境搭建 在搭建jeecg的maven开发环境之前,须要先配置好本机的maven环境,并在eclipse中安装好m2ecl ...

  5. Newtonsoft.Json2.0下面序列化和反序列化

    序列化 string xml = JavaScriptConvert.SerializeObject(dataTable); 反序列化 JavaScriptConvert.DeserializeObj ...

  6. Linux 命令 及 简单操作 学习

    众所周知,linux命令很多很多,但是,请不用担心,相信你自己不断的积累,终有一天你和你和小伙伴都会为你惊呆的...... 废话不多说,那,什么时候动手????---------现在,马上..... ...

  7. 如何获取ubuntu源码包里面的源码?

    ubuntu 源仓库说明 1.在获取源码包之前,确保在软件源配置文件/etc/apt/sources.list中添加了deb-src项 vim /etc/apt/sources.list # # de ...

  8. 【Java】数组不能通过toString方法转为字符串

    java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的.object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toStrin ...

  9. java Serializable和Externalizable序列化反序列化详解(转载)

    一.什么是序列化? “对象序列化”(Object Serialization)是 Java1.1就开始有的特性. 简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存 ...

  10. Appium 输入 & 符号,实际输入&-

    2018-10-11 12:27:12:178 - [debug] [MJSONWP] Calling AppiumDriver.setValue() with args: [["& ...