四、Maven
为什么要学习这个技术?
-
在Java web开发中,需要使用大量的jar包,我们手动去导入
-
如何能够让一个东西自动帮我导入和配置这个jar包
由此Maen诞生了
框架管理工具
1、下载安装Maven
下载积解压即可;
2、配置环境变量
在我们的系统环境变量中
配置如下配置:
-
M2_HOME:maven目录下的bin目录
D:\idea\maven\apache-maven-3.6.3\bin
-
MAVEN_HOME: maven的目录
D:\idea\maven\apache-maven-3.6.3
-
在系统的path中配置
%MAVEN_HOME%\bin
打开cmd运行,显示这样就是配置成功
配置成功
3、阿里云镜像
在D:\idea\maven\apache-maven-3.8.6\conf\settings.xml下配置
-
镜像:mirrors
-
作用:加速我们的下载
-
-
国内建议使用阿里云的镜像
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
-
在maven3.8.1以上会多出一个mirror,注释掉即可
<mirror> <id>maven-default-http-blocker</id> <mirrorOf>external:http:*</mirrorOf> <name>Pseudo repository to mirror external repositories initially using HTTP.</name> <url>http://0.0.0.0/</url> <blocked>true</blocked> </mirror>
4、本地仓库
<localRepository>D:\idea\maven\apache-maven-3.8.6\maven-repo</localRepository>
5、idea下使用maven
1、创建一个普通的maven项目,不用模板
2、idea下创建一个maven项目模板
3、在idea中Maven设置
4、标记文件夹功能
第一方法:
和之前普通maven项目,我们可以看见缺少一些东西
我们可以看见灰色的,这是没有给权限
这时我们右击
这样就可以添加文件了
第二方法:
6、在idea中配置tomcat
-
第一步
-
第二步
-
第三步
-
第四步
-
第五步,解决警告
为什么需要解决警告就好比之前访问的服务器,那就是一个文件夹,必须有路径
-
选第一个,警告就没有了
-
启动tomcat
-
最后成功
-
pom.xml文件
pom.xml是Maven的核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <!--Maven版本和头文件--> <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> <!-- 这里是我们刚才配置的GAV--> <groupId>com.lyh</groupId> <artifactId>javaWeb-01-maven</artifactId> <version>1.0-SNAPSHOT</version> <!-- package:项目的打包方式 jar:java应用 war:JavaWeb应用 --> <packaging>war</packaging> <name>javaWeb-01-maven 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> <!-- 具体依赖的Jar包配置文件--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <!-- 项目构造的东西--> <build> <finalName>javaWeb-01-maven</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
注意:maven由于他的约定大于配置,我们之后会遇见我们写的配置文件,无法被导出或者出现问题
解决方案:
<!-- 在build中配置resources,来防止我们资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> </excludes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
五、maven仓库的使用
1、配置Servlet的依赖
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
2、创建一个Java项目
代码:
package com.lyh.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//响应的类型html
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
//获取响应的输出流
PrintWriter out = resp.getWriter();
out.print("<html>");
out.print("<head>");
out.print("<title>Hello</title>");
out.print("</head>");
out.print("<boby>");
out.print("<h1>Hello 您好haWord</h1>");
out.print("</boby>");
out.print("</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
3、配置web.xml里面的文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
metadata-complete="true">
<!-- web.xml中配置web的核心应用-->
<!-- 注册Servlet-->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.lyh.servlet.HelloServlet</servlet-class>
</servlet>
<!-- 一个Servlet对应一个Mapping:映射-->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<!-- 请求路径-->
<url-pattern>/lyh</url-pattern>
</servlet-mapping>
</web-app>
4、在webapp目录下创建一个静态html文件
5、测试运行tomcat
-
第一次出现的是默认的jsp页面打印出来
-
第二次是我们自己写的harder.html
-
第三次是我们写的Java代码运行出来的页面(这里也是靠配置依赖找到的)
本文作者:菜鸡前来
本文链接:https://www.cnblogs.com/lyhidea/p/16943141.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步