zoukankan      html  css  js  c++  java
  • maven 构建的 webapp 基本配置

    一、核心依赖

    webapp 的依赖,大致分为三部分:基本的 spring 组件依赖、日志及测试组件、web组件依赖等。大致为:

    <dependencies>
        <!-- 1.base spring dependency -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${version.springframework}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${version.springframework}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${version.springframework}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${version.springframework}</version>
        </dependency>
        
        <!-- 2.base logger and test dependency -->
        <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
          <version>${version.log4j}</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${version.slf4j}</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>${version.slf4j}</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${version.junit}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${version.springframework}</version>
          <scope>test</scope>
        </dependency>
        
        <!-- 3.base web dependency -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${version.springframework}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${version.springframework}</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>${version.javax.servlet.api}</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>javax.servlet.jsp-api</artifactId>
          <version>${version.javax.servlet.jsp.api}</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>com.tutianer</groupId>
          <artifactId>common</artifactId>
          <version>${version.tutianer.common}</version>
        </dependency>
        
      </dependencies>
    View Code

    二、项目构建配置

    项目构建常见的配置项主要是设置jdk版本和资源过滤。如下:

    <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                <source>1.8</source>
                <target>1.8</target>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-resources-plugin</artifactId>
              <configuration>
                <resources>
                  <resource>
                    <directory>${project.basedir}/src/main/resources</directory>
                    <filtering>true</filtering>
                  </resource>
                </resources>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-maven-plugin</artifactId>
              <version>${version.jetty}</version>
              <configuration>
                <httpConnector>
                  <port>8003</port>
                </httpConnector>
              </configuration>
            </plugin>
          </plugins>
        </pluginManagement>
        
        <finalName>v1</finalName>
      </build>
    View Code

    其中finaleName表示 webapp 打包后的名字,作为默认的 web 项目,设置为 ROOT 可以直接在 tomcat 中使用。

    三、属性配置

    <profiles>
        <profile>
              <id>dev</id>
              <activation>
                  <activeByDefault>true</activeByDefault>
              </activation>
              <properties>
                <config.logger.level>INFO</config.logger.level>
                <config.redis.server>127.0.0.1</config.redis.server>
                <config.redis.port>6379</config.redis.port>
                <config.redis.prefixKey>local_</config.redis.prefixKey>
                <config.db.driverClassName>com.mysql.jdbc.Driver</config.db.driverClassName>
                <config.db.url>jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8&amp;useUnicode=true&amp;useSSL=false</config.db.url>
                <config.db.username>root</config.db.username>
                <config.db.password>123456</config.db.password>
                <config.domain>http://localhost:8002</config.domain>
              </properties>
          </profile>
          <profile>
              <id>prod</id>
              <properties>
                <config.logger.level>INFO</config.logger.level>
                <config.redis.server>127.0.0.1</config.redis.server>
                <config.redis.port>6382</config.redis.port>
                <config.redis.prefixKey>prod_</config.redis.prefixKey>
                <config.db.driverClassName>com.mysql.jdbc.Driver</config.db.driverClassName>
                <config.db.url>jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8&amp;useUnicode=true&amp;useSSL=false</config.db.url>
                <config.db.username>username</config.db.username>
                <config.db.password>pwd</config.db.password>
                <config.domain>http://domain</config.domain>
              </properties>
          </profile>
      </profiles>
    View Code

    在常规开发中,一个常见问题是在本地开发、测试、生产等几个环境中来回切换需要手工修改各项配置,而 maven 的属性配置则很好的解决了这个问题。当项目构建配置中使用了maven-resources-plugin插件时,在测试或者打包时,会自动将属性中配置的值替换到maven-resources-plugin所指定的资源目录中。

    需要注意的是,其中的数据库配置要用&amp;做转义;而该项直接在 xml 文件中配置则不需要转义。

    这里默认激活的是activeByDefault所在的配置节,在打包或者测试时激活其他配置需要加参数P 来指定激活的配置项:-Pprod 

    四、web 版本配置

    由于 maven 的 archvetype 略微陈旧,创建的 webapp 会被设置为2.3版本,可以在配置文件中修改为2.5。配置文件是web 项目.setting 目录中的org.eclipse.wst.common.project.facet.core.xml 文件,将

    <installed facet="jst.web" version="2.3"/>

    修改为:

    <installed facet="jst.web" version="2.5"/>

    五、其余配置

    其余诸如spring、web.xml 等配置信息。

  • 相关阅读:
    English,The Da Vinci Code, Chapter 23
    python,meatobject
    English,The Da Vinci Code, Chapter 22
    English,The Da Vinci Code, Chapter 21
    English,The Da Vinci Code, Chapter 20
    English,The Da Vinci Code, Chapter 19
    python,xml,ELement Tree
    English,The Da Vinci Code, Chapter 18
    English,The Da Vinci Code, Chapter 17
    English,The Da Vinci Code, Chapter 16
  • 原文地址:https://www.cnblogs.com/bashenandi/p/8504242.html
Copyright © 2011-2022 走看看