史上最全maven教程

广州吉欧电子科技有限公司安卓开发工程师岗位 - 技术核心、职责详解与面试指南 广州吉欧电子科技有限公司招聘安卓开发工程师,该岗位可接收优秀应届生。岗位职责包括:1)负责安卓应用架构设计与开发,确保稳定性和可扩展性;2)功能模块编码实现与测试;3)性能优化与用户体验提升;4)后端接口对接;5)系统版本适配;6)用户反馈处理迭代;7)特殊要求熟悉RTK高精度定位技术原理及使用。该岗位要求扎实的安卓开发技术、良好的架构设计能力、性能优化经验,以及特定领域的RTK专业知识,是一个技术全面且具有专业特色的开发岗位。 阅读详情

Maven 介绍

步骤 1 : 什么是Maven

Maven 是专门用于构建和管理Java相关项目的工具。

Maven是意第绪语,依地语(犹太人使用的国际语),表示专家的意思。

所以用Maven管理Java 项目,你就是专家了 -_-!

步骤 2 : Maven 主要用处一:相同的项目结构

使用Maven管理的Java 项目都有着相同的项目结构

  1. 有一个pom.xml 用于维护当前项目都用了哪些jar包
  2. 所有的java代码都放在 src/main/java 下面
  3. 所有的测试代码都放在src/test/java 下面

Maven 主要用处一:相同的项目结构

步骤 3 : Maven 主要用处二:统一维护jar包

比如说有3个Java 项目,这些项目都不是maven风格。那么这3个项目,就会各自维护一套jar包。 而其中有些jar包是相同的。

而maven风格的项目,首先把所有的jar包都放在"仓库“ 里,然后哪个项目需要用到这个jar包,只需要给出jar包的名称和版本号就行了。 这样jar包就实现了共享

如图所示,在pom.xml里,表示用到了mysql 的jar包,版本号是5.1.30。

那么仓库在哪里? 怎么建设呢? 在后续教程会讲到: 仓库

Maven 主要用处二:统一维护jar包
Maven 下载与配置

步骤 1 : 下载

下载区(点击进入)可以下载 apache-maven-3.5.0-bin.zip, 版本是3.5.0,当前是(2017.7.21)最新版本

官方最新版下载地址:http://maven.apache.org/download.cgi

步骤 2 : 配置环境变量

右键我的电脑-属性-高级系统设置-环境变量-系统变量-Path

配置环境变量

步骤 3 : 检验版本

使用win+r, 然后输入cmd 打开命令行,输入如下命令
mvn -v

倘若出现如图所示的截图,即表示配置成功了

检验版本
Maven 仓库

步骤 1 : 仓库概念

所谓的仓库就是用于存放项目需要的jar包的。
maven采用一个仓库,多个项目的方式,让多个项目共享一个仓库里的相同jar包。

步骤 2 : 仓库默认位置

打开
D:\software\apache-maven-3.5.0\conf\settings.xml

可以看到,在52行指定了仓库的位置是${user.home}/.m2/repository。
对应我的机器就是
C:\Users\X7TI.m2\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 at
 
    http://www.apache.org/licenses/LICENSE-2.0
 Unless 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>
  -->

。。。略

步骤 3 : 默认下载路径

maven 会默认从maven官方提供的服务器下载jar包。
而官方服务器在国外,因为大家都知道的原因,网速很慢,而且容易卡断。 为了便于快速下载相关jar包,可以使用国内maven 阿里云的下载地址:使用阿里云下载路径

步骤 4 : 使用阿里云下载路径

打开
D:\software\apache-maven-3.5.0\conf\settings.xml

修改在mirrors下新加一个阿里云的镜像地址:

<mirror><id>alimaven</id><mirrorOf>central</mirrorOf><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/repositories/central/</url></mirror>

也可以直接复制粘贴即可,修改位置是160-165行

<?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 at
 
    http://www.apache.org/licenses/LICENSE-2.0
 Unless 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>d:/maven/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>-->
  </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>alimaven</id><mirrorOf>central</mirrorOf><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/repositories/central/</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>

