maven 學習之路之二(1)

上次我簡單講了maven的安裝和構建生命週期。
這一篇博客我將用實際項目來分享下maven整個構建生命週期的具體使用:
此次我將用maven作一個本身寫程序的一個模版程序。java

本身實現一個簡單的頁面登陸功能:mysql

一.準備項目環境:

mysql +idea +maven:git

1.建表:用於用戶登陸表:

CREATE TABLE userinfo (
id int(11) NOT NULL AUTO_INCREMENT,
name char(3) NOT NULL DEFAULT 」,
age tinyint(3) unsigned NOT NULL DEFAULT ‘0’,
email varchar(30) NOT NULL DEFAULT 」,
tel char(13) NOT NULL DEFAULT 」,
salary decimal(7,2) NOT NULL DEFAULT ‘0.00’,
riqi date NOT NULL DEFAULT ‘1000-01-01’,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
表建好以後github

2.搭建項目開發環境:

這裏我用mybaties 和springmvc +jsp 作個簡單的演示項目:web

1建立項目這裏寫圖片描述
next:

這裏寫圖片描述

next:這裏寫圖片描述

這是web項目結構基本搭建好啦。redis

2接下來配置maven pom 依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>login</groupId>
  <artifactId>login</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>login Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <!-- Spring框架版本  -->
    <spring.version>3.2.0.RELEASE</spring.version>
    <spring.rabbit.version>1.4.1.RELEASE</spring.rabbit.version>
    <!--  maven編譯字符集  -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  </properties>
  <dependencyManagement>
    <dependencies>
      <!-- 日誌工具  -->
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
      <!-- 另外一個日誌工具  -->
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
      </dependency>
      <!-- Protobuf協議支持  -->
      <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>2.5.0</version>
      </dependency>
      <dependency>
        <groupId>com.googlecode.protobuf-java-format</groupId>
        <artifactId>protobuf-java-format</artifactId>
        <version>1.2</version>
      </dependency>
      <dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-json</artifactId>
        <version>1.0.9</version>
      </dependency>
      <!-- Spring + Spring MVC框架支持  -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.4.2.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.0.0.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.8.1.RELEASE</version>
      </dependency>
      <!-- Java Servlet支持  -->
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
      </dependency>
      <!-- JSP支持  -->
      <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
      </dependency>
      <!-- JSP標籤語言支持  -->
      <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
      </dependency>
      <!-- 測試框架  -->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.4.9</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>com.github.springtestdbunit</groupId>
        <artifactId>spring-test-dbunit</artifactId>
        <version>1.0.1</version>
        <scope>test</scope>
      </dependency>
      <!-- http通信支持,使用內部通信模塊httpComm須要這個  -->
      <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.17.1</version>
      </dependency>
      <!-- 日期格式化  -->
      <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
      </dependency>
      <!-- 文件分塊上傳支持  -->
      <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
      </dependency>
      <!-- apache httpclient,用於http通信  -->
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.1</version>
      </dependency>
      <!-- xml解析工具  -->
      <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
      </dependency>
      <!-- 圖像編碼相關  -->
      <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
      </dependency>
      <!-- 工具類函數,例如字符串處理函數集StringUtils.isBlank等  -->
      <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
      </dependency>
      <!-- 阿里巴巴開源的數據庫鏈接工具  -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.9</version>
      </dependency>
      <!-- mysql數據庫jdbc鏈接  -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.29</version>
      </dependency>
      <!-- Mybatis框架  -->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.4</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.2</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.2</version>
      </dependency>
      <!-- json工具  -->
      <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
      </dependency>
      <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.1.42</version>
      </dependency>
      <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
      </dependency>
      <!-- 通用數據校驗工具,例如Email校驗等  -->
      <dependency>
        <groupId>commons-validator</groupId>
        <artifactId>commons-validator</artifactId>
        <version>1.4.0</version>
      </dependency>
      <!--  壓縮工具  -->
      <dependency>
        <groupId>org.zeroturnaround</groupId>
        <artifactId>zt-zip</artifactId>
        <version>1.8</version>
      </dependency>
      <!--  svn工具  -->
      <dependency>
        <groupId>org.tmatesoft.svnkit</groupId>
        <artifactId>svnkit</artifactId>
        <version>1.8.5</version>
      </dependency>
      <!--  ssh工具  -->
      <dependency>
        <groupId>ch.ethz.ganymed</groupId>
        <artifactId>ganymed-ssh2</artifactId>
        <version>build210</version>
      </dependency>
      <!--  tomcat部署工具  -->
      <dependency>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>common-tomcat-maven-plugin</artifactId>
        <version>2.2</version>
      </dependency>
      <!--  文件操做  -->
      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.2</version>
      </dependency>
      <!--  AOP依賴  -->
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.1</version>
      </dependency>
      <!--  sigar  -->
      <dependency>
        <groupId>org.fusesource</groupId>
        <artifactId>sigar</artifactId>
        <version>1.6.4</version>
      </dependency>
      <!-- Google提供的通用Utils,集成大量通用的工具類函數  -->
      <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
      </dependency>
      <dependency>
        <groupId>com.google.collections</groupId>
        <artifactId>google-collections</artifactId>
        <version>1.0</version>
      </dependency>
      <!--  quartz集羣任務框架  -->
      <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
      </dependency>
      <!--  redis客戶端  -->
      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.6.0</version>
      </dependency>
      <!--  Spring + RabbitMQ 集成框架  -->
      <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>${spring.rabbit.version}</version>
      </dependency>
      <!-- Java UUID Generator  -->
      <dependency>
        <groupId>org.safehaus.jug</groupId>
        <artifactId>jug</artifactId>
        <version>2.0.0</version>
        <classifier>lgpl</classifier>
      </dependency>
      <!-- Java映射工具  -->
      <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.9</version>
      </dependency>
      <!-- fastdfs  -->
      <dependency>
        <groupId>org.csource.fastdfs</groupId>
        <artifactId>fastdfs-client</artifactId>
        <version>1.24</version>
      </dependency>
      <!--  鏈接池  -->
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.0</version>
      </dependency>
      <!--  maven-jetty-plugin  -->
      <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.26</version>
      </dependency>
      <!--  dubbo  -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.4.fixed</version>
      </dependency>
      <!--  dubbo依賴  -->
      <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
      </dependency>
      <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
        <version>0.4</version>
      </dependency>
      <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty</artifactId>
        <version>6.1.26</version>
      </dependency>
      <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.bsf</groupId>
        <artifactId>bsf-api</artifactId>
        <version>3.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.3.3</version>
      </dependency>
      <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
      </dependency>
      <dependency>
        <groupId>com.netflix.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>1.1.16</version>
      </dependency>
      <dependency>
        <groupId>com.googlecode.xmemcached</groupId>
        <artifactId>xmemcached</artifactId>
        <version>1.3.6</version>
      </dependency>
      <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-simple</artifactId>
        <version>2.6.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>2.6.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.thrift</groupId>
        <artifactId>libthrift</artifactId>
        <version>0.8.0</version>
      </dependency>
      <dependency>
        <groupId>com.caucho</groupId>
        <artifactId>hessian</artifactId>
        <version>4.0.7</version>
      </dependency>
      <dependency>
        <groupId>org.jboss.netty</groupId>
        <artifactId>netty</artifactId>
        <version>3.2.5.Final</version>
      </dependency>
      <dependency>
        <groupId>org.apache.mina</groupId>
        <artifactId>mina-core</artifactId>
        <version>1.1.7</version>
      </dependency>
      <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-core</artifactId>
        <version>2.1.4</version>
      </dependency>
      <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jsp-2.1</artifactId>
        <version>6.1.14</version>
      </dependency>
      <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.15.0-GA</version>
      </dependency>
      <!--  ExcelUtils  -->
      <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
      </dependency>
      <!--  圖片處理工具  -->
      <dependency>
        <groupId>org.im4java</groupId>
        <artifactId>im4java</artifactId>
        <version>1.4.0</version>
      </dependency>
      <!--  中國銀聯工具  -->
      <dependency>
        <groupId>com.unionpay</groupId>
        <artifactId>netpayclient</artifactId>
        <version>2.5</version>
      </dependency>
      <!-- java擴展郵件包  -->
      <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
      </dependency>
      <!-- velocity模板引擎  -->
      <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
      </dependency>
      <!--  集成測試框架  -->
      <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.10</version>
      </dependency>
      <!--  web端功能性測試工具  -->
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>unpack</id>
                            <phase>package</phase>
                            <goals>
                                <goal>unpack</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>com.alibaba</groupId>
                                        <artifactId>dubbo</artifactId>
                                        <version>2.5.4.fixed</version>
                                        <outputDirectory>${project.build.directory}/dubbo</outputDirectory>
                                        <includes>META-INF/assembly/**</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptor>src/main/resources/assembly/assembly.xml</descriptor> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> </plugins> </pluginManagement> </build> <distributionManagement> <repository> <id>say-releases</id> <url> http://127.0.0.1:8081/nexus/content/repositories/releases/ </url> </repository> <snapshotRepository> <id>say-snapshots</id> <url> http://127.0.0.1:8081/nexus/content/repositories/snapshots/ </url> </snapshotRepository> </distributionManagement> <dependencyManagement></dependencyManagement> </project> 

上面的pom.xml 我就直接找我項目中實際用到的依賴了,就不一個一個的去挑選了。這裏稍微介紹下幾個地方:spring

<dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.0.0.RELEASE</version>
     </dependency>

這個是表示具體的依賴:
都會放在:sql

<dependencies>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.0.0.RELEASE</version>
         </dependency>
        ....
    </dependencies>
<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>unpack</id>
                            <phase>package</phase>
                            <goals>
                                <goal>unpack</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>com.alibaba</groupId>
                                        <artifactId>dubbo</artifactId>
                                        <version>2.5.4.fixed</version>
                                        <outputDirectory>${project.build.directory}/dubbo</outputDirectory>
                                        <includes>META-INF/assembly/**</includes>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptor>src/main/resources/assembly/assembly.xml</descriptor>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <forkMode>once</forkMode>
                        <argLine>-Dfile.encoding=UTF-8</argLine>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

bulid 標籤下配置的是構建項目的一些插件,這裏有編譯,和打包插件。數據庫

<distributionManagement>
        <repository>
            <id>say-releases</id>
            <url>
                http://127.0.0.1:8081/nexus/content/repositories/releases/
            </url>
        </repository>
        <snapshotRepository>
            <id>say-snapshots</id>
            <url>
                http://127.0.0.1:8081/nexus/content/repositories/snapshots/
            </url>
        </snapshotRepository>
    </distributionManagement>

這個表示倉庫的依賴,主要用於install 和deploy 。這裏先不加詳細介紹。
這裏就配置好pom.xml
這裏爲了偷懶介紹一款mybaties的插件叫作mybaities generator 這款插件可以生成mybaties的xml配置文件和接口以及數據庫的實體類。apache

相關文章
相關標籤/搜索