IntelliJ IDEA 创建Maven 工程

使用IDEA 创建一个Maven工程
相关介绍:

pom.xml:是Maven的核心文件,它是指示Maven如何工作的元数据文件,位于每个工程的根目录中。

GroupId:是一个工程在全局中的唯一标识符,有利于使用一个完全不同的包名将一个工程从其他有类似名称的工程中区分出来。

Artifact:中文名为“构件”,是工程将要产生或需要使用的文件,它可以是.jar  .war 源文件等等。

Dependency:依赖的包。

Plug-in:Maven就是一堆插件的集合。

Repository:仓库,即放置Artifact的地方

开始创建工程:

IDEA(File--New Project)新建一个Maven项目













创建完成,下面开始添加一个入门Spring Web应用所需的依赖包

<?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.smart</groupId>
  <artifactId>chapter2</artifactId>
  <version>1.0</version>
  <name>CSDNDemo</name>
  <description>CSDNDemo</description>
  <packaging>war</packaging>
  <dependencies>
    <!-- 依赖的spring 类库-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</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-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--依赖的连接池类库-->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>${commons-dbcp.version}</version>
    </dependency>
    <!--依赖的数据库驱动类-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <!--依赖的Web类库-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${servlet.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
    <!--依赖的测试类库-->
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>${testng.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <!-- jetty servlet容器插件 -->
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.25</version>
        <configuration>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>8000</port>
              <maxIdleTime>60000</maxIdleTime>
            </connector>
          </connectors>
          <contextPath>/bbs</contextPath>
          <scanIntervalSeconds>0</scanIntervalSeconds>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <parallel>methods</parallel>
          <threadCount>10</threadCount>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
    <file.encoding>UTF-8</file.encoding>
    <spring.version>4.2.2.RELEASE</spring.version>
    <mysql.version>5.1.29</mysql.version>
    <servlet.version>3.0-alpha-1</servlet.version>
    <aspectj.version>1.8.1</aspectj.version>
    <commons-codec.version>1.9</commons-codec.version>
    <commons-dbcp.version>1.4</commons-dbcp.version>
    <hibernate.validator.version>5.0.2.Final</hibernate.validator.version>
    <jetty.version>8.1.8.v20121106</jetty.version>
    <slf4j.version>1.7.5</slf4j.version>
    <testng.version>6.8.7</testng.version>
  </properties>
</project>

然后打开Maven Project项目管理界面,下载安装添加的依赖


目录结构:

目录

目的

${basedir}

存放 pom.xml和所有的子目录

${basedir}/src/main/java

项目的 java源代码

${basedir}/src/main/resources

项目的资源,比如说 property文件

${basedir}/src/test/java

项目的测试类,比如说 JUnit代码

${basedir}/src/test/resources

测试使用的资源





### intellij idea 创建新项目 没有 Maven 选项 的解决方案 当遇到 IntelliJ IDEA创建新项目时没有 Maven 选项的情况,可以按照以下方法来解决问题。 #### 安装并配置 Maven 插件 确认已安装 Maven 插件。如果未安装,则可以通过插件市场下载并安装该插件[^1]。进入 `File` -> `Settings` (Windows/Linux) 或者 `IntelliJ IDEA` -> `Preferences` (macOS),导航到 `Plugins` 并搜索 "Maven" 进行安装。 #### 配置全局工具设置 确保正确设置了 Maven 路径。同样通过上述路径访问 Settings/Preferences 对话框,在左侧列表中选择 `Build, Execution, Deployment` -> `Build Tools` -> `Maven` 来指定本地计算机上的 Maven 主目录位置以及用户设置文件的位置[^2]。 #### 更新 IDE 缓存与索引 有时缓存问题也会导致缺少某些功能项显示不正常。尝试清理缓存和重新构建索引:关闭当前打开的所有项目;点击 `File` -> `Invalidate Caches / Restart...` ,然后重启软件让其自动重建必要的数据结构以便恢复正常工作状态。 #### 使用命令行初始化项目再导入至IDEA 作为替代方案之一,可以在命令提示符下利用 mvn archetype:generate 命令快速搭建好基础框架后再将其作为一个现有模块引入到 IntellIJ IDEA 当中继续开发。 ```bash mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=my-web-app \ -DarchetypeArtifactId=maven-archetype-webapp \ -DinteractiveMode=false ``` 以上措施有助于解决在 IntelliJ IDEA 新建工程过程中缺失 Maven 构建方式的问题,并能帮助开发者顺利开展基于此技术栈的应用程序编写活动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值