步骤 5 : 修改仓库位置
仓库默认位置 是 C:\Users\X7TI.m2\repository,放在C盘有一点不好,重装系统就都没有了。所以通常我会把仓库的位置修改为:
d:/maven/repository

配置文件的第55行,复制粘贴即可。

<?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 at
 
    http://www.apache.org/licenses/LICENSE-2.0
 Unless 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>d:/maven/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>-->
  </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>alimaven</id><mirrorOf>central</mirrorOf><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/repositories/central/</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>

步骤 6 : 使用现成的仓库

在下载区(点击进入)有我目前使用的maven仓库,虽不能说齐全,但是相当一部分常用的已经有了。 可以直接下载并解压在
d:/maven/repository

这样就不需要自己下载了。

使用现成的仓库
Maven 创建Maven项目

步骤 1 : 关于Maven风格的Java项目

Maven作为一种工具,可以创建标准化的Maven风格Java项目。 但是在实际工作中,很少有机会直接通过Maven 命令行来做到这一点。 通常都是在Eclipse或者IDEA中,通过集成Maven的方式来创建Java 项目。

本知识点会演示这种做法,目的是为了方便理解在IDE中对Maven的运用

步骤 2 : 切换到项目目录

按下win+r,然后输入cmd,接着输入
cd e:\project

切换到项目目录

步骤 3 : 创建maven项目

首先确保e:\project目录下没有j2se文件夹,否则会报错
然后执行命令:

mvn archetype:generate -DgroupId=com.how2java -DartifactId=j2se -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

archetype:generate 表示创建个项目
-DgroupId 项目包名: com.how2java
-DartifactId 项目名称: j2se
-DarchetypeArtifactId 项目类型: maven-archetype-quickstart
-DinteractiveMode:false 表示前面参数都给了,就不用一个一个地输入了

运行成功会看到绿色的BUILD SUCCESS

创建maven项目

步骤 4 : 项目结构

如图所示,会创建一个标准结构的maven 项目,不仅如此还送了一个App.java。里面输出了一个Hello World!

项目结构
package com.how2java;
 /**
 * Hello world!
 *
 */public class App{public static void main( String[] args ){System.out.println( "Hello World!" );}}

步骤 5 : 运行package命令

然后运行package命令
mvn package

注: 运行这个命令之前要先把当前路径切换到 j2se来
cd j2se

最后会看到一个绿色的BUILD SUCCESS

package做了很多事情,编译,测试,打包,最后生成了一个j2se-1.0-SNAPSHOT.jar包,里面放了APP这个类

运行package命令

步骤 6 : 执行Jar

java -cp target/j2se-1.0-SNAPSHOT.jar com.how2java.App

如图所示,会创建一个标准结构的maven 项目,不仅如此还送了一个App.java。里面输出了一个Hello World!

执行Jar
Maven Eclipse 设置

步骤 1 : 集成MAVEN

Eclipse EE版本是已经集成好MAVEN的了,无需额外手动集成。
没有的同学可以在这里下载:下载 Eclipse

步骤 2 : 设置MAVEN路径

菜单->Window->Preferences->Maven->Installations-> 指定 d:\software\apache-maven-3.5.0

注: 把原来的那个低版本maven删除掉,如果有的话

设置MAVEN路径

步骤 3 : 设置仓库路径

菜单->Window->Preferences->Maven->User Settings->
Global Settings 和 User Settings都使用:
D:\software\apache-maven-3.5.0\conf\settings.xml

点击一下Reindex,确保 local Repository为:
d:/maven/repository

设置仓库路径
Maven Eclipse Maven 项目

步骤 1 : 删除j2se目录

按照前面的学习流程,已经存在一个j2se目录,为避免干扰因素,先把这个目录删除掉

步骤 2 : 新建Maven 项目

菜单->File->New->Other->Maven->Maven Project

新建Maven 项目

步骤 3 : 这个界面点下一步

这个界面点下一步

步骤 4 : 这个界面使用默认的选项,点下一步

这里选择默认的maven-archetype-quickstart, 和命令行创建maven项目中的项目类型一致的

