maven 構建的 webapp 基本配置

來源:互聯網
上載者:User

標籤:127.0.0.1   enc   closed   set   local   tomcat   pac   ase   gif   

一、核心依賴

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 等配置資訊。

 

maven 構建的 webapp 基本配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.