如何使用IntelliJ IDEA将普通项目转换为Maven项目

女爷i 2024-05-25 17:36 221阅读 0赞

普通项目与Maven项目差异

直接先上图,使用IDEA创建的普通JAVA项目结构如下:
在这里插入图片描述
可以看到普通java项目除了一个src及配置信息外,空空如也。通过IDEA工具创建Maven项目

在这里插入图片描述
创建的项目结构如下:

在这里插入图片描述
多了一个pom.xml文件,并且src文件夹中自动创建了main/java、main/resources等文件夹,方便管理。很明显使用maven来管理项目更加方便管理。同样各种开发环境都有各种的包管理工具,如PHP中会使用Composer,nodejs使用npm,android开发常用Gradle方式来进行包依赖管理。

如何在IDEA中转换普通项目

由于之前项目是普通项目,所以需要将其转换为Maven项目,在IDEA实现转换很简单,主要有以下方法:

添加Maven支持:

右键选项 “Add Framework Support”,如下:

在这里插入图片描述
然后设置maven相关信息:

在这里插入图片描述
最后编辑下pom.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>jstudy.mybatis</groupId>
  7. <artifactId>java-mybatis-tiny</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <maven.compiler.source>14</maven.compiler.source>
  11. <maven.compiler.target>14</maven.compiler.target>
  12. </properties>
  13. </project>

至此,转换完毕。

添加pom.xml文件转换

在项目根目录下添加pom.xml,并将上面内容粘贴,IDE工具将会自动识别maven项目,然后点击同步下项目:

在这里插入图片描述
转换完毕。

pom.xml文件说明