这个界面使用默认的选项,点下一步

步骤 5 : 这一步填写如图所示的信息

Group Id: 填写包名
Artifact Id: 填写j2se
然后点击Finish

这一步填写如图所示的信息

步骤 6 : 运行App

运行App
Maven 添加包

步骤 1 : Maven项目用到第三方jar

当一个Maven项目需要用到第三方jar的时候,就需要添加对应的jar包。
本例演示如何添加一个jdbc的jar包

步骤 2 : 修改App

修改App.class,故意写一段需要依赖jar包才可以成功执行的代码: 初始化mysql驱动

修改App
package com.how2java.j2se;
 /**
 * Hello world!
 *
 */public class App{public static void main( String[] args ) throws ClassNotFoundException{Class.forName("com.mysql.jdbc.Driver");System.out.println("初始化驱动成功");System.out.println( "Hello World!" );}}

步骤 3 : 给maven项目添加jar

双击pom.xml,出现pom专有的编辑页面。
点击OverView右边的Dependencies可以看到里面有一个默认存在的junit
然后点击Add,弹出选择窗口
在中间 Enter GroupId, artifactId or sha1 prefix or pattern(*)… 这个地方输入mysql,下面就会出来被搜索到的mysql-connector-java
选中它,并点击ok

注: Scope要选择compile,表示编译期间要用到。
注: 如果输入了mysql下面没有自动显示搜索结果怎么办?点击 重建本地仓库

给maven项目添加jar

步骤 4 : 保存再执行

点击保存,一定要记得保存,保存才能使得pom.xml中的jar包依赖起作用,接着再运行APP

保存再执行

步骤 5 : 重建本地仓库

倘若在给maven项目添加jar中无法搜索到mysql jar包,那么可以按照如下行为操作一遍
菜单->Window->Show View->Other->Maven->Maven Repositories->Local Repository->Rebuild Index

重建本地仓库
Maven 创建Web项目

步骤 1 : 关于Maven风格的Web项目

与创建Maven项目类似的,还可以创建web 项目
同样的,这种事情,几乎不会用命令行来做,都是用IDE来做的,比如Eclipse Web 项目

所以本知识点也就演示一下,实际工作一般不这么干

步骤 2 : 创建命令

  1. 首先切换目录
  2. 切换盘符
  3. 执行命令(确保e:\project下没有j2ee目录)
    mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.how2java -DartifactId=j2ee -DinteractiveMode=false

这个命令和创建maven java项目中的命令一样,不过类型是从
maven-archetype-quickstart 改成了 maven-archetype-webapp
项目名称从 j2se改成了j2ee而已

创建命令

步骤 3 : 目录结构

与maven创建的java项目结构类似,不过没有java目录,需要自己手动创建。 所以命令行方式的maven web项目很少用到

目录结构
Maven Eclipse Web 项目

步骤 1 : 删除j2ee目录

删除创建Web项目步骤创建的j2ee目录

步骤 2 : 新建Maven 项目

菜单->File->New->Other->Maven->Maven Project

新建Maven 项目

步骤 3 : 这个界面点下一步

这个界面点下一步

步骤 4 : 这个界面使用webapp,点下一步

默认选中的是maven-archetype-quickstart, 请修改为: maven-archetype-webapp

这个界面使用webapp,点下一步

步骤 5 : 这一步填写如图所示的信息

这一步填写如图所示的信息

步骤 6 : 此时得到的maven web 项目的问题

此时能得到的maven web 项目,不过有两个问题

  1. 没有java源代码目录
  2. index.jsp报错

此时得到的maven web 项目的问题

步骤 7 : 创建java 源代码目录

在上一步的截图中可以发现,没有地方存放java源文件,这个时候就需要按照如下步骤做:
右键项目->属性->Java Build Path->Libraries->Edit->Workspace default
JRE(jdk8)->Finish

创建java 源代码目录

步骤 8 : java源文件目录创建好了

如图所示,java源文件目录创建好了

java源文件目录创建好了

步骤 9 : 通过pom.xml添加servlet jar包

