SSH项目搭建(四)——Maven的pom.xml配置

时间:2023-01-17 00:26:18

史上最全的maven的pom.xml文件详解:

https://www.cnblogs.com/qq765065332/p/9238135.html

下面的节点有不理解是啥意思的可以到上面链接的文章里搜索对照

打开父项目的pom.xml

整个pom文件太大,我们一点一点添吧,不然很乱,很难讲清楚。

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">
<modelVersion>4.0.0</modelVersion>
<groupId>tsglxt</groupId>
<artifactId>tsglxt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 打包方式 -->
<packaging>pom</packaging> <!-- 引入的模块 -->
<modules>
<module>tsglxt-common</module>
<module>tsglxt-dao</module>
<module>tsglxt-service</module>
<module>tsglxt-web</module>
</modules>
</project>

2、添加节点

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>tsglxt</groupId>
<artifactId>tsglxt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <!-- 以值替代名称,Properties可以在整个POM中使用,也可以作为触发条件。格式是<name>value</name>。 -->
<properties> </properties> <!-- 构建项目需要的信息 -->
<build> </build> <!-- 继承自该项目的所有子项目的默认依赖信息。这部分的依赖信息不会被立即解析,而是当子项目声明一个依赖(必须描述group ID和artifact ID信息),如果group ID和artifact ID以外的一些信息没有描述,则通过group ID和artifact ID匹配到这里的依赖,并使用这里的依赖信息。 -->
<dependencyManagement>
<!-- 项目引入插件所需要的额外依赖 -->
<dependencies> </dependencies>
</dependencyManagement> <!-- 项目引入插件所需要的额外依赖 -->
<dependencies> </dependencies> <!-- 项目分发信息,在执行mvn deploy后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件部署到远程仓库。 -->
<distributionManagement> </distributionManagement> <modules>
<module>tsglxt-common</module>
<module>tsglxt-dao</module>
<module>tsglxt-service</module>
<module>tsglxt-web</module>
</modules>
</project>

  关于dependencyManagement里的dependencies和不在dependencyManagement里的dependencies的区别:

  https://blog.csdn.net/OiteBody/article/details/70882940

3、添加Maven plugin (看准第一个节点,然后在2中找到对应的节点,把里边的内容复制过去。后面的步骤也是这样)

       <properties>
