[目录]
参考
本文参考了 Ken 的博客。结合自己的实际操作过程写成。
开发环境
名称 | 版本 |
---|---|
操作系统 | Windows 10 X64 |
JDK | JDK1.8(jdk-8u151-windows-x64) |
IntelliJ IDEA | IntelliJ IDEA 2018.3 |
Maven | Maven 3.6.0 |
项目创建
使用 Maven 模板创建项目
打开 IntelliJ IDEA 2018.3
选择 Create New Project
或 文件
-New
-项目
选择 Maven
,勾选 Create from archetype
,选择项目模板 maven-archetype-quickstart
项目命名与 Maven 配置
项目Maven坐标信息
在 POM 中,groupId
, artifactId
, packaging
, version
叫作 maven 坐标,它能唯一的确定一个项目。有了 maven 坐标,我们就可以用它来指定我们的项目所依赖的其他项目,插件,或者父项目。
节点 | 描述 |
---|---|
groupId | 代表组织和整个项目的唯一标志。比如所有的 Maven 组件的 groupId 都是 org.apache.maven。 |
artifactId | 具体项目的名称,。groupId 和 artifactId 共同确定一个项目在 maven repo 中的位置。例如:groupId=com.my.luoma,artifactId=test,在 maven repo 中的位置为: $M2_REPO/com/my/luoma/test |
version | 用于说明目前项目的版本,在引用依赖的时候确定具体依赖的版本号。 |
packaging | 规定项目的输出格式,包括jar、war、pom、apk等,根据实际需要确定。例如,开发一般的java库,可以使用jar packaging;开发android则是apk packaging。 |
一般 Maven 坐标写成如下的格式:
groupId:artifactId:packaging:version
虽然在不将项目提交到 Maven 官方仓库的情况下,这不是强制约束,但还是建议不论大小项目一律遵守 Maven 的命名标准。
选择 Maven 配置、仓库等
名称 | 说明 | 配置 |
---|---|---|
Maven Home Directory | Maven 安装包路径 | D:\Program Files\apache-maven-3.6.0 |
User settings file | 用户配置文件 | D:\Program Files\apache-maven-3.6.0\conf\settings.xml |
Local repository | 本地仓库 | D:\Program Files\Maven\Repository |
这里为什么要这么选择,参考
指定项目名称&项目目录,这里的项目名称只做显示使用。跟 Maven 坐标无关。如果项目文件夹未创建,会提示帮你创建,选择完成之后。IDEA就会进行项目创建&初始化工作。
项目创建完成后,IDEA识别到这是一个Maven项目,是否导入到项目的IDEA的配置中。选择开启自动导入即可
创建项目配置出错及解决方案
创建项目出现如下情况
详情如下
"D:\Program Files\Java\jdk1.8.0_151\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\Users\v_hwhao\AppData\Local\Temp\archetypetmp "-Dmaven.home=D:\Program Files\apache-maven-3.6.0" "-Dclassworlds.conf=D:\Program Files\apache-maven-3.6.0\bin\m2.conf" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2018.3 -s "D:\Program Files\apache-maven-3.6.0\conf\settings.xml" "-Dmaven.repo.local=D:\Program Files\Maven\Repository" -DinteractiveMode=false -DgroupId=com.my.luoma -DartifactId=test -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'id' (position: START_TAG seen ...</mirror>\n -->\n\t \n\t<id>... @160:6) @ D:\Program Files\apache-maven-3.6.0\conf\settings.xml, line 160, column 6
[WARNING] Unrecognised tag: 'id' (position: START_TAG seen ...</mirror>\n -->\n\t \n\t<id>... @160:6) @ D:\Program Files\apache-maven-3.6.0\conf\settings.xml, line 160, column 6
[WARNING]
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
[WARNING] Could not transfer metadata org.apache.maven.plugins:maven-archetype-plugin/maven-metadata.xml from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.52.215] failed: Connection timed out: connect
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.573 s
[INFO] Finished at: 2019-04-01T16:54:47+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.apache.maven.plugins:maven-archetype-plugin:RELEASE or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-archetype-plugin:jar:RELEASE: Failed to resolve version for org.apache.maven.plugins:maven-archetype-plugin:jar:RELEASE: Could not find metadata org.apache.maven.plugins:maven-archetype-plugin/maven-metadata.xml in local (D:\Program Files\Maven\Repository) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
[ERROR] Maven execution terminated abnormally (exit code 1)
原因在于 D:\Program Files\apache-maven-3.6.0\conf\settings.xml
文件,我的配置是
<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>
-->
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirrors>
D:\Program Files\apache-maven-3.6.0\conf\settings.xml
对应配置修改为
<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>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
再次创建项目又出问题及解决方案
再次创建项目,报下面的错误
"D:\Program Files\Java\jdk1.8.0_151\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\Users\v_hwhao\AppData\Local\Temp\archetypetmp "-Dmaven.home=D:\Program Files\apache-maven-3.6.0" "-Dclassworlds.conf=D:\Program Files\apache-maven-3.6.0\bin\m2.conf" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2018.3 -s "D:\Program Files\apache-maven-3.6.0\conf\settings.xml" "-Dmaven.repo.local=D:\Program Files\Maven\Repository" -DinteractiveMode=false -DgroupId=com.my.luoma -DartifactId=test -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate
[INFO] Scanning for projects...
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
[WARNING] Could not transfer metadata org.apache.maven.plugins:maven-archetype-plugin/maven-metadata.xml from/to alimaven (http://maven.aliyun.com/nexus/content/groups/public/): Connect to maven.aliyun.com:80 [maven.aliyun.com/182.92.29.54, maven.aliyun.com/182.92.29.53, maven.aliyun.com/182.92.29.40, maven.aliyun.com/182.92.29.16, maven.aliyun.com/182.92.29.13, maven.aliyun.com/59.110.251.12, maven.aliyun.com/59.110.251.11, maven.aliyun.com/59.110.251.10, maven.aliyun.com/59.110.251.9, maven.aliyun.com/59.110.251.4] failed: Connection timed out: connect
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:32 min
[INFO] Finished at: 2019-04-01T17:26:01+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.apache.maven.plugins:maven-archetype-plugin:RELEASE or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-archetype-plugin:jar:RELEASE: Failed to resolve version for org.apache.maven.plugins:maven-archetype-plugin:jar:RELEASE: Could not find metadata org.apache.maven.plugins:maven-archetype-plugin/maven-metadata.xml in local (D:\Program Files\Maven\Repository) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
[ERROR] Maven execution terminated abnormally (exit code 1)
我访问了配置中的阿里云 Maven 链接,发现没用了
http://maven.aliyun.com/nexus/content/groups/public/
当前页面不可用.
暂不支持通过仓库URL浏览仓库内容,但不影响构建使用。浏览所有可用仓库及仓库内容,请访问首页:https://maven.aliyun.com
只有重新找一个 Maven 链接了,又找到一个,D:\Program Files\apache-maven-3.6.0\conf\settings.xml
对应配置修改为
<mirrors>
<mirror>
<id>internal</id>
<mirrorOf>*</mirrorOf>
<name>internal repository</name>
<url>http://maven.oa.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
这次这个链接时可访问的
再次重新创建项目,修改了项目名称
创建终于成功
通过下面的创建的信息可以看到,Maven 去下载了很多的 Java 包,打开 Maven 本地仓库目录 D:\Program Files\Maven\Repository
,可以看到下载了很多 Java 包
Downloading from maven-archetype-quickstart-repo: http://maven.oa.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar
Downloaded from maven-archetype-quickstart-repo: http://maven.oa.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar (7.1 kB at 83 kB/s)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.my.luoma
[INFO] Parameter: artifactId, Value: test
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.my.luoma
[INFO] Parameter: packageInPathFormat, Value: com/my/luoma
[INFO] Parameter: package, Value: com.my.luoma
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.my.luoma
[INFO] Parameter: artifactId, Value: test
[INFO] Project created from Archetype in dir: C:\Users\v_hwhao\AppData\Local\Temp\archetypetmp\test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:03 min
[INFO] Finished at: 2019-04-01T17:43:29+08:00
[INFO] ------------------------------------------------------------------------
[INFO] Maven execution finished
启动应用程序
点击 运行
-运行
或者
或者
Ctrl
+Shift
+F10
项目结构说明
根目录说明
目录/文件 | 说明 |
---|---|
.idea | 创建项目的时候自动创建一个 .idea 的项目配置目录来保存项目的配置信息。这是默认选项。 子目录包含一系列XML文件,包括:compiler.xml、encodings.xml、modules.xml等。这些文件记录工程本身的核心信息,包括:模块组件的名称和位置、编译器设置等,可以存放到VCS。一个例外是workspace.xml,该文件存储个人设置(例如窗口位置)以及其它附属于开发环境的信息,不应该存放到VCS |
src | 源文件文件目录(源代码、单元测试代码等) |
target | 编译输出目录,用于存放编译后的文件(类文件,war包jar包等) |
maven-console-test.iml | 模块是工程中一个可以独立编译、运行、调试、测试的单元。模块的配置信息默认存放在其内容根目录(Content root folder)下的 .iml 文件中,该文件一般存放到VCS。 |
pom.xml | Maven 项目配置文件。POM( Project Object Model,项目对象模型 ) 是 Maven 工程的基本工作单元,是一个XML文件,包含了项目的基本信息,用于描述项目如何构建,声明项目依赖,等等。执行任务或目标时,Maven 会在当前目录中查找 POM。它读取 POM,获取所需的配置信息,然后执行目标。 |
外部库 | Project的依赖的 Java 包 |
1.在 IntelliJ IDEA 中 Project 是最顶级的级别,次级别是 Module
2.一个 Project 可以有多个 Module。
目前主流的大型项目结构都是类似这种多 Module 结构,比如我们的项目可以划分为maven-console-test-app,maven-console-test-utils,maven-console-test-model等等
3.Module之前可以互相依赖
4.Project是一个抽象个概念,Project 由一个或者多个Module 组成
5.Project 跟 Module 之间的关系由 pom.xml 来配置
6.Module 之间的依赖由 Module 文件夹中的 pom.xml来配置
源文件目录说明
目录/文件 | 说明 |
---|---|
src/main | 项目主目录 |
src/main/java | 项目的源代码所在的目录(Sources Root) |
src/main/resources | 项目的资源文件所在的目录(Resources Root) |
src/main/filters | 项目的资源过滤文件所在的目录 |
src/main/webapp | web项目的配置、视图等目录 |
src/main/java/com.my.luoma | com.my.luoma 是module的默认package,Maven的规范 |
src/test | 项目测试目录(Sources Root) |
src/test/java | 测试代码所在的目录(Resources Root) |
src/test/resources | 测试相关的资源文件所在的目录 |
src/test/filters | 测试相关的资源过滤文件所在的目录 |
大多数情况下,一个项目都只有一个 Module 构成,需要进行分层都会通过 package 来完成。
例如:
pom.xml 文件说明
<?xml version="1.0" encoding="UTF-8"?>
<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.my.luoma</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_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-jar-plugin</artifactId>
<version>3.0.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>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
节点 | 描述 |
---|---|
modelVersion | Maven 配置版本 |
groupId | 代表组织和整个项目的唯一标志。比如所有的 Maven 组件的 groupId 都是 org.apache.maven。 |
artifactId | 具体项目的名称,。groupId 和 artifactId 共同确定一个项目在 maven repo 中的位置。例如:groupId=com.my.luoma,artifactId=test,在 maven repo 中的位置为: $M2_REPO/com/my/luoma/test |
version | 用于说明目前项目的版本,在引用依赖的时候确定具体依赖的版本号。 |
packaging | 规定项目的输出格式,包括jar、war、pom、apk等,根据实际需要确定。例如,开发一般的java库,可以使用jar packaging;开发android则是apk packaging。 |
name | 项目显示名称 |
url | 项目地址 |
properties | 用于定义变量,可以在当前配置文件pom.xml,以及子Module的pom.xml中引用,引用方式:${propertyname},例如:${junit.version} |
dependencies | 用户配置Module的依赖 |