这个时候,还存在index.jsp会 报错的问题。 这个问题的解决办法是添加servlet.jar包。
做法参考:给maven项目添加jar

通过pom.xml添加servlet jar包

步骤 10 : 添加好之后,index.jsp也不报错了

添加好之后,index.jsp也不报错了

步骤 11 : 通过eclipse内置tomcat启动

maven web-app 项目就像动态web项目那样,是支持通过eclipse 内置tomcat启动的

右键点击项目j2ee,点击Run As -> Run on Server ,成功之后就会看到如图所示的Hello World!

如果对于Eclipse内置Tomcat配置方式不熟悉,请参考教程:通过Eclipse启动Tomcat-Run On Server

通过eclipse内置tomcat启动
在这里插入图片描述
Maven 创建 SSM 项目

步骤 1 : 基于前面的知识点

本知识点在SSM 的PageHelper的基础上进行,所以要先到SSM 的PageHelper去下载可运行的项目,并解压。

注: 不要下载到当前知识点的可运行项目了哦,当前的是已经完成了目前的步骤流程后完成的项目

步骤 2 : 创建maven web项目

File->New->Other->Maven->Maven Project

创建maven web项目

步骤 3 : 设置项目目录

默认即可

设置项目目录

步骤 4 : 选择Archetype

选择 maven-archetype-webapp

选择Archetype

步骤 5 : 设置GroupId和Artifact Id

设置GroupId和Artifact Id,分别是com.how2java和ssm。
后续配置也是用的这个,所以照着做,别自作聪明改其他的,不然后面走不通

设置GroupId和Artifact Id

步骤 6 : 项目截图

此时项目就创建好了,不过还会报错,接着做后续的一系列操作

项目截图

步骤 7 : 创建java 源代码目录

在上一步的截图中可以发现,没有地方存放java源文件,这个时候就需要按照如下步骤做:
右键项目->属性->Java Build Path->Libraries->Edit->Workspace default
JRE(jdk8)->Finish

创建java 源代码目录

步骤 8 : java源文件目录创建好了

java源文件目录创建好了

步骤 9 : pom.xml

接着复制粘贴pom.xml为下面的代码。 粘贴之后,jsp本来的报错就消失了,不过出来一个新的问题,要求通过Maven更新项目。

