SSH项目搭建(四)——Maven的pom.xml配置
史上最全的maven的pom.xml文件详解:
https://www.cnblogs.com/qq765065332/p/9238135.html
下面的节点有不理解是啥意思的可以到上面链接的文章里搜索对照
打开父项目的pom.xml
整个pom文件太大,我们一点一点添吧,不然很乱,很难讲清楚。
1、初始状态
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>tsglxt</groupId> 4 <artifactId>tsglxt</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <!-- 打包方式 --> 7 <packaging>pom</packaging> 8 9 <!-- 引入的模块 --> 10 <modules> 11 <module>tsglxt-common</module> 12 <module>tsglxt-dao</module> 13 <module>tsglxt-service</module> 14 <module>tsglxt-web</module> 15 </modules> 16 </project>
2、添加节点
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>tsglxt</groupId> 6 <artifactId>tsglxt</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>pom</packaging> 9 10 <!-- 以值替代名称,Properties可以在整个POM中使用,也可以作为触发条件。格式是<name>value</name>。 --> 11 <properties> 12 13 </properties> 14 15 <!-- 构建项目需要的信息 --> 16 <build> 17 18 </build> 19 20 <!-- 继承自该项目的所有子项目的默认依赖信息。这部分的依赖信息不会被立即解析,而是当子项目声明一个依赖(必须描述group ID和artifact ID信息),如果group ID和artifact ID以外的一些信息没有描述,则通过group ID和artifact ID匹配到这里的依赖,并使用这里的依赖信息。 --> 21 <dependencyManagement> 22 <!-- 项目引入插件所需要的额外依赖 --> 23 <dependencies> 24 25 </dependencies> 26 </dependencyManagement> 27 28 <!-- 项目引入插件所需要的额外依赖 --> 29 <dependencies> 30 31 </dependencies> 32 33 <!-- 项目分发信息,在执行mvn deploy后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件部署到远程仓库。 --> 34 <distributionManagement> 35 36 </distributionManagement> 37 38 39 <modules> 40 <module>tsglxt-common</module> 41 <module>tsglxt-dao</module> 42 <module>tsglxt-service</module> 43 <module>tsglxt-web</module> 44 </modules> 45 </project>
关于dependencyManagement里的dependencies和不在dependencyManagement里的dependencies的区别:
https://blog.csdn.net/OiteBody/article/details/70882940
3、添加Maven plugin (看准第一个节点,然后在2中找到对应的节点,把里边的内容复制过去。后面的步骤也是这样)
1 <properties> 2 <!-- Maven plugin --> 3 <maven-source-plugin.version>2.0.3</maven-source-plugin.version> 4 <maven-compiler-plugin.version>2.0.2</maven-compiler-plugin.version> 5 <maven-assembly-plugin.version>2.2.1</maven-assembly-plugin.version> 6 <maven-deploy-plugin.version>2.4</maven-deploy-plugin.version> 7 <maven-war-plugin.version>2.1.1</maven-war-plugin.version> 8 <maven-version-plugin.version>2.1</maven-version-plugin.version> 9 <maven-jar-plugin.version>2.3.2</maven-jar-plugin.version> 10 <maven-archetype-plugin.version>2.2</maven-archetype-plugin.version> 11 12 </properties> 13 14 <build> 15 <pluginManagement> 16 <plugins> 17 <plugin> 18 <groupId>org.apache.maven.plugins</groupId> 19 <artifactId>maven-jar-plugin</artifactId> 20 <version>${maven-jar-plugin.version}</version> 21 </plugin> 22 <plugin> 23 <groupId>org.apache.maven.plugins</groupId> 24 <artifactId>maven-archetype-plugin</artifactId> 25 <version>${maven-archetype-plugin.version}</version> 26 </plugin> 27 <plugin> 28 <groupId>org.apache.maven.plugins</groupId> 29 <artifactId>maven-war-plugin</artifactId> 30 <version>${maven-war-plugin.version}</version> 31 </plugin> 32 <plugin> 33 <groupId>org.codehaus.mojo</groupId> 34 <artifactId>versions-maven-plugin</artifactId> 35 <version>${maven-version-plugin.version}</version> 36 </plugin> 37 <plugin> 38 <groupId>org.apache.maven.plugins</groupId> 39 <artifactId>maven-source-plugin</artifactId> 40 <version>${maven-source-plugin.version}</version> 41 <executions> 42 <execution> 43 <id>attach-sources</id> 44 <goals> 45 <goal>jar</goal> 46 </goals> 47 </execution> 48 </executions> 49 </plugin> 50 <plugin> 51 <groupId>org.apache.maven.plugins</groupId> 52 <artifactId>maven-compiler-plugin</artifactId> 53 <version>${maven-compiler-plugin.version}</version> 54 <configuration> 55 <encoding>${source.encoding}</encoding> 56 <source>${java.version}</source> 57 <target>${java.version}</target> 58 </configuration> 59 </plugin> 60 <plugin> 61 <groupId>org.apache.maven.plugins</groupId> 62 <artifactId>maven-assembly-plugin</artifactId> 63 <version>${maven-assembly-plugin.version}</version> 64 <configuration> 65 <appendAssemblyId>false</appendAssemblyId> 66 <descriptors> 67 <descriptor>src/main/resources/assembly.xml</descriptor> 68 </descriptors> 69 </configuration> 70 <executions> 71 <execution> 72 <id>make-assembly</id> 73 <goals> 74 <goal>single</goal> 75 </goals> 76 </execution> 77 </executions> 78 </plugin> 79 </plugins> 80 </pluginManagement> 81 </build>
4、添加apche common
1 <dependencyManagement> 2 <!-- 项目引入插件所需要的额外依赖 --> 3 <dependencies> 4 <!-- Apache commons --> 5 <dependency> 6 <groupId>commons-lang</groupId> 7 <artifactId>commons-lang</artifactId> 8 <version>${commons-lang.version}</version> 9 </dependency> 10 <dependency> 11 <groupId>commons-beanutils</groupId> 12 <artifactId>commons-beanutils</artifactId> 13 <version>${commons-beanutils.version}</version> 14 </dependency> 15 <dependency> 16 <groupId>commons-collections</groupId> 17 <artifactId>commons-collections</artifactId> 18 <version>${commons-collections.version}</version> 19 </dependency> 20 <dependency> 21 <groupId>commons-httpclient</groupId> 22 <artifactId>commons-httpclient</artifactId> 23 <version>${commons-httpclient.version}</version> 24 </dependency> 25 <dependency> 26 <groupId>commons-net</groupId> 27 <artifactId>commons-net</artifactId> 28 <version>${commons-net.version}</version> 29 </dependency> 30 <dependency> 31 <groupId>commons-net</groupId> 32 <artifactId>commons-net</artifactId> 33 <classifier>sources</classifier> 34 <version>${commons-net.version}</version> 35 </dependency> 36 <dependency> 37 <groupId>commons-dbcp</groupId> 38 <artifactId>commons-dbcp</artifactId> 39 <version>${commons-dbcp.version}</version> 40 </dependency> 41 <dependency> 42 <groupId>commons-pool</groupId> 43 <artifactId>commons-pool</artifactId> 44 <version>${commons-pool.version}</version> 45 </dependency> 46 <dependency> 47 <groupId>commons-fileupload</groupId> 48 <artifactId>commons-fileupload</artifactId> 49 <version>${commons-fileupload.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>net.sf.ehcache</groupId> 53 <artifactId>ehcache-core</artifactId> 54 <version>${ehcacheVersion}</version> 55 </dependency> 56 57 </dependencies> 58 </dependencyManagement>
5、添加Spring,Struts,Hibernate
1 <properties> 2 <!-- Spring --> 3 <spring.version>3.2.9.RELEASE</spring.version> 4 <spring.mock.version>2.0-rc2</spring.mock.version> 5 6 <!-- Struts, Hibernate --> 7 <struts.version>2.3.32</struts.version> 8 <struts.test.version>2.2.1.1</struts.test.version> 9 <hibernate.version>3.6.10.Final</hibernate.version> 10 <hibernate.annotation.version>3.5.6-Final</hibernate.annotation.version> 11 <hibernate.search>3.4.2.Final</hibernate.search> 12 <aspectjweaver.version>1.6.11</aspectjweaver.version> 13 </properties> 14 15 <dependencyManagement> 16 <!-- 项目引入插件所需要的额外依赖 --> 17 <dependencies> 18 <!-- Spring --> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-core</artifactId> 22 <version>${spring.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-beans</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-context</artifactId> 32 <version>${spring.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-context-support</artifactId> 37 <version>${spring.version}</version> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-orm</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-test</artifactId> 47 <version>${spring.version}</version> 48 <type>jar</type> 49 <scope>test</scope> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-web</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-webmvc</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 62 <!-- Struts --> 63 <dependency> 64 <groupId>org.apache.struts</groupId> 65 <artifactId>struts2-core</artifactId> 66 <version>${struts.version}</version> 67 <exclusions> 68 <exclusion> 69 <artifactId>tools</artifactId> 70 <groupId>com.sun</groupId> 71 </exclusion> 72 </exclusions> 73 </dependency> 74 <dependency> 75 <groupId>org.apache.struts</groupId> 76 <artifactId>struts2-junit-plugin</artifactId> 77 <version>${struts.test.version}</version> 78 </dependency> 79 <dependency> 80 <groupId>org.apache.struts</groupId> 81 <artifactId>struts2-json-plugin</artifactId> 82 <version>${struts.version}</version> 83 </dependency> 84 <dependency> 85 <groupId>org.apache.struts</groupId> 86 <artifactId>struts2-spring-plugin</artifactId> 87 <version>${struts.version}</version> 88 </dependency> 89 <dependency> 90 <groupId>org.apache.struts</groupId> 91 <artifactId>struts2-convention-plugin</artifactId> 92 <version>${struts.version}</version> 93 </dependency> 94 <dependency> 95 <groupId>org.aspectj</groupId> 96 <artifactId>aspectjweaver</artifactId> 97 <version>${aspectjweaver.version}</version> 98 </dependency> 99 100 <!-- hibernate --> 101 <dependency> 102 <groupId>org.hibernate</groupId> 103 <artifactId>hibernate-core</artifactId> 104 <version>${hibernate.version}</version> 105 </dependency> 106 <dependency> 107 <groupId>org.hibernate</groupId> 108 <artifactId>hibernate-c3p0</artifactId> 109 <version>${hibernate.version}</version> 110 </dependency> 111 <dependency> 112 <groupId>org.hibernate</groupId> 113 <artifactId>hibernate-ehcache</artifactId> 114 <version>${hibernate.version}</version> 115 </dependency> 116 <dependency> 117 <groupId>org.hibernate</groupId> 118 <artifactId>hibernate-search</artifactId> 119 <version>${hibernate.search}</version> 120 </dependency> 121 <dependency> 122 <groupId>org.hibernate</groupId> 123 <artifactId>hibernate-entitymanager</artifactId> 124 <version>${hibernate.version}</version> 125 </dependency> 126 <dependency> 127 <groupId>org.hibernate</groupId> 128 <artifactId>hibernate-validator</artifactId> 129 <version>4.2.0.Final</version> 130 </dependency> 131 </dependencies> 132 </dependencyManagement>
6、其他配置
1 <properties> 2 <!-- 统一编码格式 --> 3 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 4 <toolkit.version>0.0.8-SNAPSHOT</toolkit.version> 5 <source.encoding>UTF-8</source.encoding> 6 <!-- java --> 7 <java.version>1.8</java.version> 8 9 <!-- log4j和slf4j --> 10 <slf4j.version>1.6.2</slf4j.version> 11 <log4j.version>1.2.14</log4j.version> 12 13 <!-- javaee --> 14 <javaee.version>6.0-5</javaee.version> 15 <servlet.version>3.0-alpha-1</servlet.version> 16 <jsp.version>2.2</jsp.version> 17 <guava.version>14.0.1</guava.version> 18 <gson.version>2.2.4</gson.version> 19 <json.version>20090211</json.version> 20 <junit.version>4.8.1</junit.version> 21 22 <mysql-connector-java.version>5.1.29</mysql-connector-java.version> 23 <oracle-connector-java.version>11.2.0.1.0</oracle-connector-java.version> 24 25 <!-- HTML tags --> 26 <displaytag.version>1.2</displaytag.version> 27 <servlet.jstl.version>1.2</servlet.jstl.version> 28 <apache.standard.taglib.version>1.1.2</apache.standard.taglib.version> 29 <sitemesh.version>2.4.2</sitemesh.version> 30 </properties> 31 32 <dependencyManagement> 33 <!-- 项目引入插件所需要的额外依赖 --> 34 <dependencies> 35 <!-- toolkit --> 36 <dependency> 37 <groupId>common.toolkit</groupId> 38 <artifactId>common-toolkit</artifactId> 39 <version>${toolkit.version}</version> 40 <exclusions> 41 <exclusion> 42 <groupId>org.apache.struts</groupId> 43 <artifactId>struts2-core</artifactId> 44 </exclusion> 45 </exclusions> 46 </dependency> 47 <dependency> 48 <groupId>common.toolkit</groupId> 49 <artifactId>common-toolkit</artifactId> 50 <version>${toolkit.version}</version> 51 <classifier>sources</classifier> 52 </dependency> 53 54 <!-- Log --> 55 <dependency> 56 <groupId>commons-logging</groupId> 57 <artifactId>commons-logging</artifactId> 58 <version>${commons-logging.version}</version> 59 </dependency> 60 <dependency> 61 <groupId>log4j</groupId> 62 <artifactId>log4j</artifactId> 63 <version>${log4j.version}</version> 64 </dependency> 65 <dependency> 66 <groupId>org.slf4j</groupId> 67 <artifactId>slf4j-log4j12</artifactId> 68 <version>${slf4j.version}</version> 69 </dependency> 70 <dependency> 71 <groupId>org.slf4j</groupId> 72 <artifactId>slf4j-api</artifactId> 73 <version>${slf4j.version}</version> 74 </dependency> 75 76 <!-- Servlet --> 77 <dependency> 78 <groupId>org.apache.openejb</groupId> 79 <artifactId>javaee-api</artifactId> 80 <version>${javaee.version}</version> 81 </dependency> 82 <dependency> 83 <groupId>javax.servlet</groupId> 84 <artifactId>servlet-api</artifactId> 85 <version>${servlet.version}</version> 86 </dependency> 87 <dependency> 88 <groupId>javax.servlet.jsp</groupId> 89 <artifactId>jsp-api</artifactId> 90 <version>${jsp.version}</version> 91 </dependency> 92 93 <!-- Guava --> 94 <dependency> 95 <groupId>com.google.guava</groupId> 96 <artifactId>guava</artifactId> 97 <version>${guava.version}</version> 98 </dependency> 99 100 <!-- JSON --> 101 <dependency> 102 <groupId>org.json</groupId> 103 <artifactId>json</artifactId> 104 <version>${json.version}</version> 105 </dependency> 106 <dependency> 107 <groupId>com.google.code.gson</groupId> 108 <artifactId>gson</artifactId> 109 <version>${gson.version}</version> 110 </dependency> 111 112 <!-- Test --> 113 <dependency> 114 <groupId>junit</groupId> 115 <artifactId>junit</artifactId> 116 <version>${junit.version}</version> 117 <scope>test</scope> 118 </dependency> 119 120 <!-- DataBase Driver --> 121 <dependency> 122 <groupId>mysql</groupId> 123 <artifactId>mysql-connector-java</artifactId> 124 <version>${mysql-connector-java.version}</version> 125 </dependency> 126 <dependency> 127 <groupId>com.oracle</groupId> 128 <artifactId>ojdbc6</artifactId> 129 <version>${oracle-connector-java.version}</version> 130 </dependency> 131 132 <!-- Tags --> 133 <dependency> 134 <groupId>displaytag</groupId> 135 <artifactId>displaytag</artifactId> 136 <version>${displaytag.version}</version> 137 </dependency> 138 <dependency> 139 <groupId>javax.servlet</groupId> 140 <artifactId>jstl</artifactId> 141 <version>${servlet.jstl.version}</version> 142 </dependency> 143 <dependency> 144 <groupId>taglibs</groupId> 145 <artifactId>standard</artifactId> 146 <version>${apache.standard.taglib.version}</version> 147 </dependency> 148 <dependency> 149 <groupId>opensymphony</groupId> 150 <artifactId>sitemesh</artifactId> 151 <version>${sitemesh.version}</version> 152 </dependency> 153 </dependencies> 154 </dependencyManagement>
7、根据各自项目需要,自行添加别的maven配置信息
打开dao层的pom.xml
dao层需要引用common层的po、vo等实体
1 <dependencies> 2 <dependency> 3 <groupId>tsglxt</groupId> 4 <artifactId>tsglxt-common</artifactId> 5 <version>${project.version}</version> 6 </dependency> 7 </dependencies>
打开service层的pom.xml
service层需要引用dao层和common层,因为dao层引入过common层的xml,引入dao层后common层也自动引入了
1 <dependencies> 2 <dependency> 3 <groupId>tsglxt</groupId> 4 <artifactId>tsglxt-dao</artifactId> 5 <version>${project.version}</version> 6 </dependency> 7 </dependencies>
打开web层的pom.xml
web层(action层)需要引用service层,同上同理
web层还需要引入jstl等jar包……
需要添加一个build节点,描述项目相关的所有资源路径列表。
1 <dependencies> 2 <dependency> 3 <groupId>tsglxt</groupId> 4 <artifactId>tsglxt-service</artifactId> 5 <version>${project.version}</version> 6 </dependency> 7 8 <dependency> 9 <groupId>nl.captcha</groupId> 10 <artifactId>simplecaptcha</artifactId> 11 <version>1.2.1</version> 12 </dependency> 13 14 <dependency> 15 <groupId>jstl</groupId> 16 <artifactId>jstl</artifactId> 17 <version>1.2</version> 18 </dependency> 19 <dependency> 20 <groupId>taglibs</groupId> 21 <artifactId>standard</artifactId> 22 <version>1.1.2</version> 23 </dependency> 24 <dependency> 25 <groupId>org.tuckey</groupId> 26 <artifactId>urlrewritefilter</artifactId> 27 <version>4.0.3</version> 28 </dependency> 29 </dependencies> 30 31 <build> 32 <!-- 产生的构件的文件名,默认值是${artifactId}-${version} --> 33 <finalName>tsglxt-web</finalName> 34 <!-- 这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里 --> 35 <resources> 36 <!-- 这个元素描述了项目相关或测试相关的所有资源路径 --> 37 <resource> 38 <!-- 描述存放资源的目录,该路径相对POM路径 --> 39 <directory>src/main/resources</directory> 40 <!-- 是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出 --> 41 <filtering>true</filtering> 42 </resource> 43 </resources> 44 </build>
到这里pom.xml就配完了,但是你会发现web层的pom.xml有一个报错:
web.xml is missing and <failOnMissingWebXml> is set to true
意思很显然,没有找到web.xml嘛。我们搭建的项目里并没有自动生成这个文件。别慌,后面的章节有讲到这个。
还有一点,通过maven搭建项目,就不需要自己去官网下载jar包了,前面第一章就没啥用了,第二章的jar包不再是手动添加到项目里,而是通过pom.xml的配置直接引用maven仓库里的jar包。