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>
<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>
https://spring.io/guides/gs/maven/#scratch
在spring 官网中指出,一个基本的POM文件只需要4个标签
With the exception of the optional <packaging> element, this is the simplest possible pom.xml file necessary to build a Java project. It includes the following details of the project configuration:
- <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.
================================================================
多模块web项目POM文件之间的关系:
- 只有web这个模块是按war打包
- 当没有使用<packageing>时,即缺省时,会被maven打包成jar
- 当<packageing>的值是POM时,会把它的module打包,但是它自己不会有target。一个模块用POM的方式打包时,不可以被其他模块直接依赖(因为它没有打包出任何war和jar的东西,它只是可以打包出它的模块)
一般项目只有父模块或者是相对父模块是将<packageing>的值设置为pom的,打包完会在各个子模块的target下看到打出的包若子模块a中嵌套子模块b和子模块c,那么a子模块也可设置为pom的打包形式,但是其他模块(如d)不能直接引用a,要直接引用b,或者c
=================================================================
POM文件之间的继承和聚合:
每个模块都有自己的POM文件,POM文件要注意的两个特性是继承和聚合。
1)看聚合关系,查看父层的pom,pom文件通过<module>标签将模块聚合起来
2)看继承关系,查看子层的pom,pom文件通过<parent>指定继承父层
- 子pom 会直接继承 父pom <properties /> 中声明的属性
- 父pom 中仅仅使用<dependencies> <dependency>依赖的包,会被 子pom 直接继承(不需要显式依赖)<dependency /> 具有依赖传递性,就是可以层层传递
- 父pom 中仅仅使用<plugins> <plugin>依赖的插件,会被 子pom 直接继承(不需要显式依赖)
- 父pom中可以使用<dependecyManagement>和<pluginManagement>来统一管理jar包和插件pugin,不会被 子pom 直接继承。此时子pom如果希望继承该包或插件,则需要显式依赖,同时像 <version /> <url /> 等配置项可以不被显式写出,默认从父pom继承
参考:
https://blog.csdn.net/qq_32331073/article/details/77197360
https://blog.csdn.net/f45056231p/article/details/84942531
=================================================================
mvn的一些主要命令:
mvn complie
This will run Maven, telling it to execute the compile goal. When it’s finished, you should find the compiled .class files in the target/classes directory.
因为我们不是解决部署.class 文件所以还需要打包mvn package
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
Maven also maintains a repository of dependencies on your local machine (usually in a .m2/repository directory in your home directory) for quick access to project dependencies. If you’d like to install your project’s JAR file to that local repository, then you should invoke the install goal
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.mvn test
maven use a plugin called "surefire" to run unit tests. The default configuration of this plugin complies and runs all classes in src/test/java with a name matching Test
大白话就是当你引进了junit的包之后,在maven的加成下,使用@Test的注解,maven能找到并直接调用
=================================================================
<scope>标签
By default, all dependencies are scoped as compile dependencies. That is, they should be available at compile-time (and if you were building a WAR file, including in the /WEB-INF/libs folder of the WAR). Additionally, you may specify a <scope> element to specify one of the following scopes:provided - Dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code (e.g., the Java Servlet API).
【这一条我又疑问,也就是<scope>的值为complie的话,放到容器里就没有啦?】test - Dependencies that are used for compiling and running tests, but not required for building or running the project’s runtime code.
=================================================================