右键项目->Maven->Update Project,会弹出一个对话框,点击Ok,这个问题就消失了。

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.how2java</groupId>
  <artifactId>ssm</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <properties><spring.version>4.1.3.RELEASE</spring.version><pagehelper.version>5.1.2-beta</pagehelper.version><mysql.version>5.1.6</mysql.version><mybatis.spring.version>1.2.3</mybatis.spring.version><mybatis.version>3.1.1</mybatis.version><junit.version>4.12</junit.version><jstl.version>1.2</jstl.version><jsqlparser.version>1.0</jsqlparser.version><jackson.version>1.2.7</jackson.version><servlet-api.version>3.1.0</servlet-api.version><druid.version>1.0.18</druid.version><log4j.version>1.2.16</log4j.version><commons-logging.version>1.2</commons-logging.version><commons-fileupload.version>1.2.1</commons-fileupload.version><commons-io.version>1.3.2</commons-io.version><commons-lang.version>2.6</commons-lang.version><aopalliance.version>1.0</aopalliance.version><mybatis-generator.version>1.3.5</mybatis-generator.version>
  </properties>
 
  <dependencies>
 <dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>${junit.version}</version></dependency>
 <dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  <version>${mybatis.version}</version></dependency>
 <dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis-spring</artifactId>  <version>${mybatis.spring.version}</version></dependency>
 <dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>${mysql.version}</version></dependency>
 <dependency>  <groupId>com.alibaba</groupId>  <artifactId>druid</artifactId>  <version>${druid.version}</version></dependency>
 <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  <version>${spring.version}</version></dependency>
 <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-test</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-webmvc</artifactId>  <version>${spring.version}</version></dependency>
 <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  <version>${spring.version}</version></dependency>
 <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-aspects</artifactId>  <version>${spring.version}</version></dependency><!-- JSP相关 --><dependency>  <groupId>jstl</groupId>  <artifactId>jstl</artifactId>  <version>${jstl.version}</version></dependency>
 <dependency>  <groupId>javax.servlet</groupId>  <artifactId>javax.servlet-api</artifactId>  <version>${servlet-api.version}</version>  <scope>provided</scope></dependency>
 <!-- pageHelper --><dependency>  <groupId>com.github.pagehelper</groupId>  <artifactId>pagehelper</artifactId>  <version>${pagehelper.version}</version></dependency>
 <!--jsqlparser--><dependency>  <groupId>com.github.jsqlparser</groupId>  <artifactId>jsqlparser</artifactId>  <version>${jsqlparser.version}</version></dependency><dependency>  <groupId>log4j</groupId>  <artifactId>log4j</artifactId>  <version>${log4j.version}</version></dependency>
 <dependency>  <groupId>commons-logging</groupId>  <artifactId>commons-logging</artifactId>  <version>${commons-logging.version}</version></dependency>
 <dependency>  <groupId>commons-fileupload</groupId>  <artifactId>commons-fileupload</artifactId>  <version>${commons-fileupload.version}</version></dependency>
 <dependency>  <groupId>commons-io</groupId>  <artifactId>commons-io</artifactId>  <version>${commons-io.version}</version></dependency>
 <dependency>  <groupId>commons-lang</groupId>  <artifactId>commons-lang</artifactId>  <version>${commons-lang.version}</version></dependency>
 <dependency>  <groupId>aopalliance</groupId>  <artifactId>aopalliance</artifactId>  <version>${aopalliance.version}</version></dependency>
 <dependency>  <groupId>org.mybatis.generator</groupId>  <artifactId>mybatis-generator-core</artifactId>  <version>${mybatis-generator.version}</version></dependency>
 
  </dependencies>
 
  <build><finalName>${project.artifactId}</finalName><plugins>  <!-- 资源文件拷贝插件 -->  <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.7</version><configuration>  <encoding>UTF-8</encoding></configuration>  </plugin>  <!-- java编译插件 -->  <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration>  <source>1.8</source>  <target>1.8</target>  <encoding>UTF-8</encoding></configuration>  </plugin></plugins><pluginManagement>  <plugins><!-- 配置Tomcat插件 --><plugin>  <groupId>org.apache.tomcat.maven</groupId>  <artifactId>tomcat7-maven-plugin</artifactId>  <version>2.2</version></plugin>  </plugins></pluginManagement>
 <resources>  <resource><directory>src/main/resources</directory><includes>  <include>**/*.properties</include>
          <include>**/*.xml</include>  <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>  <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
 
  </build>
 
</project>

步骤 10 : 接着复制项目文件

首先从在基于前面的知识点里下载的项目里解压出来,然后按照如下顺序复制粘贴。 不要一股脑全粘贴进去,会出现无法解决的错误哦

步骤 11 : web.xml

首先覆盖web.xml
把下载下来的xxx/ssm/WebContent/WEB-INF/web.xml
复制到当前的 ssm/src/main/webapp/web.xml这里

web.xml

步骤 12 : jsp 目录

接着复制jsp目录。
千万不要复制lib目录

jsp 目录

步骤 13 : 配置文件

把三个配置文件复制到 ssm/src/main/resources下面

配置文件

步骤 14 : java源代码

把java源代码复制到src/main/java下

java源代码

步骤 15 : 关于Category.xml

按照maven ssm项目风格来说,这个文件本应该放在Resource目录的mapper文件夹下,但是这么做还要修改配置信息,为了避免问题复杂化,而且考虑到即使放在当前位置也是可以正常工作的,所以就暂时不修改它的位置了。

关于Category.xml

步骤 16 : 启动Tomcat

右键点击项目,点击Run As -> Run on Server 启动Tomcat

如果对于Eclipse内置Tomcat配置方式不熟悉,请参考教程:通过Eclipse启动Tomcat-Run On Server

步骤 17 : 访问测试地址

