1. 程式人生 > >SSM框架配合Maven時,pom.xml配置詳解。

SSM框架配合Maven時,pom.xml配置詳解。

這篇部落格針對我這樣的小白。

一. pom.xml有什麼用?: 

我們知道maven(什麼是maven請移步https://www.cnblogs.com/whgk/p/7112560.html

可以給我們管理jar包和其他資源(jar包:別人寫好的輪子,我們負責呼叫)

那pom.xml裡面就書寫了,告訴了maven,當前專案需要用到哪些輪子

從pom.xml中我們可以看見我們只需要書寫我們需要的輪子的:

1.屬於哪個組織寫的(<groupId>

2.寫的啥(<artifactId>

3. 需要哪個版本(<version>

4.在你的專案中作用範圍(<scope>)比如寫test那就是這個jar包只在test那個包下使用,寫provided那就是開發階段使用,專案部署在tomcat上後,tomcat是沒有被provided修飾過的jar包的。當然是為了節約伺服器資源,不需要的jar包就不傳。

ps:至於maven去哪裡找資源,就是他的事了,你只需要你說明你要啥。當然你可以繼續瞭解本地倉庫與遠端倉庫。

然後可以看見一個jar包就如下配置:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

一個SSM框架下的Maven專案的pom.xml配置:

<?xml version="1.0" encoding="UTF-8"?><!--指定xml文件的版本和編碼方式-->
<!--project是所有pom.xml的根元素,
它還聲明瞭一些POM相關的名稱空間及xsd元素,
雖然這些屬性不是必須的,
但使用這些屬效能夠讓第三方工具
(如:IDE中的xml編輯器)幫助我們快速編輯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/xsd/maven-4.0.0.xsd">
    <!--指定了當前POM模型的版本,對於maven2及maven3來說,它只能是4.0.0。
    這段程式碼中最重要的是包含groupId,artifactId和version的三行。
    這三個元素定義了一個專案基本的座標,在maven的世界,
    任何的jar,pom或者war都是以基於這些基本的座標進行區分的。-->
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yun</groupId><!--填寫你的組織名例如-->
    <artifactId>test</artifactId><!--專案名字-->
    <version>1.0-SNAPSHOT</version><!--專案的版本 snapshot為快照意思,代表當前為測試版,
                                        開發版,相對的為release,意味發行版本,代表穩定版-->
    <packaging>war</packaging><!--問你專案需要打成什麼包?pom jar war?一般javaWeb專案填war-->
    <name>ssm Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <dependencies>
        <!--引入junit,做單元測試用,@Test註解需要它-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--set get方法使用註解開發時需要它-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--引入servlet-api 比如說HttpServletRequest和HttpServletResponse等物件,這些物件都要靠這個jar包才能使用-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <!--jsp的依賴-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!--引入jstl,書寫jstl時要用到-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--引入mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <!--引入mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <!--引入spring-->
        <!--提供對AspectJ的支援-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--這個jar 檔案包含在應用中使用Spring 的AOP 特性時所需的類和原始碼級元資料支援。使用基於AOP 的Spring特性,
        如宣告型事務管理(Declarative Transaction Management),也要在應用裡包含這個jar包。-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--這個jar 檔案包含對Spring 對JDBC 資料訪問進行封裝的所有類。-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--基於tx的事務管理需要-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--這個jar 檔案是所有應用都要用到的,它包含訪問配置檔案、
        建立和管理bean 以及進行Inversion of Control / Dependency Injection(IoC/DI)操作相關的所有類。
        如果應用只需基本的IoC/DI 支援,引入spring-core.jar 及spring-beans.jar 檔案就可以了。
        外部依賴spring-core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--這個jar 檔案包含支援UI模版(Velocity,FreeMarker,JasperReports),
        郵件服務,指令碼服務(JRuby),快取Cache(EHCache),
        任務計劃Scheduling(uartz)方面的類。
        外部依賴spring-context, (spring-jdbc, Velocity, FreeMarker, JasperReports, BSH, Groovy, JRuby, Quartz, EHCache)-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--spring表示式語言-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--基礎web功能,如檔案上傳-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!--spring測試,提供junit與mock測試功能-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <!--引入springmvc,mvc實現-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <!--fastjson,把物件和json格式互轉換時需要用到-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        <!--ueditor,一個富文字編輯器-->
        <dependency>
            <groupId>com.gitee.qdbp.thirdparty</groupId>
            <artifactId>ueditor</artifactId>
            <version>1.4.3.3</version>
        </dependency>
        <!--jackson,把物件和json格式互轉換時需要用到-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.7</version>
        </dependency>
        <!--dbcp,還記得資料庫連線池麼??還有C3P0,druid這些-->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!--jedis,Jedis是Redis官方推薦的Java連線開發工具。-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.2</version>
        </dependency>
        <!--commons-lang3-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <!--commons-fileupload-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <!--引入log4j,生成日誌需要這個,log for java ,4是 for的近音-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>

        <!--引入mybatis和spring-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>ssm</finalName><!--該專案名的時候改這裡-->
    </build>
</project>

 

 

 

 

 

參考部落格:

http://blog.sina.com.cn/s/blog_534f69a001010lpv.html

https://blog.csdn.net/u011781521/article/details/53760225

https://www.cnblogs.com/whgk/p/7112560.html

https://blog.csdn.net/qq277798882/article/details/53790199

https://blog.csdn.net/qq277798882/article/details/53790199