普通項目與Maven項目差異
直接先上圖,使用IDEA創(chuàng)建的普通JAVA項目結構如下:
可以看到普通java項目除了一個src及配置信息外,空空如也。通過IDEA工具創(chuàng)建Maven項目
創(chuàng)建的項目結構如下:
多了一個pom.xml文件,并且src文件夾中自動創(chuàng)建了main/java、main/resources等文件夾,方便管理。很明顯使用maven來管理項目更加方便管理。同樣各種開發(fā)環(huán)境都有各種的包管理工具,如PHP中會使用Composer,nodejs使用npm,android開發(fā)常用Gradle方式來進行包依賴管理。
如何在IDEA中轉換普通項目
由于之前項目是普通項目,所以需要將其轉換為Maven項目,在IDEA實現(xiàn)轉換很簡單,主要有以下方法:
添加Maven支持:
右鍵選項 “Add Framework Support”,如下:
然后設置maven相關信息:
最后編輯下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>jstudy.mybatis</groupId>
<artifactId>java-mybatis-tiny</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
</project>
至此,轉換完畢。
添加pom.xml文件轉換
在項目根目錄下添加pom.xml,并將上面內(nèi)容粘貼,IDE工具將會自動識別maven項目,然后點擊同步下項目:
轉換完畢。
pom.xml文件說明
由于使用maven管理項目,順便將pom.xml文件配置詳解記錄一下:文章來源:http://www.zghlxwxcb.cn/news/detail-473377.html
<!-- project 根描述符 -->
<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">
<!-- 指定maven版本,一般為4.0.0 -->
<modelVersion>4.0.0</modelVersion>
<!-- 基本配置 -->
<!-- 繼承功能,可以指定父POM -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<!-- 指定父項目搜索路徑,一般遠程項目或者倉庫無需配置 -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- 一般jar包為: groupId:artifactId:version -->
<!-- 團體、組織標書符,項目的命名空間 -->
<groupId>org.example</groupId>
<!-- 項目唯一標識符,不能含有點號(.) -->
<artifactId>learn-pom</artifactId>
<!-- 版本號,可以實用特殊字符串 SNAPSHOT、LATEST、RELEASE -->
<!-- SNAPSHOT: 用于開發(fā)過程中,表示不穩(wěn)定版本 -->
<!-- LATEST: 表示特定構建的最新發(fā)布版本 -->
<!-- RELEASE: 最有一個穩(wěn)定的發(fā)布版本 -->
<version>1.0-SNAPSHOT</version>
<!-- 項目打包類型,默認jar,常見類型:pom, jar, maven-plugin, ejb, war, ear, rar, par -->
<packaging>jar</packaging>
<!-- 項目依賴 -->
<dependencies>
<!-- 依賴節(jié)點 -->
<dependency>
<!-- 與方面解釋一樣 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
<!-- 對應使用的打包類型,默認為jar -->
<type>jar</type>
<!-- 指任務的類路徑及依賴關系傳遞性,包括 compile(默認)、provided、runtime、test、system -->
<!-- compile: 默認范圍,在所有classpath級依賴項目都可使用 -->
<!-- provided: 編譯和測試可用,但不可傳遞 -->
<!-- runtime: 字面意,只在運行和測試時有效 -->
<!-- test: 測試和執(zhí)行階段有效,不是傳遞的 -->
<!-- system: 除特殊指定,始終可用,與systemPath結合使用 -->
<scope>test</scope>
<!-- scope為system時有效 -->
<systemPath></systemPath>
<!-- 可選項,無需此依賴運行 -->
<optional>true</optional>
<!-- 排除一個或多個元素 -->
<exclusions>
<exclusion>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-teseter</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 多項目(子父項目)中方便依賴管理,保證版本一致,父項目中使用,聲明依賴,但不引用,子項目中可以不加版本,使用父項目中配置 -->
<dependencyManagement></dependencyManagement>
<!-- 聲明變量,如果后面用到可使用${變量名}來替代 -->
<properties>
<sf.version>2.0.0</sf.version>
</properties>
<!-- 多模塊管理 -->
<modules>
<module>project-1</module>
<module>project-2</module>
<module>project-3</module>
</modules>
<!-- 構建設置 -->
<!-- 包括project build、profile build -->
<build>
<!-- 執(zhí)行目標 -->
<defaultGoal>install</defaultGoal>
<!-- 構建目標目錄 -->
<directory>${basedir}/target</directory>
<!-- 各種默認路徑及默認值 -->
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<!-- 項目最終構建名稱 -->
<finalName>myproject-SNAPSHOT</finalName>
<!-- 過濾資源信息 -->
<filters>
<filter>myproject.properties</filter>
</filters>
<!-- 擴展插件 -->
<extensions>
<extension>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</extension>
</extensions>
<!-- 使用插件 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 是否加載插件擴展名 -->
<extensions>false</extensions>
<!-- 是否可繼承 -->
<inherited>true</inherited>
<!-- 插件本身需要的依賴 -->
<dependencies></dependencies>
<!-- 插件配置 -->
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<!-- 指定插件不同的目標 -->
<executions>
<execution>
<!-- 目標標識 -->
<id>tester</id>
<!-- 目標列表 -->
<goals>
<goal>run</goal>
</goals>
<!-- 是否繼承 -->
<inherited>false</inherited>
<!-- 特定配置 -->
<configuration></configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- 與dependencyManagement類似,為了統(tǒng)一版本 -->
<pluginManagement></pluginManagement>
</build>
<!-- 針對site生成報告插件,與build內(nèi)插件類似 -->
<reporting>
<plugins></plugins>
</reporting>
<!-- 項目信息,一般不用填寫 -->
<!-- 項目名稱 -->
<name>learn pom</name>
<!-- 描述 -->
<description>just learn pom project</description>
<!-- 開始年份 -->
<inceptionYear>2020</inceptionYear>
<!-- 項目地址 -->
<url>https://github.com/</url>
<!-- 使用協(xié)議 -->
<licenses>
<license>
<!-- 使用協(xié)議 -->
<name>My License</name>
<!-- 分發(fā)方式 -->
<distribution>github</distribution>
<!-- 協(xié)議地址 -->
<url>https://github.com/</url>
<!-- 注釋 -->
<comments>Just my License</comments>
</license>
</licenses>
<!-- 組織信息 -->
<organization>
<!-- 組織名稱 -->
<name>My Cor</name>
<!-- 組織鏈接 -->
<url>https://github.com/</url>
</organization>
<!-- 開發(fā)者列表 -->
<developers>
<developer>
<id>master</id>
<name>hunkxia</name>
<email>hunk.xia@gmail.com</email>
<organization>My Cor</organization>
<organizationUrl>https://github.com/</organizationUrl>
<!-- 開發(fā)者角色列表 -->
<roles>
<role>master</role>
</roles>
<!-- 開發(fā)者其它信息 -->
<properties></properties>
</developer>
</developers>
<!-- 貢獻者列表 -->
<contributors></contributors>
<!-- 環(huán)境設置 -->
<!-- 缺陷提交討論地址 -->
<issueManagement>
<!-- 缺陷跟蹤系統(tǒng) -->
<system>iss</system>
<url>https://github.com/</url>
</issueManagement>
<!-- 構建系統(tǒng)配置 -->
<ciManagement>
<!-- 構建系統(tǒng)類型 -->
<system>cis</system>
<!-- 構建地址 -->
<url>https://github.com/</url>
<!-- 通知人列表 -->
<notifiers>
<notifier>
<type>mail</type>
<sendOnError>true</sendOnError>
<sendOnFailure>true</sendOnFailure>
<sendOnSuccess>true</sendOnSuccess>
<sendOnWarning>true</sendOnWarning>
<address>hunk.xia@gmail.com</address>
<!-- 通知配置 -->
<configuration></configuration>
</notifier>
</notifiers>
</ciManagement>
<!-- 郵件列表 -->
<mailingLists>
<!-- 列表信息 -->
<mailingList>
<name>list-1</name>
<subscribe></subscribe>
<unsubscribe></unsubscribe>
<post></post>
<archive></archive>
<otherArchives></otherArchives>
</mailingList>
</mailingLists>
<!-- 源代碼版本控制 -->
<scm>
<connection></connection>
<developerConnection></developerConnection>
<tag>HEAD</tag>
<url></url>
</scm>
<!-- 預處理操作 -->
<prerequisites></prerequisites>
<!-- 倉庫庫配置,可以用來設置遠程倉庫 -->
<repositories>
<repository>
<id>my repos</id>
<name>My Repository</name>
<url>http://repo.myrepos.cn/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<!-- 插件倉庫 -->
<pluginRepositories></pluginRepositories>
<!-- 發(fā)布管理,管理整個構建 -->
<distributionManagement>
<repository></repository>
<site></site>
<downloadUrl></downloadUrl>
<status></status>
</distributionManagement>
<!-- 配置不同環(huán)境,不同包 -->
<profiles>
<profile>
<id>release</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</profile>
<profile>
<id>test</id>
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<includes>
<include>config/*.properties</include>
<include>log4j.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>log4j.xml</exclude>
</excludes>
</resource>
</resources>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</project>
本篇完,下次再深入講解和研究下多項目(父子項目、依賴項目)如何規(guī)范配置。文章來源地址http://www.zghlxwxcb.cn/news/detail-473377.html
到了這里,關于如何使用IntelliJ IDEA將普通項目轉換為Maven項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!