访问如下测试地址:

http://localhost:8080/ssm/listCategory

访问测试地址

步骤 18 : 可运行项目
在下载区(点击进入)有本知识点对应的可运行项目下载 ,实在自己搞不出来,就下载解压出来比较一下。

注:不要和基于前面的知识点里的下载搞混了哦

AI上推荐 之 协同过滤 㐇 阅读详情

相关推荐

【Zotero】《纯操作讲解,GPT配置,Zotero标注可视化》- 知识点目录

Temperature分布在。之间,越低输出越稳定;

墨门 3396

Maven与IDEA版本兼容问题以及配置

接着在maven项目的porm.xml文件中配置相关依赖的坐标,导入某些依赖的坐标时可能会导入不成功,体现为书写的坐标全为红色,这时,只需要把依赖的坐标书写完整,右键porm.xml,选择maven下的reimport,需要等待一会儿的依赖下载,下载完成之后整个HelloWorld的maven工程就建立好了。最后修改maven插件的设置,选择自己安装的maven,不使用IDEA自带的maven插件。环境变量配置完成后,进行maven的配置文件settings.xml的配置。

m0_67401382的博客 9399

Keil5与ARM编译器V5安装指南:从下载到配置全流程解析

本文提供Keil5与ARM Compiler V5的完整安装与配置指南。针对新版Keil5默认不包含V5编译器导致老工程编译失败的问题,详细解析了从软件下载、授权管理到获取并正确安装ARM Compiler V5(AC5)的全流程,确保开发者能顺利搭建兼容老项目的稳定开发环境。

weixin_29158047的博客 878

创建/导入maven项目时 eclipse默认jdk版本配置

这个问题被困扰了端时间啊,因为有时项目来回弄,都得重新设置jdk版本挺烦人的,今天在往上看到了一篇文章总结下: maven的conf\setting.xml文件中找到jdk注释的地方,打开修改即可,如下 jdk-1.6 1.6 jdk14 Reposi

Hardy专栏 5806

PyTorch实战:A2C算法在Pendulum环境中的调参技巧与收敛优化

本文详细介绍了使用PyTorch实现A2C算法在Pendulum环境中的调参技巧与收敛优化方法。通过分析Pendulum环境特性、设计高效的网络架构、优化超参数设置以及提升训练稳定性,帮助开发者掌握强化学习中的policy-based方法。文章特别强调了奖励工程、优势估计改进和梯度裁剪等关键优化策略,为连续控制任务提供了实用解决方案。

rum55的博客 214

如何检查你的系统是否支持 TPM 2.0

TPM 2.0 是 Windows 11 的硬性要求,作为安全加密芯片,用于存储密钥、认证信息,提升系统安全性。它可防止数据窃取,支持 BitLocker 等功能。多数现代主板已集成 TPM,但需在 BIOS 中启用。可通过 tpm.msc、PowerShell 或微软 PC 健康检查工具验证支持情况。部分设备采用固件实现的 fTPM/PTT,无需独立芯片。

山岚的运维笔记 119

Jenkins+Gitlab+Sonar代码检查平台搭建-Maven

maven jenkins sonar gitlab Jenkins+Gitlab+Sonar代码检查平台搭建-Maven Jenkins+Gitlab+Sonar代码检查平台搭建-Maven Jenkins+Gitlab+Sonar代码检查平台搭建-Maven Jenkins+Gitlab+Sonar代码检查平台搭建-Maven

Dream_Flying_BJ的博客 1557

如何点亮一颗sensor

要点亮一个sensor需要从,时钟、sensorI2C,供电,reset脚,mipi的配置几个方面一一确认。 reset脚拉高 给sensor的时钟有没有。(目前用到的常为24MHZ) 供电 查看sensor的数据手册,看有几个电源,用示波器量一下是否为手册规定的电压 比如:DVDD 数字电压: AVDD模拟电压 DOVDD芯片引脚出来的电压等为多少。 sensor的I2C能否抓到 将I2C的口对应上,用示波器量取i2c的波形。 检查有应答并且数据正确。读写地址是否正...

