学习SSM阶段使用到的jar包坐标.

学习SSM阶段使用到的jar包坐标.

  • 前言
  • 一、拿来主义.以后构建maven项目, 只需要拿走以下代码即可,但是要遵循使用说明, 替换部分内容
    • 使用说明:
  • 二、使用版本锁定的方式解决依赖冲突
  • 三、一些小坑
    • 1.properties标签中JDK版本的指定
    • 2.servlet-api和jsp-api这两个jar包的问题


前言

用maven集成Java项目的确非常香, 每次构建工程时只需要拷贝一下这里的pom.xml代码即可. 不过仍然有几点需要注意


一、拿来主义.以后构建maven项目, 只需要拿走以下代码即可,但是要遵循使用说明, 替换部分内容

使用说明:

使用说明:
第一 :
开头那几行, 出现了标签(modules标签用于多模块构建),
那么就需要在module里面删除之前工程的模块,加入新工程的模块名. 如果有多个模块,就加多个子标签
如果报错了就试试, 如果不报错就不用管

第二 :
在项目的子模块的pom.xml中, 有一个标签(parent标签用于多模块构建),
代表了他的父模块是谁. 如果报错了就在子模块里加入parent标签的配置即可(如果不会添加就去本地copy一个配好了的子模块的pom.xml)
如果报错了就试试, 如果不报错就不用管

第三 : ctrl+r, 替换掉artifactID(必须)
和groupID(这个可选)里面的内容

第四 : 如果导入完成并且刷新了maven项目之后,报错内容为: xxx module 已经被maven项目移除, 你想在本地项目树中移除该模块吗?
这时候一定要选否, 然后选中xxx module(报错的module)里面的pom.xml, 右击pom.xml->add as maven project

pom.xml的完整代码如下:


<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.0modelVersion>

    <groupId>xxxxxxgroupId>
    <artifactId>xxxxxxartifactId>
    <version>1.0-SNAPSHOTversion> 
    <packaging>pompackaging>

    <name>xxxxxx Maven Webappname>
    
    <url>http://www.example.comurl>

    <properties>

        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>12maven.compiler.source>
        <maven.compiler.target>12maven.compiler.target>
        <spring.version>5.1.9.RELEASEspring.version>
        <springmvc.version>5.1.9.RELEASEspringmvc.version>
        <mybatis.version>3.5.2mybatis.version>
    properties>
    
    <dependencyManagement>
        
        <dependencies>
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>${mybatis.version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${springmvc.version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-coreartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aopartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-expressionartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-beansartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-context-supportartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-txartifactId>
                <version>${spring.version}version>
            dependency>
        dependencies>
    dependencyManagement>
 
    
    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.2version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.32version>
        dependency>
        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-expressionartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            
        dependency>
        <dependency>
            
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2version>
            
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
    dependencies>

    <build>
        <finalName>xxxxxxfinalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-pluginartifactId>
                    <version>3.1.0version>
                plugin>
                
                <plugin>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.0.2version>
                plugin>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.2version>
                    <configuration>
                        <source>12source>
                        <target>12target>
                    configuration>
                plugin>

                <plugin>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>2.22.1version>
                plugin>
                <plugin>
                    <artifactId>maven-war-pluginartifactId>
                    <version>3.2.2version>
                plugin>
                <plugin>
                    <artifactId>maven-install-pluginartifactId>
                    <version>2.5.2version>
                plugin>
                <plugin>
                    <artifactId>maven-deploy-pluginartifactId>
                    <version>2.8.2version>
                plugin>
                
            plugins>
        pluginManagement>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>
project>

二、使用版本锁定的方式解决依赖冲突

这是项目不同模块之间jar包依赖的常见毛病. 不过只需要版本锁定一下就可以解决这个问题. 代码如下:


<dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${springmvc.version}version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-expressionartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>
    dependencies>
dependencyManagement>

三、一些小坑

1.properties标签中JDK版本的指定

<properties>
    
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>12maven.compiler.source>
    <maven.compiler.target>12maven.compiler.target>
    <spring.version>5.1.9.RELEASEspring.version>
    <springmvc.version>5.1.9.RELEASEspringmvc.version>
    <mybatis.version>3.5.2mybatis.version>
properties>

如果这里用的是1.8 这种带"." 的版本, 那就填1.7 1.8之类的
如果这里用的是JDK11, 12 那就直接填写11或12

2.servlet-api和jsp-api这两个jar包的问题

众所周知这两个jar包常常需要跟一个作用范围provided

<scope>providedscope>

但是在JDK12 + tomcat9 + maven3.5.2的环境下, 如果加上provided就会报错
所以这里就不加作用域了

你可能感兴趣的:(Maven,maven,java,jar)