We are going to jump headlong into creating your first Maven project! To create our first Maven project we are going to use Maven's archetype mechanism. An archetype is defined as an original pattern or model from which all other things of the same kind are made . In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements. We are going to show you how the archetype mechanism works now, but if you would like to know more about archetypes please refer to our Introduction to Archetypes .
On to creating your first project! In order to create the simplest of Maven projects, execute the following from the command line:
mvn archetype:create \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DgroupId=com.mycompany.app \
-DartifactId=my-app
Once you have executed this command, you will notice a few things have happened. First, you will notice that a directory named my-app has been created for the new project, and this directory contains a file named pom.xml that should look like this:
<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.mycompany.app</groupid>
<artifactid>my-app</artifactid>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
pom.xml contains the Project Object Model (POM) for this project. The POM is the basic unit of work in Maven. This is important to remember because Maven is inherently project-centric in that everything revolves around the notion of a project. In short, the POM contains every important piece of information about your project and is essentially one-stop-shopping for finding anything related to your project. Understanding the POM is important and new users are encouraged to refer to the Introduction to the POM .
This is a very simple POM but still displays the key elements every POM contains, so let's walk through each of them to familiarize you with the POM essentials:
- project This is the top-level element in all Maven pom.xml files.
- modelVersion This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
- groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.
- artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactid>-<version>.<extension> (for example, myapp-1.0.jar ).</extension></version></artifactid>
- packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.
- version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
- name This element indicates the display name used for the project. This is often used in Maven's generated documentation.
- url This element indicates where the project's site can be found. This is often used in Maven's generated documentation.
- description This element provides a basic description of your project. This is often used in Maven's generated documentation.
For a complete reference of what elements are available for use in the POM please refer to our POM Reference . Now let's get back to the project at hand.
After the archetype generation of your first project you will also notice that the following directory structure has been created:
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
As you can see, the project created from the archetype has a POM, a source tree for your application's sources and a source tree for your test sources. This is the standard layout for Maven projects (the application sources reside in ${basedir}/src/main/java and test sources reside in ${basedir}/src/test/java , where ${basedir} represents the directory containing pom.xml ).
If you were to create a Maven project by hand this is the directory structure that we recommend using. This is a Maven convention and to learn more about it you can read our Introduction to the Standard Directory Layout .
Now that we have a POM, some application sources, and some test sources you are probably asking ...
http://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project
如果是建一个web项目的话 需要在命令中加上
-DarchetypeArtifactId=maven-archetype-webapp
http://today.java.net/pub/a/today/2007/03/01/building-web-applications-with-maven-2.html
分享到:
相关推荐
Android Application Development with Maven is intended for Android developers or devops engineers who want to use Maven to effectively develop quality Android applications. Whether you are already ...
Above all, you will also learn to create site reports and documentation for your project along with handling typical build requirements and other advanced Maven usages. Table of Contents Chapter 1: ...
然而,开发者在使用IntelliJ IDEA(简称Idea)导入Maven项目时,可能会遇到“Unable to import Maven project”的错误提示,这会阻碍项目的正常启动和开发工作。本文将详细解释这个问题的原因,并提供解决方案。 ...
下载Maven的最新版本,解压到指定目录,并在系统的环境变量中设置`M2_HOME`指向Maven的安装路径,同时将`%M2_HOME%\bin`添加到`PATH`变量中,以便在命令行中直接运行Maven命令。 创建J2EE项目的第一步是定义项目...
Maven则是一个项目管理和集成工具,它可以帮助开发者管理依赖、构建项目并确保构建的一致性。在这个"使用Maven构建Struts2最简单例子"中,我们将探讨如何结合这两个工具来创建一个基础的J2EE应用。 首先,让我们...
idea2023自带maven版本不能正常加载http开头的资源 可以加载的版本maven官网已经不...- Downgrade Maven to version 3.8.1 or earlier in settings ---------------------------------------------------------对号入座
1.7 or above (this is to execute Maven - it still allows you to build against 1.3 and prior JDK's). Memory: No minimum requirement. Disk: Approximately 10MB is required for the Maven ...
** Maven 概述** Maven 是一个强大的项目管理和构建工具,广泛应用于Java开发领域。它通过使用一个统一的构建过程,简化了项目的构建、依赖管理以及文档生成等任务。Maven 3.5.2是Maven的一个稳定版本,包含了众多...
maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装...
Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project
2. **创建项目**:使用`mvn archetype:create`命令生成项目结构。 3. **编辑POM.xml**:配置项目信息、依赖和插件。 4. **构建项目**:运行`mvn compile`编译源码,`mvn test`运行测试,`mvn install`将项目安装到...
例如,如果在开发环境中执行`NODE_ENV=dev mvn package`,Maven会找到`src/main/resources/application-dev.properties`并将其包含在打包结果中。同样,测试环境和生产环境也以此类推。 至于压缩包子文件的文件名称...
Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven...
node-maven node-maven is a CLI tool for helping you escape from search-copy-...Enter to copy the maven library url (the latest version) to clipboard. Backspace to get out of list. License MIT License
<localRepository>/path/to/your/repo ``` - **远程仓库**: Maven使用中央仓库下载依赖。如果你需要添加或更改远程仓库,可以在`settings.xml`的`mirrors`或`profiles`部分进行配置。 ### 4. 验证安装 安装完成...
export M2_HOME=/path/to/apache-maven-3.6.3 export PATH=$M2_HOME/bin:$PATH ``` 其中`/path/to/`应替换为实际的解压路径。保存并关闭文件,然后运行`source ~/.bash_profile`或`source ~/.zshrc`使更改生效。...