什么是maven
代码语言:javascript复制基于项目对象模型(pom)的概念,通过中央信息片段管理一个项目的创建,
报告生成的项目管理工具。
maven目录结构
代码语言:javascript复制artifactId(maven项目)
--src
--main
--java(存放java代码)
--resources(存放配置文件)
--test
--java(存放测试代码,打包时不会进行包装)
--target(存放编译之后的代码)
--pom.xml(存放依赖的地方)
导入依赖的过程
代码语言:javascript复制当创建的项目进行编译时,会识别pom.xml中的依赖,首先maven会识别安装目录下conf文件夹下的
settings.xml寻找本地库的路径并匹配有无依赖对应的jar包,如果没有会去远程库(镜像库(阿里或者网易)maven中央库)中去下载对应的jar包。
maven常用命令管理项目的生命周期
代码语言:javascript复制创建 mvn archetype:generate
会先加载maven创建项目用到的依赖,如果没有依赖对应的jar包会去下载,然后进入选择
创建项目的骨架,默认选项为(普通java项目),我们还需要知道是web项目就可以了。
然后依次编辑groupId,artifactId,version信息(默认1.0-SNAPSHOT),package(默认为
groupId),Y确认信息。
E:mvn_test>mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR- Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 7
Define value for property 'groupId': com.jmy
Define value for property 'artifactId': mavendemo02
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.jmy: :
Confirm properties configuration:
groupId: com.jmy
artifactId: mavendemo02
version: 1.0-SNAPSHOT
package: com.jmy
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: E:mvn_test
[INFO] Parameter: package, Value: com.jmy
[INFO] Parameter: groupId, Value: com.jmy
[INFO] Parameter: artifactId, Value: mavendemo02
[INFO] Parameter: packageName, Value: com.jmy
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: E:mvn_testmavendemo02
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:00 min
[INFO] Finished at: 2019-10-18T19:18:31 08:00
[INFO] ------------------------------------------------------------------------
编译 mvn compile 会自动导入pom.xml中的依赖,并将编译之后的字节码文件放入target/classes目录下
代码语言:javascript复制E:mvn_testmavendemo02>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.jmy:mavendemo02 >-------------------------
[INFO] Building mavendemo02 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavendemo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:mvn_testmavendemo02srcmainresources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavendemo02 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to E:mvn_testmavendemo02targetclasses
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.282 s
[INFO] Finished at: --18T19:: 08:
[INFO] ------------------------------------------------------------------------
测试 mvn test 运行test目录下的测试代码
代码语言:javascript复制E:mvn_testmavendemo02>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.jmy:mavendemo02 >-------------------------
[INFO] Building mavendemo02 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavendemo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:mvn_testmavendemo02srcmainresources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavendemo02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavendemo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:mvn_testmavendemo02srctestresources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavendemo02 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to E:mvn_testmavendemo02targettest-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavendemo02 ---
[INFO] Surefire report directory: E:mvn_testmavendemo02targetsurefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.jmy.AppTest
Tests run: , Failures: , Errors: , Skipped: , Time elapsed: sec
Results :
Tests run: , Failures: , Errors: , Skipped:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.944 s
[INFO] Finished at: --18T19:: 08:
[INFO] ------------------------------------------------------------------------
清理 mvn clean 将target目录清理干净
代码语言:javascript复制E:mvn_testmavendemo02>mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.jmy:mavendemo02 >-------------------------
[INFO] Building mavendemo02 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mavendemo02 ---
[INFO] Deleting E:mvn_testmavendemo02target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.359 s
[INFO] Finished at: --18T19:: 08:
[INFO] ------------------------------------------------------------------------
打包 mvn package 将编译 测试在运行一遍生成当前项目的jar/war包
代码语言:javascript复制E:mvn_testmavendemo02>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.jmy:mavendemo02 >-------------------------
[INFO] Building mavendemo02 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavendemo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:mvn_testmavendemo02srcmainresources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavendemo02 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to E:mvn_testmavendemo02targetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavendemo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:mvn_testmavendemo02srctestresources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavendemo02 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to E:mvn_testmavendemo02targettest-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavendemo02 ---
[INFO] Surefire report directory: E:mvn_testmavendemo02targetsurefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.jmy.AppTest
Tests run: , Failures: , Errors: , Skipped: , Time elapsed: 0.016 sec
Results :
Tests run: , Failures: , Errors: , Skipped:
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavendemo02 ---
[INFO] Building jar: E:mvn_testmavendemo02targetmavendemo02-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.476 s
[INFO] Finished at: --18T19:: 08:
[INFO] ------------------------------------------------------------------------
安装 mvn install 将当前项目的jar包放入本地库中
代码语言:javascript复制E:mvn_testmavendemo02>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.jmy:mavendemo02 >-------------------------
[INFO] Building mavendemo02 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavendemo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:mvn_testmavendemo02srcmainresources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavendemo02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavendemo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:mvn_testmavendemo02srctestresources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavendemo02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavendemo02 ---
[INFO] Surefire report directory: E:mvn_testmavendemo02targetsurefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.jmy.AppTest
Tests run: , Failures: , Errors: , Skipped: , Time elapsed: 0.015 sec
Results :
Tests run: , Failures: , Errors: , Skipped:
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavendemo02 ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mavendemo02 ---
[INFO] Installing E:mvn_testmavendemo02targetmavendemo02-1.0-SNAPSHOT.jar to D:mvn_repocomjmymavendemo021.0-SNAPSHOTmavendemo02-1.0-SNAPSHOT.jar
[INFO] Installing E:mvn_testmavendemo02pom.xml to D:mvn_repocomjmymavendemo021.0-SNAPSHOTmavendemo02-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.566 s
[INFO] Finished at: --18T19:: 08:
[INFO] ------------------------------------------------------------------------
发布 mvn deploy 将将当前项目的jar包放入私服中
代码语言:javascript复制没有私服,就不再测试。
pom.xml详解
代码语言:javascript复制<?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>
<!--当前项目的maven资源左坐标-->
<groupId>com.jmy</groupId>
<artifactId>maven_01</artifactId>
<version>1.0-SNAPSHOT</version>
<!--mvn package 打成什么样的包jar/war-->
<packaging>jar</packaging>
<build>
<plugins>
<plugin><!--插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<!--需要导入的依赖-->
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>