IntelliJ IDEA 2018.3 + Maven 3.6.0 创建项目

2019年04月01日 16:44 · 阅读(2997) ·

[目录]

参考

本文参考了 Ken 的博客。结合自己的实际操作过程写成。

Java快速入门教程 3、使用IntelliJ IDEA+Maven 创建、开发、管理项目

Maven 相关知识

开发环境

名称 版本
操作系统 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 安装与配置-配置到 IntelliJ IDEA 2018.3

指定项目名称&项目目录,这里的项目名称只做显示使用。跟 Maven 坐标无关。如果项目文件夹未创建,会提示帮你创建,选择完成之后。IDEA就会进行项目创建&初始化工作。

项目创建完成后,IDEA识别到这是一个Maven项目,是否导入到项目的IDEA的配置中。选择开启自动导入即可

创建项目配置出错及解决方案

创建项目出现如下情况

详情如下

  1. "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
  2. [WARNING]
  3. [WARNING] Some problems were encountered while building the effective settings
  4. [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
  5. [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
  6. [WARNING]
  7. [INFO] Scanning for projects...
  8. Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
  9. [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
  10. [INFO] ------------------------------------------------------------------------
  11. [INFO] BUILD FAILURE
  12. [INFO] ------------------------------------------------------------------------
  13. [INFO] Total time: 23.573 s
  14. [INFO] Finished at: 2019-04-01T16:54:47+08:00
  15. [INFO] ------------------------------------------------------------------------
  16. [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]
  17. [ERROR]
  18. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  19. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  20. [ERROR]
  21. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  22. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
  23. [ERROR] Maven execution terminated abnormally (exit code 1)

原因在于 D:\Program Files\apache-maven-3.6.0\conf\settings.xml 文件,我的配置是

  1. <mirrors>
  2. <!-- mirror
  3. | Specifies a repository mirror site to use instead of a given repository. The repository that
  4. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  5. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  6. |
  7. <mirror>
  8. <id>mirrorId</id>
  9. <mirrorOf>repositoryId</mirrorOf>
  10. <name>Human Readable Name for this Mirror.</name>
  11. <url>http://my.repository.com/repo/path</url>
  12. </mirror>
  13. -->
  14. <id>alimaven</id>
  15. <name>aliyun maven</name>
  16. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  17. <mirrorOf>central</mirrorOf>
  18. </mirrors>

D:\Program Files\apache-maven-3.6.0\conf\settings.xml 对应配置修改为

  1. <mirrors>
  2. <!-- mirror
  3. | Specifies a repository mirror site to use instead of a given repository. The repository that
  4. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  5. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  6. |
  7. <mirror>
  8. <id>mirrorId</id>
  9. <mirrorOf>repositoryId</mirrorOf>
  10. <name>Human Readable Name for this Mirror.</name>
  11. <url>http://my.repository.com/repo/path</url>
  12. </mirror>
  13. -->
  14. <mirror>
  15. <id>alimaven</id>
  16. <name>aliyun maven</name>
  17. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  18. <mirrorOf>central</mirrorOf>
  19. </mirror>
  20. </mirrors>

再次创建项目又出问题及解决方案

再次创建项目,报下面的错误

  1. "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
  2. [INFO] Scanning for projects...
  3. Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
  4. [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
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] BUILD FAILURE
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] Total time: 03:32 min
  9. [INFO] Finished at: 2019-04-01T17:26:01+08:00
  10. [INFO] ------------------------------------------------------------------------
  11. [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]
  12. [ERROR]
  13. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  14. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  15. [ERROR]
  16. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  17. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
  18. [ERROR] Maven execution terminated abnormally (exit code 1)

我访问了配置中的阿里云 Maven 链接,发现没用了

http://maven.aliyun.com/nexus/content/groups/public/

  1. 当前页面不可用.
  2. 暂不支持通过仓库URL浏览仓库内容,但不影响构建使用。浏览所有可用仓库及仓库内容,请访问首页:https://maven.aliyun.com

只有重新找一个 Maven 链接了,又找到一个,D:\Program Files\apache-maven-3.6.0\conf\settings.xml 对应配置修改为

  1. <mirrors>
  2. <mirror>
  3. <id>internal</id>
  4. <mirrorOf>*</mirrorOf>
  5. <name>internal repository</name>
  6. <url>http://maven.oa.com/nexus/content/groups/public/</url>
  7. </mirror>
  8. </mirrors>

这次这个链接时可访问的

再次重新创建项目,修改了项目名称

创建终于成功

通过下面的创建的信息可以看到,Maven 去下载了很多的 Java 包,打开 Maven 本地仓库目录 D:\Program Files\Maven\Repository,可以看到下载了很多 Java 包

  1. 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
  2. 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)
  3. [INFO] ----------------------------------------------------------------------------
  4. [INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:RELEASE
  5. [INFO] ----------------------------------------------------------------------------
  6. [INFO] Parameter: groupId, Value: com.my.luoma
  7. [INFO] Parameter: artifactId, Value: test
  8. [INFO] Parameter: version, Value: 1.0-SNAPSHOT
  9. [INFO] Parameter: package, Value: com.my.luoma
  10. [INFO] Parameter: packageInPathFormat, Value: com/my/luoma
  11. [INFO] Parameter: package, Value: com.my.luoma
  12. [INFO] Parameter: version, Value: 1.0-SNAPSHOT
  13. [INFO] Parameter: groupId, Value: com.my.luoma
  14. [INFO] Parameter: artifactId, Value: test
  15. [INFO] Project created from Archetype in dir: C:\Users\v_hwhao\AppData\Local\Temp\archetypetmp\test
  16. [INFO] ------------------------------------------------------------------------
  17. [INFO] BUILD SUCCESS
  18. [INFO] ------------------------------------------------------------------------
  19. [INFO] Total time: 01:03 min
  20. [INFO] Finished at: 2019-04-01T17:43:29+08:00
  21. [INFO] ------------------------------------------------------------------------
  22. [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 文件说明

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.my.luoma</groupId>
  6. <artifactId>test</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>test</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://www.example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.7</maven.compiler.source>
  14. <maven.compiler.target>1.7</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.11</version>
  21. <scope>test</scope>
  22. </dependency>
  23. </dependencies>
  24. <build>
  25. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  26. <plugins>
  27. <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
  28. <plugin>
  29. <artifactId>maven-clean-plugin</artifactId>
  30. <version>3.1.0</version>
  31. </plugin>
  32. <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
  33. <plugin>
  34. <artifactId>maven-resources-plugin</artifactId>
  35. <version>3.0.2</version>
  36. </plugin>
  37. <plugin>
  38. <artifactId>maven-compiler-plugin</artifactId>
  39. <version>3.8.0</version>
  40. </plugin>
  41. <plugin>
  42. <artifactId>maven-surefire-plugin</artifactId>
  43. <version>2.22.1</version>
  44. </plugin>
  45. <plugin>
  46. <artifactId>maven-jar-plugin</artifactId>
  47. <version>3.0.2</version>
  48. </plugin>
  49. <plugin>
  50. <artifactId>maven-install-plugin</artifactId>
  51. <version>2.5.2</version>
  52. </plugin>
  53. <plugin>
  54. <artifactId>maven-deploy-plugin</artifactId>
  55. <version>2.8.2</version>
  56. </plugin>
  57. <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
  58. <plugin>
  59. <artifactId>maven-site-plugin</artifactId>
  60. <version>3.7.1</version>
  61. </plugin>
  62. <plugin>
  63. <artifactId>maven-project-info-reports-plugin</artifactId>
  64. <version>3.0.0</version>
  65. </plugin>
  66. </plugins>
  67. </pluginManagement>
  68. </build>
  69. </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的依赖