由于使用maven管理项目,顺便将pom.xml文件配置详解记录一下:

  1. <!-- project 根描述符 -->
  2. <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
  3. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <!-- 指定maven版本,一般为4.0.0 -->
  5. <modelVersion>4.0.0</modelVersion>
  6. <!-- 基本配置 -->
  7. <!-- 继承功能,可以指定父POM -->
  8. <parent>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-parent</artifactId>
  11. <version>2.7.4</version>
  12. <!-- 指定父项目搜索路径,一般远程项目或者仓库无需配置 -->
  13. <relativePath /> <!-- lookup parent from repository -->
  14. </parent>
  15. <!-- 一般jar包为: groupId:artifactId:version -->
  16. <!-- 团体、组织标书符,项目的命名空间 -->
  17. <groupId>org.example</groupId>
  18. <!-- 项目唯一标识符,不能含有点号(.) -->
  19. <artifactId>learn-pom</artifactId>
  20. <!-- 版本号,可以实用特殊字符串 SNAPSHOT、LATEST、RELEASE -->
  21. <!-- SNAPSHOT: 用于开发过程中,表示不稳定版本 -->
  22. <!-- LATEST: 表示特定构建的最新发布版本 -->
  23. <!-- RELEASE: 最有一个稳定的发布版本 -->
  24. <version>1.0-SNAPSHOT</version>
  25. <!-- 项目打包类型,默认jar,常见类型:pom, jar, maven-plugin, ejb, war, ear, rar, par -->
  26. <packaging>jar</packaging>
  27. <!-- 项目依赖 -->
  28. <dependencies>
  29. <!-- 依赖节点 -->
  30. <dependency>
  31. <!-- 与方面解释一样 -->
  32. <groupId>io.springfox</groupId>
  33. <artifactId>springfox-boot-starter</artifactId>
  34. <version>3.0.0</version>
  35. <!-- 对应使用的打包类型,默认为jar -->
  36. <type>jar</type>
  37. <!-- 指任务的类路径及依赖关系传递性,包括 compile(默认)、provided、runtime、test、system -->
  38. <!-- compile: 默认范围,在所有classpath级依赖项目都可使用 -->
  39. <!-- provided: 编译和测试可用,但不可传递 -->
  40. <!-- runtime: 字面意,只在运行和测试时有效 -->
  41. <!-- test: 测试和执行阶段有效,不是传递的 -->
  42. <!-- system: 除特殊指定,始终可用,与systemPath结合使用 -->
  43. <scope>test</scope>
  44. <!-- scope为system时有效 -->
  45. <systemPath></systemPath>
  46. <!-- 可选项,无需此依赖运行 -->
  47. <optional>true</optional>
  48. <!-- 排除一个或多个元素 -->
  49. <exclusions>
  50. <exclusion>
  51. <groupId>io.springfox</groupId>
  52. <artifactId>springfox-boot-teseter</artifactId>
  53. </exclusion>
  54. </exclusions>
  55. </dependency>
  56. </dependencies>
  57. <!-- 多项目(子父项目)中方便依赖管理,保证版本一致,父项目中使用,声明依赖,但不引用,子项目中可以不加版本,使用父项目中配置 -->
  58. <dependencyManagement></dependencyManagement>
  59. <!-- 声明变量,如果后面用到可使用${变量名}来替代 -->
  60. <properties>
  61. <sf.version>2.0.0</sf.version>
  62. </properties>
  63. <!-- 多模块管理 -->
  64. <modules>
  65. <module>project-1</module>
  66. <module>project-2</module>
  67. <module>project-3</module>
  68. </modules>
  69. <!-- 构建设置 -->
  70. <!-- 包括project build、profile build -->
  71. <build>
  72. <!-- 执行目标 -->
  73. <defaultGoal>install</defaultGoal>
  74. <!-- 构建目标目录 -->
  75. <directory>${basedir}/target</directory>
  76. <!-- 各种默认路径及默认值 -->
  77. <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  78. <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
  79. <outputDirectory>${basedir}/target/classes</outputDirectory>
  80. <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
  81. <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
  82. <!-- 项目最终构建名称 -->
  83. <finalName>myproject-SNAPSHOT</finalName>
  84. <!-- 过滤资源信息 -->
  85. <filters>
  86. <filter>myproject.properties</filter>
  87. </filters>
  88. <!-- 扩展插件 -->
  89. <extensions>
  90. <extension>
  91. <groupId>mysql</groupId>
  92. <artifactId>mysql-connector-java</artifactId>
  93. <version>8.0.30</version>
  94. </extension>
  95. </extensions>
  96. <!-- 使用插件 -->
  97. <plugins>
  98. <plugin>
  99. <groupId>org.springframework.boot</groupId>
  100. <artifactId>spring-boot-maven-plugin</artifactId>
  101. <!-- 是否加载插件扩展名 -->
  102. <extensions>false</extensions>
  103. <!-- 是否可继承 -->
  104. <inherited>true</inherited>
  105. <!-- 插件本身需要的依赖 -->
  106. <dependencies></dependencies>
  107. <!-- 插件配置 -->
  108. <configuration>
  109. <excludes>
  110. <exclude>
  111. <groupId>org.projectlombok</groupId>
  112. <artifactId>lombok</artifactId>
  113. </exclude>
  114. </excludes>
  115. </configuration>
  116. <!-- 指定插件不同的目标 -->
  117. <executions>
  118. <execution>
  119. <!-- 目标标识 -->
  120. <id>tester</id>
  121. <!-- 目标列表 -->
  122. <goals>
  123. <goal>run</goal>
  124. </goals>
  125. <!-- 是否继承 -->
  126. <inherited>false</inherited>
  127. <!-- 特定配置 -->
  128. <configuration></configuration>
  129. </execution>
  130. </executions>
  131. </plugin>
  132. </plugins>
  133. <!-- 与dependencyManagement类似,为了统一版本 -->
  134. <pluginManagement></pluginManagement>
  135. </build>
  136. <!-- 针对site生成报告插件,与build内插件类似 -->
  137. <reporting>
  138. <plugins></plugins>
  139. </reporting>
  140. <!-- 项目信息,一般不用填写 -->
  141. <!-- 项目名称 -->
  142. <name>learn pom</name>
  143. <!-- 描述 -->
  144. <description>just learn pom project</description>
  145. <!-- 开始年份 -->
  146. <inceptionYear>2020</inceptionYear>
  147. <!-- 项目地址 -->
  148. <url>https://github.com/</url>
  149. <!-- 使用协议 -->
  150. <licenses>
  151. <license>
  152. <!-- 使用协议 -->
  153. <name>My License</name>
  154. <!-- 分发方式 -->
  155. <distribution>github</distribution>
  156. <!-- 协议地址 -->
  157. <url>https://github.com/</url>
  158. <!-- 注释 -->
  159. <comments>Just my License</comments>
  160. </license>
  161. </licenses>
  162. <!-- 组织信息 -->
  163. <organization>
  164. <!-- 组织名称 -->
  165. <name>My Cor</name>
  166. <!-- 组织链接 -->
  167. <url>https://github.com/</url>
  168. </organization>
  169. <!-- 开发者列表 -->
  170. <developers>
  171. <developer>
  172. <id>master</id>
  173. <name>hunkxia</name>
  174. <email>hunk.xia@gmail.com</email>
  175. <organization>My Cor</organization>
  176. <organizationUrl>https://github.com/</organizationUrl>
  177. <!-- 开发者角色列表 -->
  178. <roles>
  179. <role>master</role>
  180. </roles>
  181. <!-- 开发者其它信息 -->
  182. <properties></properties>
  183. </developer>
  184. </developers>
  185. <!-- 贡献者列表 -->
  186. <contributors></contributors>
  187. <!-- 环境设置 -->
  188. <!-- 缺陷提交讨论地址 -->
  189. <issueManagement>
  190. <!-- 缺陷跟踪系统 -->
  191. <system>iss</system>
  192. <url>https://github.com/</url>
  193. </issueManagement>
  194. <!-- 构建系统配置 -->
  195. <ciManagement>
  196. <!-- 构建系统类型 -->
  197. <system>cis</system>
  198. <!-- 构建地址 -->
  199. <url>https://github.com/</url>
  200. <!-- 通知人列表 -->
  201. <notifiers>
  202. <notifier>
  203. <type>mail</type>
  204. <sendOnError>true</sendOnError>
  205. <sendOnFailure>true</sendOnFailure>
  206. <sendOnSuccess>true</sendOnSuccess>
  207. <sendOnWarning>true</sendOnWarning>
  208. <address>hunk.xia@gmail.com</address>
  209. <!-- 通知配置 -->
  210. <configuration></configuration>
  211. </notifier>
  212. </notifiers>
  213. </ciManagement>
  214. <!-- 邮件列表 -->
  215. <mailingLists>
  216. <!-- 列表信息 -->
  217. <mailingList>
  218. <name>list-1</name>
  219. <subscribe></subscribe>
  220. <unsubscribe></unsubscribe>
  221. <post></post>
  222. <archive></archive>
  223. <otherArchives></otherArchives>
  224. </mailingList>
  225. </mailingLists>
  226. <!-- 源代码版本控制 -->
  227. <scm>
  228. <connection></connection>
  229. <developerConnection></developerConnection>
  230. <tag>HEAD</tag>
  231. <url></url>
  232. </scm>
  233. <!-- 预处理操作 -->
  234. <prerequisites></prerequisites>
  235. <!-- 仓库库配置,可以用来设置远程仓库 -->
  236. <repositories>
  237. <repository>
  238. <id>my repos</id>
  239. <name>My Repository</name>
  240. <url>http://repo.myrepos.cn/content/groups/public/</url>
  241. <releases>
  242. <enabled>true</enabled>
  243. <updatePolicy>daily</updatePolicy>
  244. </releases>
  245. <snapshots>
  246. <enabled>true</enabled>
  247. <checksumPolicy>warn</checksumPolicy>
  248. </snapshots>
  249. <layout>default</layout>
  250. </repository>
  251. </repositories>
  252. <!-- 插件仓库 -->
  253. <pluginRepositories></pluginRepositories>
  254. <!-- 发布管理,管理整个构建 -->
  255. <distributionManagement>
  256. <repository></repository>
  257. <site></site>
  258. <downloadUrl></downloadUrl>
  259. <status></status>
  260. </distributionManagement>
  261. <!-- 配置不同环境,不同包 -->
  262. <profiles>
  263. <profile>
  264. <id>release</id>
  265. <build>
  266. <resources>
  267. <resource>
  268. <directory>src/main/resources</directory>
  269. <includes>
  270. <include>**/*</include>
  271. </includes>
  272. </resource>
  273. </resources>
  274. </build>
  275. </profile>
  276. <profile>
  277. <id>test</id>
  278. <build>
  279. <resources>
  280. <resource>
  281. <directory>src/test/resources</directory>
  282. <includes>
  283. <include>config/*.properties</include>
  284. <include>log4j.xml</include>
  285. </includes>
  286. </resource>
  287. <resource>
  288. <directory>src/main/resources</directory>
  289. <includes>
  290. <include>**/*.xml</include>
  291. </includes>
  292. <excludes>
  293. <exclude>log4j.xml</exclude>
  294. </excludes>
  295. </resource>
  296. </resources>
  297. </build>
  298. <activation>
  299. <activeByDefault>true</activeByDefault>
  300. </activation>
  301. </profile>
  302. </profiles>
  303. </project>

本篇完,下次再深入讲解和研究下多项目(父子项目、依赖项目)如何规范配置。

发表评论

表情:
评论列表 (有 0 条评论,221人围观)

还没有评论,来说两句吧...

相关阅读