weixin_40787463的博客 3652

Maven项目管理

Maven项目管理 1.下载maven安装包apache 官网  http://apache.opencas.org/   2.解压到本地,并配置环境变量path中添加maven的环境变量:E:\工具\apache-maven-3.3.9\bin     3.查看maven的目录结构      lib:maven所拥有的jar包

qq_29803591的博客 264

SCI、中科院1区算法应用:(BWO-CNN-BiGRU-Attention)白鲸优化深度学习-融合注意力机制预测程序代码!包含特征可视化,数据Excel导入,直接运行!

BWO 白鲸优化算法,于2022年发表在SCI、中科院1区期刊《Knowledge-Based Systems》上。我们利用该高创新算法对我们的CNN-BiGRU-Attention时序和空间特征结合-融合注意力机制的回归预测程序代码中的超参数进行优化。

weixin_48747334的博客 3265

STM32H743 ADC多通道采样实战:CubeMX配置+DMA传输避坑指南

本文详细介绍了STM32H743 ADC多通道采样的实战配置,包括CubeMX参数设置、DMA传输优化及常见问题解决方案。重点解析了ADC模块的核心特性、多通道采样序列配置、Cache一致性处理等关键技术,帮助开发者高效实现稳定可靠的模拟信号采集系统。

weixin_29175469的博客 613

maven

【one】<dependencyManagement> POM 的<dependencyManagement>元素包含可以被其他项目使用的<dependency>声明。 这样的POM 的子项目将会自动继承这些声明。其他项目可以通过使用<scope>元素的import 值来导入它们 引用了<dependencyManagement>元素的...

wlwork66的博客 666

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

史上最全安装Maven教程

史上最全安装Maven教程 简单了解一下什么是Maven 1.Maven翻译为“专家“, ”内行”的意思,是著名Apache公司下基于Java开发的开源项目。 2.Maven项目对象模型(POM)是一个项目管理工具软件,可以通过简短的中央信息描述来管理项目的搭建,报告和文档等步骤。 3.Maven是一个项目管理的工具,可以对项目进行构建、依赖的管理。 PS:(Gradle的项目搭建方式,将在不久的未来取代Maven,大势所趋。原因如何? 小Du下期为您揭晓) 一.官网下载 下载官网→_→传送门 二.配

小Du猿的博客 34万+

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Maven 史上最全教程​​​​​​​

Maven史上最全教程

Stephen 329

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

史上最全 Maven 教程

Maven,这位项目管理的法师,通过标准化的构建过程、强大的依赖管理和灵活的插件系统,让Java项目开发变得高效、有序。随着你逐渐深入,你会发现更多隐藏在Maven深处的高级魔法,让软件开发之旅充满乐趣和效率。希望这次旅行让你对Maven有了全面的理解,接下来,就去实践吧,成为你项目中的首席魔法师!🧙‍♂️✨。

java专栏 595

史上最全 Maven 教程,没有之一

Maven 是 Java 领域内非常流行的构建工具,它简化了项目的构建过程,并且提供了强大的依赖管理和项目信息管理功能。下面是一份详细的 Maven 教程,涵盖了从安装到进阶使用的各个方面。

qq_33240556的博客 1134

史上最全 Maven 教程,建议收藏!!

Maven 的出现则大大降低开发人员的准备工作,让开发人员更专心与业务,下面即介绍 Maven 基本使用。Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。

weixin_66153022的博客 3285

史上最全Maven教程,没有之一

前后端微服务商城项目,手把手教学!如果你是有一定的开发经验,我相信你一定被项目lib下的JAR包折磨过,如果碰上兼容问题,更是逐个下载不同版本JAR包进行替换排查,相信是每个程序员都不想再经历一边的噩梦。Maven的出现则大大降低开发人员的准备工作,让开发人员更专心与业务,下面即介绍Maven基本使用。Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。一、...

MarkerHub的博客 322
上一篇: J2EE Listener 监听器教程
下一篇: 史上最详细的Intellij IDEA开发教程
普通网友
博客等级 码龄7年 464粉丝 499原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值