使用Maven管理项目(1)--创建简单的Console App

本文介绍Maven的基本安装及配置过程,演示如何通过Maven创建Java项目,并解析pom.xml文件的关键元素,包括groupId、artifactId和version等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maven是Ant的替代工具,具体功能不多说,可以通过下面的具体案例理解使用。


下载安装

  1. 首先去http://maven.apache.org/下载maven。
  2. 我的是mac,将其解压到~/tools/下面,目录结构是这样:


  3. 接着给系统增加环境变量:$vi ~/.bash_profile
    M2_HOME=/Users/xxx/tools/apache-maven-3.1.0
    PATH=$M2_HOME:$PATH
    export M2_HOME
    export PATH
    注意更新环境变量:source .bash_profile。
  4. 运行:mvn --version,显示下载后的maven版本号说明安装成功。
  5. 运行:mvn help:system,显示环境变量,并且更新repository文件。
  6. 默认的settings.xml文件位于安装目录的conf下面,并且repository位于~/.m2/repository目录。

与MyEclipse集成

  1. 首先我们修改MyEclipse对于Maven的配置:


  2. 接着需要修改Maven的配置,注意这里我们不要自己添加用户的配置文件:

  3. ok,接下来可以创建maven项目了。

创建Maven工程

  1. 在菜单中选择File->New,创建一个maven project;

  2. 选择模板,对于普通java项目选择org.apache.maven.archetypes中的quickstart;对于web项目选择webapp就行;

  3. 这里我们选择quickstart新建一个console项目,之后next并输入项目信息;

  4. 注意如果有"Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE from any of the configured repositories.",则需要先去这种地址下载jar包:http://mirrors.ibiblio.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/,再去运行命令:
    mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=maven-archetype-quickstart -Dversion=1.1 -Dpackaging=jar -Dfile=maven-archetype-quickstart-1.1.jar
    注意对于不同的jar,groupId、artifactId等等参数均需要修改。
  5. 我在Windows上遇到过pom.xml错误:"Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (execution: default-testCompile, phase: test-compile)",还是去Preference->Maven4MyEclipse->User Settings->Update Settings解决的问题,而且再次新建Maven Project也没有同样的现象。
  6. 项目生成成功,大致结构如下(我选择的quickstart类型项目):

  7. 我们看一下pom.xml文件内容:
    <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.freesoft.mavenconsoleapp</groupId>
      <artifactId>test01</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>test01</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    
    对xml文件内容的分析如下:
    • project:pom.xml文件中的顶层元素;
    • modelVersion:指明POM使用的对象模型的版本。这个值很少改动。
    • groupId:指明创建项目的组织或者小组的唯一标识。GroupId是项目的关键标识,典型的,此标识以组织的完全限定名来定义。比如,org.apache.maven.plugins是所有Maven插件项目指定的groupId。
    • artifactId:指明此项目产生的主要产品的基本名称。项目的主要产品通常为一个JAR文件。第二,象源代码包通常使用artifactId作为最后名称的一部分。典型的产品名称使用这个格式: <artifactId>- <version>. <extension>(比如:myapp-1.0.jar)。
    • version:项目产品的版本号。Maven帮助你管理版本,可以经常看到SNAPSHOT这个版本,表明项目处于开发阶段。
    • name:项目的显示名称,通常用于maven产生的文档中。
    • url:指定项目站点,通常用于maven产生的文档中。 
    • description:描述此项目,通常用于maven产生的文档中。

    项目中我们最需要关注的是groupId、artifactId、version这些内容。
  8. 接着右键工程,选择Run As->Maven build...,


  9. 然后选择Goals,选择jar,并且打包:


  10. Run,去output目录查看输出内容:test01-0.0.1-SNAPSHOT.jar。
  11. 这个jar中没有指定启动class,所以运行需要:java -cp test01-0.0.1-SNAPSHOT.jar com.freesoft.mavenconsoleapp.test01.App
  12. 如果需要清理,可以运行clean;如果需要运行JUnit测试,可以直接运行test;
  13. 至此,整个项目完成。


### IntelliJ IDEA Maven Project Archetype Templates Differences In the context of developing applications using IntelliJ IDEA with Maven, understanding the distinctions between different archetypes is crucial for selecting an appropriate starting point. An archetype serves as a project template that defines the structure and initial configuration of a new Maven project. Archetypes can vary significantly based on their intended use cases: - **Basic Archetype**: Provides minimal setup suitable for simple projects without specific frameworks or libraries. - **Spring Boot Archetype**: Includes configurations tailored specifically for Spring Boot applications, integrating common dependencies like Spring MVC, security, etc.[^1]. - **Web Application Archetype**: Sets up a basic web application framework including necessary directory structures and files such as `web.xml`. - **Java Application Archetype**: Focuses purely on Java-based console applications without any additional layers or integrations. For practicing MyBatis, one should look into archetypes that support database interactions effectively while also providing flexibility to integrate MyBatis seamlessly. The most recommended approach would be creating a custom archetype from an existing working example containing MyBatis integration by executing commands similar to those used in generating base projects but ensuring relevant dependencies are included within the POM file during creation. Alternatively, consider utilizing popular community-contributed archetypes available through repositories which already include setups conducive to learning ORM tools like MyBatis. These pre-configured templates often come bundled with starter codes demonstrating how entities map against tables along with CRUD operations implementation examples. To find these specialized archetypes: 1. Open terminal inside IntelliJ IDEA. 2. Execute command `mvn archetype:generate` followed by specifying filter criteria related to 'mybatis' when prompted interactively. 3. Choose desired option number corresponding to preferred archetype after reviewing descriptions provided at this stage carefully before proceeding further. This method ensures compatibility issues remain minimized since chosen options will have been tested across various environments prior to publication thereby offering stable foundations upon which learners may build robust skills efficiently. ```bash mvn archetype:generate -DgroupId=com.example.mybatistest -DartifactId=mybatis-practice-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值