【Maven配置&IDEA中操作】---Javaweb

【Maven配置&IDEA中操作】---Javaweb

一、下载与安装

下载地址:

https://maven.apache.org/download.cgi

下载完成以后解压即可。

二、环境变量配置

1、系统变量添加

①M2_HOME

配置解压后的bin目录路径

image-20210730174614725

②MAVEN_HOME

配置解压后的maven目录

2、用户变量中的path添加

image-20210730174715846

3、验证

配置成功后cmd窗口输入mvn -version

image-20210730174753973

三、MAVEN自身配置conf/settings.xml

1、配置镜像

image-20210730175030645

	 <mirror>
		<id>alimaven</id>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>central</mirrorOf>        
    </mirror>
	
	<mirror>
		<id>nexus-aliyun</id>
		<mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
	</mirror>

2、配置本地下载包的仓库

image-20210730175117774

这里直接在下载好的maven中新建一个仓库就可以了

image-20210730175145585

四、IDEA中使用Maven

1、使用Maven创建一个简单的Javaweb项目

image-20210730175602087

image-20210730175637545

image-20210730175716348

然后需要选择Maven

image-20210730175802338

按照提示选择自己的目录就可以了

成功之后,maven会自己导包,直到项目创建成功。

image-20210730180020191

2、IDEA中的Maven设置

image-20210730180258267

到这里位置全部就配置完了。

3、创建一个空的Maven项目对比学习

一个干净的Maven项目

image-20210730182443472

只有maven创建的webapp应用才有

image-20210730182619949

4、在第一次创建好的webapp应用下添加文件夹

image-20210730182835720

或者采取下面的方式也可

image-20210730183018529

五、在IDEA中配置Tomcat

首先需要Tomcat的系统配置参考如下:

https://www.cnblogs.com/Knowledge-has-no-limit/p/7240585.html

①在”系统变量“中添加系统变量CATALINA_BASE,CATALINA_HOME;

变量名:CATALINA_BASE

变量值:D:\tools\apache-tomcat-8.5.4 //Tomcat安装目录

image-20210731233536662

变量名:CATALINA_HOME

变量值:D:\tools\apache-tomcat-8.5.4

image-20210731233555858

②此处还需修改ClassPath和Path的变量值。

在ClassPath的变量值中加入:%CATALINA_HOME%\lib\servlet-api.jar;(注意加的时候在原变量值后加英文状态下的“;”)

image-20210731233638347

在Path的变量值中加入:%CATALINA_HOME%\bin;%CATALINA_HOME%\lib(注意加的时候在原变量值后加英文状态下的“;”)

image-20210731233652972

③验证

image-20210731233711639

1、添加Tomcat本地配置

image-20210730183346641

2、具体的设置

image-20210730183656568

解决警告问题:为什么会有这个问题呢?因为我们访问一个网站,就得指定一个文件夹的名字。

image-20210730183743303

image-20210801163941621

3、Maven的Javaweb模板配置文件解释

<?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.darkerg</groupId>
  <artifactId>javaweb-01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
<!--项目的打包方式
  jar:java应用
  war:Javaweb应用-->
  <packaging>war</packaging>

<!-- 配置-->
  <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>
    <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的约定大于配置,可能会出现资源导出失败的问题。

解决方案:在pom.xml中配置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>
posted @ 2021-08-01 18:05  DarkerG  阅读(80)  评论(0编辑  收藏  举报