spring mvc学习(24):配置maven环境和创建maven项目(建议收藏,超全超详细)
1本次歌謠就對如何創建一個maven項目做一個詳細的講解,畢竟卡了我三天,久久不能入眠,也搜了網上很多的博客
都沒有順利的解決maven項目的創建。這篇建議大家收藏,總會用到的。不然大家看網上的博客也是一臉懵逼。
2首先工具使用
eclipse
3首先我們要創建maven環境,這一步網上的教程一般都是不說的,據說是可安裝也可不安裝。但是最好安裝吧
這邊我就大概講解一下了
首先去網上下載一個https://maven.apache.org/download.cgi
網址是這個,解壓放到我們的盤中,隨便一個盤符都行,我放在D盤了
接著我們就解壓,建立一個java文件放在里面
然后配置環境變量
自己的計算機? 右鍵---屬性---高級系統設置----環境變量 建立一個M2_HOME 和一個MAVEN_HOME
然后加
這些安裝好了之后打開cmd
mvn -v
這時候就安裝完成了
接著咱們就需要下載鏡像,
鏈接:https://pan.baidu.com/s/1H9136tKTECW25LpybQ6OQA
提取碼:h7lx
放在repository目錄下
這些弄好了,注意兩個地方
<?xml version="1.0" encoding="UTF-8"?><!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --><!--| This is the configuration file for Maven. It can be specified at two levels:|| 1. User Level. This settings.xml file provides configuration for a single user,| and is normally provided in ${user.home}/.m2/settings.xml.|| NOTE: This location can be overridden with the CLI option:|| -s /path/to/user/settings.xml|| 2. Global Level. This settings.xml file provides configuration for all Maven| users on a machine (assuming they're all using the same Maven| installation). It's normally provided in| ${maven.conf}/settings.xml.|| NOTE: This location can be overridden with the CLI option:|| -gs /path/to/global/settings.xml|| The sections in this sample file are intended to give you a running start at| getting the most out of your Maven installation. Where appropriate, the default| values (values used when the setting is not specified) are provided.||--> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><!-- localRepository| The path to the local repository maven will use to store artifacts.|| Default: ${user.home}/.m2/repository<localRepository>/path/to/local/repo</localRepository>--><localRepository>D:java\repository</localRepository><!-- interactiveMode| This will determine whether maven prompts you when it needs input. If set to false,| maven will use a sensible default value, perhaps based on some other setting, for| the parameter in question.|| Default: true<interactiveMode>true</interactiveMode>--><!-- offline| Determines whether maven should attempt to connect to the network when executing a build.| This will have an effect on artifact downloads, artifact deployment, and others.|| Default: false<offline>false</offline>--><!-- pluginGroups| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.|--><pluginGroups><!-- pluginGroup| Specifies a further group identifier to use for plugin lookup.<pluginGroup>com.your.plugins</pluginGroup>--></pluginGroups><!-- proxies| This is a list of proxies which can be used on this machine to connect to the network.| Unless otherwise specified (by system property or command-line switch), the first proxy| specification in this list marked as active will be used.|--><proxies><!-- proxy| Specification for one proxy, to be used in connecting to the network.|<proxy><id>optional</id><active>true</active><protocol>http</protocol><username>proxyuser</username><password>proxypass</password><host>proxy.host.net</host><port>80</port><nonProxyHosts>local.net|some.host.com</nonProxyHosts></proxy>--></proxies><!-- servers| This is a list of authentication profiles, keyed by the server-id used within the system.| Authentication profiles can be used whenever maven must make a connection to a remote server.|--><servers><!-- server| Specifies the authentication information to use when connecting to a particular server, identified by| a unique name within the system (referred to by the 'id' attribute below).|| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are| used together.|<server><id>deploymentRepo</id><username>repouser</username><password>repopwd</password></server>--><!-- Another sample, using keys to authenticate.<server><id>siteServer</id><privateKey>/path/to/private/key</privateKey><passphrase>optional; leave empty if not used.</passphrase></server>--><server> <id>n</id> <username>abc</username> <password>12345</password> </server></servers><!-- mirrors| This is a list of mirrors to be used in downloading artifacts from remote repositories.|| It works like this: a POM may declare a repository to use in resolving certain artifacts.| However, this repository may have problems with heavy traffic at times, so people have mirrored| it to several places.|| That repository definition will have a unique id, so we can create a mirror reference for that| repository, to be used as an alternate download site. The mirror site will be the preferred| server for that repository.|--><mirrors><!-- mirror| Specifies a repository mirror site to use instead of a given repository. The repository that| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.|<mirror><id>mirrorId</id><mirrorOf>repositoryId</mirrorOf><name>Human Readable Name for this Mirror.</name><url>http://my.repository.com/repo/path</url></mirror>--><mirror><id>nexus-aliyun</id><mirrorOf>*</mirrorOf><name>Nexus aliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public</url></mirror> </mirrors><!-- profiles| This is a list of profiles which can be activated in a variety of ways, and which can modify| the build process. Profiles provided in the settings.xml are intended to provide local machine-| specific paths and repository locations which allow the build to work in the local environment.|| For example, if you have an integration testing plugin - like cactus - that needs to know where| your Tomcat instance is installed, you can provide a variable here such that the variable is| dereferenced during the build process to configure the cactus plugin.|| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles| section of this document (settings.xml) - will be discussed later. Another way essentially| relies on the detection of a system property, either matching a particular value for the property,| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.| Finally, the list of active profiles can be specified directly from the command line.|| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact| repositories, plugin repositories, and free-form properties to be used as configuration| variables for plugins in the POM.||--><profiles><!-- profile| Specifies a set of introductions to the build process, to be activated using one or more of the| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>| or the command line, profiles have to have an ID that is unique.|| An encouraged best practice for profile identification is to use a consistent naming convention| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.| This will make it more intuitive to understand what the set of introduced profiles is attempting| to accomplish, particularly when you only have a list of profile id's for debug.|| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.<profile><id>jdk-1.4</id><activation><jdk>1.4</jdk></activation><repositories><repository><id>jdk14</id><name>Repository for JDK 1.4 builds</name><url>http://www.myhost.com/maven/jdk14</url><layout>default</layout><snapshotPolicy>always</snapshotPolicy></repository></repositories></profile>--><!--| Here is another profile, activated by the system property 'target-env' with a value of 'dev',| which provides a specific path to the Tomcat instance. To use this, your plugin configuration| might hypothetically look like:|| ...| <plugin>| <groupId>org.myco.myplugins</groupId>| <artifactId>myplugin</artifactId>|| <configuration>| <tomcatLocation>${tomcatPath}</tomcatLocation>| </configuration>| </plugin>| ...|| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to| anything, you could just leave off the <value/> inside the activation-property.|<profile><id>env-dev</id><activation><property><name>target-env</name><value>dev</value></property></activation><properties><tomcatPath>/path/to/tomcat/instance</tomcatPath></properties></profile>--></profiles><!-- activeProfiles| List of profiles that are active for all builds.|<activeProfiles><activeProfile>alwaysActiveProfile</activeProfile><activeProfile>anotherAlwaysActiveProfile</activeProfile></activeProfiles>--> </settings>先把配置文件都給上
修改倉庫指向位置
修改鏡像下載位置
這兩處修改好了之后沒問題了
4點擊windows---preferences
選中上述我們的配置文件
5打開eclipse
new ---maven project
填好信息,這里要搞個war
然后項目就建立成功了,第一次打開eclipse不一定有這個文件
那咋辦呢
參見我的這一篇吧
https://blog.csdn.net/weixin_43392489/article/details/103022682
如果沒有,就這個解決了
還有我們一開始建立文件會有一個紅色報錯,別慌
這個時候 右鍵點擊我們的項目? 選中java ee tool----Generate Deployment descriptor stub就解決了
6一般這個時候我們就開始插入一些lib包了,很多博客都不說,最后jar包都不全你跑個蛇皮
這個路徑下建立個jar包懂不懂
jar需要哪些
這些jar包懂不懂
7我這邊就給貼一個官網的地址,自個下去,如果你這也不會,那也不會,搞個蛇皮,畢竟歌謠高了三天
百度搜索springMVC maven就可以直接找到官網
https://mvnrepository.com/search?q=springmvc
在這可勁搜把
7然后請把lib下的所有lib包biludpath --- add to biludpath,記得加進去哈
這兩個包不在這個地方,自己找地方下載
把包搜全,然后配置我們的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.0</modelVersion><groupId>com.geyao</groupId><artifactId>springmaven</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.8.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.8.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.8.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.8.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.8.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.2.8.RELEASE</version> </dependency></dependencies> </project>8接著我們不是有一個web.xml文件嗎,你別動他位置好吧,我們也直接代碼上上
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>Springmaven01</display-name><welcome-file-list><welcome-file>/WEB-INF/jsp/home.jsp</welcome-file></welcome-file-list><!-- 處理中文亂碼 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- SpringMVC控制器 --><servlet><servlet-name>dispatcherServlet</servlet-name><!-- 主要就是DispatcherServlet這個servlet起到分發的作用,對請求進行控制分發 --><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- 每個springmvc項目都要一個springmvc項目配置位置,下面配置springmvc配置文件的路徑 --><param-name>contextConfigLocation</param-name><param-value>classpath*:springMVC-servlet.xml</param-value></init-param><!-- 當容器啟動時立即啟動 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><!-- 下面配置springmvc的過濾分發請求類型,可以是/ 或者*.action等 --><url-pattern>/</url-pattern></servlet-mapping></web-app>9我的配置文件是springMVC-servlet.xml
放在resource路徑下,別放錯了
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><!-- 定義要掃描 controller的包--><context:component-scan base-package="wormday.springmvc.helloworld" /><!-- 配置視圖解析器 如何把handler 方法返回值解析為實際的物理視圖 --><!--指定視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 視圖的路徑 --><property name="prefix" value="/WEB-INF/jsp/"/><!-- 視圖名稱后綴 --><property name="suffix" value=".jsp"/></bean></beans>10做好這個配置以后,建立一個wormday.springmvc.helloworld包,然后建一個indexController類,這邊你想叫什么
就是什么,別和我一樣都可以
package wormday.springmvc.helloworld;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller @RequestMapping("/test") public class IndexController {@RequestMapping("/index") public String say() { return "home";} }11然后在web-inf下面建立一個jsp文件夾,里面放一個home.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>nihaohao</h1> </body> </html>隨便寫
12接下來就是配置我們的tomcat,
window--profrerences
添加一個tomcat,當然這個也是一個需要安裝的,屬于window上面安裝tomcat
網上下載解壓沒那么多事,選中
13然后修改端口可以這里修改,避免端口占用
14緊接著就是點擊我們的項目 右擊--run as 啟動服務
15最后運行結果,如果你最后沒有創建成功,那不用找我,歌謠自己也琢磨三天。
總結
以上是生活随笔為你收集整理的spring mvc学习(24):配置maven环境和创建maven项目(建议收藏,超全超详细)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Java 高并发》01 高并发基本概念
- 下一篇: spring mvc学习(5):IDEA