<!-- Maven plugin -->
<maven-source-plugin.version>2.0.3</maven-source-plugin.version>
<maven-compiler-plugin.version>2.0.2</maven-compiler-plugin.version>
<maven-assembly-plugin.version>2.2.1</maven-assembly-plugin.version>
<maven-deploy-plugin.version>2.4</maven-deploy-plugin.version>
<maven-war-plugin.version>2.1.1</maven-war-plugin.version>
<maven-version-plugin.version>2.1</maven-version-plugin.version>
<maven-jar-plugin.version>2.3.2</maven-jar-plugin.version>
<maven-archetype-plugin.version>2.2</maven-archetype-plugin.version> </properties> <build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>${maven-archetype-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>${maven-version-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<encoding>${source.encoding}</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

4、添加apche common

     <dependencyManagement>
<!-- 项目引入插件所需要的额外依赖 -->
<dependencies>
<!-- Apache commons -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${commons-beanutils.version}</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons-collections.version}</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${commons-httpclient.version}</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons-net.version}</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<classifier>sources</classifier>
<version>${commons-net.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>${commons-pool.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcacheVersion}</version>
</dependency> </dependencies>
</dependencyManagement>

5、添加Spring,Struts,Hibernate

     <properties>
<!-- Spring -->
<spring.version>3.2.9.RELEASE</spring.version>
<spring.mock.version>2.0-rc2</spring.mock.version> <!-- Struts, Hibernate -->
<struts.version>2.3.32</struts.version>
<struts.test.version>2.2.1.1</struts.test.version>
<hibernate.version>3.6.10.Final</hibernate.version>
<hibernate.annotation.version>3.5.6-Final</hibernate.annotation.version>
<hibernate.search>3.4.2.Final</hibernate.search>
<aspectjweaver.version>1.6.11</aspectjweaver.version>
</properties> <dependencyManagement>
<!-- 项目引入插件所需要的额外依赖 -->
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>test</scope>
</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> <!-- Struts -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
<exclusions>
<exclusion>
<artifactId>tools</artifactId>
<groupId>com.sun</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${struts.test.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency> <!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>${hibernate.search}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
</dependencies>
</dependencyManagement>

6、其他配置

     <properties>
<!-- 统一编码格式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<toolkit.version>0.0.8-SNAPSHOT</toolkit.version>
<source.encoding>UTF-8</source.encoding>
<!-- java -->
<java.version>1.8</java.version> <!-- log4j和slf4j -->
<slf4j.version>1.6.2</slf4j.version>
<log4j.version>1.2.14</log4j.version> <!-- javaee -->
<javaee.version>6.0-5</javaee.version>
<servlet.version>3.0-alpha-1</servlet.version>
<jsp.version>2.2</jsp.version>
<guava.version>14.0.1</guava.version>
<gson.version>2.2.4</gson.version>
<json.version>20090211</json.version>
<junit.version>4.8.1</junit.version> <mysql-connector-java.version>5.1.29</mysql-connector-java.version>
<oracle-connector-java.version>11.2.0.1.0</oracle-connector-java.version> <!-- HTML tags -->
<displaytag.version>1.2</displaytag.version>
<servlet.jstl.version>1.2</servlet.jstl.version>
<apache.standard.taglib.version>1.1.2</apache.standard.taglib.version>
<sitemesh.version>2.4.2</sitemesh.version>
</properties> <dependencyManagement>
<!-- 项目引入插件所需要的额外依赖 -->
<dependencies>
<!-- toolkit -->
<dependency>
<groupId>common.toolkit</groupId>
<artifactId>common-toolkit</artifactId>
<version>${toolkit.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>common.toolkit</groupId>
<artifactId>common-toolkit</artifactId>
<version>${toolkit.version}</version>
<classifier>sources</classifier>
</dependency> <!-- Log -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency> <!-- Servlet -->
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
</dependency> <!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency> <!-- JSON -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency> <!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> <!-- DataBase Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle-connector-java.version}</version>
</dependency> <!-- Tags -->
<dependency>
<groupId>displaytag</groupId>
<artifactId>displaytag</artifactId>
<version>${displaytag.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${servlet.jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${apache.standard.taglib.version}</version>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>${sitemesh.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

7、根据各自项目需要,自行添加别的maven配置信息

打开dao层的pom.xml

dao层需要引用common层的po、vo等实体

   <dependencies>
<dependency>
<groupId>tsglxt</groupId>
<artifactId>tsglxt-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

打开service层的pom.xml

service层需要引用dao层和common层,因为dao层引入过common层的xml,引入dao层后common层也自动引入了

   <dependencies>
<dependency>
<groupId>tsglxt</groupId>
<artifactId>tsglxt-dao</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

打开web层的pom.xml

web层(action层)需要引用service层,同上同理

web层还需要引入jstl等jar包……

需要添加一个build节点,描述项目相关的所有资源路径列表。

     <dependencies>
<dependency>
<groupId>tsglxt</groupId>
<artifactId>tsglxt-service</artifactId>
<version>${project.version}</version>
</dependency> <dependency>
<groupId>nl.captcha</groupId>
<artifactId>simplecaptcha</artifactId>
<version>1.2.1</version>
</dependency> <dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>
</dependencies> <build>
<!-- 产生的构件的文件名,默认值是${artifactId}-${version} -->
<finalName>tsglxt-web</finalName>
<!-- 这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里 -->
<resources>
<!-- 这个元素描述了项目相关或测试相关的所有资源路径 -->
<resource>
<!-- 描述存放资源的目录,该路径相对POM路径 -->
<directory>src/main/resources</directory>
<!-- 是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出 -->
<filtering>true</filtering>
</resource>
</resources>
</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包。