初次使用Maven创建Java Project请看:https://spring.io/guides/gs/maven/
Maven的下载:https://maven.apache.org/download.cgi
Maven教程:https://www.runoob.com/maven/maven-tutorial.html
Maven Project 用XML文件定义,即pom.xml,定义Project的名字,版本和依赖库
在IDEA下使用Maven创建项目,会自动 创建基础项目结构和pom.xml,pom.xml定义了整个项目的信息
POM( Project Object Model,项目对象模型 ) 是 Maven 工程的基本工作单元,是一个XML文件,包含了项目的基本信息,用于描述项目如何构建,声明项目依赖,等等。
执行任务或目标时,Maven 会在当前目录中查找 POM。它读取 POM,获取所需的配置信息,然后执行目标。
pom.xml下的基本标签为:
-
<modelVersion>
. POM model version (always 4.0.0). -
<groupId>
. Group or organization that the project belongs to. Often expressed as an inverted domain name. -
<artifactId>
. Name to be given to the project’s library artifact (for example, the name of its JAR or WAR file). -
<version>
. Version of the project that is being built. -
<packaging>
- How the project should be packaged. Defaults to "jar" for JAR file packaging. Use "war" for WAR file packaging.
导入Maven后可使用Maven命令行
使用IDEA可自动导入,IDEA右侧点击Maven即可操控Maven,点击m标志即可进入命令行
mvn -v 查看版本
mvn compile 编译项目下的源代码生成class文件
注:IDEA报错
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project practiceOne: Compilation failure: Compilation failure:
[ERROR] 不再支持源选项 5。请使用 6 或更高版本。
[ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。
说明pom.xml文件没有配置好,使用Maven操控项目需要pom.xml的具体配置
在pom.xml下加入元素标签:
<properties>
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
</properties>
以定义编译器源和目的的java版本即可
mvn package 编译、运行、测试打包整个项目并生成jar或者war文件
官网的解释为:The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within the target directory. The name of the JAR file will be based on the project’s <artifactId>
and <version>
.
mvn install 编译、测试打包项目并将项目代码copy到本地仓库中
官网的解释为:The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency.
添加依赖:
当你的项目中需要使用其它类库时需要在pom.xml中说明依赖,并且也需要将这些类库编译打包。
官网的例子为:
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
所有的依赖都需要说明并放在dependencies标签里
其中每个dependency的属性为
-
<groupId>
- The group or organization that the dependency belongs to.即类库制作组织 -
<artifactId>
- The library that is required.即类库名 -
<version>
- The specific version of the library that is required.即类库版本
可选项:<scope>provided</scope>类库在打包运行时也一并被打包运行
<scope>test</scope>只在测试时使用
在IDEA下的pom.xml里添加依赖时,会自动帮你导入依赖的类库,默认快捷键为alt+enter,实在是太方便了
关于pom:
pom标签参考:https://www.runoob.com/maven/maven-pom.html
1.modules/module:
模块,每个模块有自己的完整的项目目录,pom.xml中的modules用来管理同个项目中的各个模块,即module。具有模块的项目被称为多模块或聚合项目。较大的项目会进行模块的划分,模块是此POM列出并作为一组执行的项目。通过一个pom
打包的项目可以将它们列为模块来聚合成一组项目进行构建,这些模块名是这些项目的相对目录。
- <modules>
- <module>my-project</module>
- <module>another-project</module>
- </modules>
其中my-project是一个单独模块,享有自己的项目结构,another-project同理
具体的,当你使用IDEA创建Maven项目时,初始的结构为:
└── src └── main └── java
└──resources
└─test
└──java
当创建完模块后,模块的目录结构(模块包含在初始项目结构之下)为:
└──模块名
└── src └── main └── java
└──resources
└─test
└──java
2.parent
表示pom与pom之间的继承关系 ,继承是 Maven 中很强大的一种功能,继承可以使得子POM可以获得 parent 中的各项配置,可以对子pom进行统一的配置和依赖管理。父POM中的大多数元素都能被子POM继承
注:目录/src/main/java中存放所有的.java源代码
/src/test/java则存放所有的测试代码
mvn compile 用于编译项目代码文件
Maven与pom.xml息息相关,所有关于项目的信息都在pom.xml中配置
官网中的一个完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- tag::joda[] -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
<!-- end::joda[] -->
<!-- tag::junit[] -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- end::junit[] -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>