obj 三种 安装 interact 记录 this tput skip
1. 配置文件修改
- 修改 local 库的位置
- 配置源 mirror,推荐很流畅的一个阿里源 2017.3.30 流畅可用
- <?xml version="1.0" encoding="UTF-8" ?>
- <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
- <localRepository>
- D:\Program Files\mvn_repo
- </localRepository>
- <mirrors>
- <!-- 阿里云仓库 -->
- <mirror>
- <id>
- alimaven
- </id>
- <mirrorOf>
- central
- </mirrorOf>
- <name>
- aliyun maven
- </name>
- <url>
- http://maven.aliyun.com/nexus/content/repositories/central/
- </url>
- </mirror>
- <!-- 中央仓库1 -->
- <mirror>
- <id>
- repo1
- </id>
- <mirrorOf>
- central
- </mirrorOf>
- <name>
- Human Readable Name for this Mirror.
- </name>
- <url>
- http://repo1.maven.org/maven2/
- </url>
- </mirror>
- <!-- 中央仓库2 -->
- <mirror>
- <id>
- repo2
- </id>
- <mirrorOf>
- central
- </mirrorOf>
- <name>
- Human Readable Name for this Mirror.
- </name>
- <url>
- http://repo2.maven.org/maven2/
- </url>
- </mirror>
- </mirrors>
- </settings>
2. maven 的几个基础概念
- groupId: 项目的组织机构,也是java包的目录结构,一般都是域名的倒序,比如 com.firefoxbug.www
- artifactId: 项目实际的名字,比如log4j
- packaging: 包的类型,比如jar
- version: 项目版本号,比如1.0-SNAPSHOT
创建工程
- $ groupId = com.firefoxbug.www $ artifactId = HelloWorld $ version = 1.0 - SNAPSHOT $ mvn archetype: generate - DgroupId = $ {
- groupId
- } - DartifactId = $ {
- artifactId
- } - Dpackage = $ {
- groupId
- } - Dversion = $ {
- version
- }
形成的目录
- HelloWorld/
- ├── pom.xml
- └── src
- ├── main
- │ └── java
- │ └── com
- │ └── firefoxbug
- │ └── www
- │ └── App.java
- └── test
- └── java
- └── com
- └── firefoxbug
- └── www
- └── AppTest.java
3. pom.xml
标签说明
- pom文件的顶级节点
- <modelVersion> object model版本,对Maven2和Maven3来说,只能是4.0.0
- <groupId>项目创建组织的标识符,一般是域名的倒写
- <artifactId> 定义了项目在所属组织的标识符下的唯一标识,一个组织下可以有多个项目
- <version> 当前项目的版本,SNAPSHOT,表示是快照版本,在开发中
- <packaging> 打包的方式,有jar、war、ear等
- <name> 项目的名称
- <url> 项目的地址
- <properties>属性配置,比如:UTF-8
- <dependencies> 构建项目依赖的jar
- <groupId>
- com.firefoxbug.helloworld
- <artifactId>
- helloworld
- <version>
- 1.0-SNAPSHOT
- <packaging>
- jar
- <name>
- helloworld
- <dependencies>
- <dependency>
- </dependency>
- </dependencies>
- </project>
如果添加 httpclient,http://mvnrepository.com / 中搜索 httpclient
以 4.5.2 为例,复制 dependency 标签插入 dependencies 标签内,
4. maven 常用 cli
- 1. mvn compile 编译源代码
- 2. mvn test-compile 编译测试代码
- 3. mvn test 运行测试
- 5. mvn package 打包,根据pom.xml打成war或jar
- 如果pom.xml中设置 war,则此命令相当于mvn war:war
- 如果pom.xml中设置 jar,则此命令相当于mvn jar:jar
- 6. mvn -Dtest package
- 打包但不编译test例子:mvn package -Dmaven.test.skip=true
- 打包、编译但是不运行test例子: mvn package -DskipTests
- 7. mvn install 在本地Repository中安装jar,就把maven构建项目的【清理】→【编译】→【测试】→【打包】的这几个过程都做了,同时将打包好的jar包发布到本地的Maven仓库中
- 8. mvn clean 清除产生的项目
- 9. mvn eclipse:eclipse 生成eclipse项目
- 10.mvn idea:idea 生成idea项目
- 11.mvn eclipse:clean 清除eclipse的一些系统设置
支持组合命令
- maven clean compile
查看 dependency:tree
- mvn dependency:tree
执行测试代码
- mvn clean test
CLI 创建 maven 项目,使用 "mvn archetype:generate" 命令和 "mvn archetype:create" 都可以创建项目,目前没有发现这两者的区别,
唯一区别的地方就是发现使用 "mvn archetype:generate" 命令创建项目时要特别长的时间才能够将项目创建好,而使用 "mvn archetype:create" 命令则可以很快将项目创建出来。
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 或者
- mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
BUILD SUCCESS 就表示项目构建成功
5. 打包的几种方式
maven 编译打包可以支持三种模式,分别如下:
-no-dependencies: 这种模式下 target 下面不会出现任何依赖的包,必须手动指定 - classpath 设置所在环境的依赖包路径;
-with-dependencies: 这种模式会把所有外部依赖的包都打进一个 jar 包,只需要把 jar 包发布到线上即可运行;
-copy-dependencies: 这种模式会把外部依赖的 jar 包都独立生成到 target / 下面,运行时候手动指定 - classpath;
-with-dependencies 例子,使用插件 maven-assembly-plugin
- <project>
- ...
- <build>
- <sourceDirectory>
- src/main/java
- </sourceDirectory>
- <plugins>
- <plugin>
- <artifactId>
- maven-assembly-plugin
- </artifactId>
- <configuration>
- <descriptorRefs>
- <descriptorRef>
- jar-with-dependencies
- </descriptorRef>
- </descriptorRefs>
- </configuration>
- <executions>
- <execution>
- <id>
- make-assembly
- </id>
- <phase>
- package
- </phase>
- <goals>
- <goal>
- attached
- </goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- ...
- </project>
-copy-dependencies: 使用插件 org.apache.maven.plugins
- <project>
- ...
- <build>
- <sourceDirectory>
- src/main/java
- </sourceDirectory>
- <plugins>
- <plugin>
- <groupId>
- org.apache.maven.plugins
- </groupId>
- <artifactId>
- maven-dependency-plugin
- </artifactId>
- <executions>
- <execution>
- <id>
- copy
- </id>
- <phase>
- package
- </phase>
- <goals>
- <goal>
- copy-dependencies
- </goal>
- </goals>
- <configuration>
- <outputDirectory>
- ${project.build.directory}
- </outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- ...
- </project>
6. 创建 webL 类型的项目
mvn archetype:generate
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-WebApp-DarchetypeArtifactId=maven-archetype-webapp-DinteractiveMode=false
打包成 war,送至 tomcat 发布
7. 常用插件 org.apache.maven.plugins
maven-compiler-plugin 等
举例
- <build>
- <finalName>
- HttpClientUtil
- </finalName>
- <plugins>
- <plugin>
- <groupId>
- org.apache.maven.plugins
- </groupId>
- <artifactId>
- maven-compiler-plugin
- </artifactId>
- <version>
- 3.3
- </version>
- <configuration>
- <source>
- 1.8
- </source>
- <target>
- 1.8
- </target>
- <encoding>
- UTF-8
- </encoding>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>
- maven-assembly-plugin
- </artifactId>
- <configuration>
- <appendAssemblyId>
- false
- </appendAssemblyId>
- <descriptorRefs>
- <descriptorRef>
- jar-with-dependencies
- </descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass>
- JmeterJavaReq.HttpClientUtil
- </mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>
- make-assembly
- </id>
- <phase>
- package
- </phase>
- <goals>
- <goal>
- assembly
- </goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
参考的文章
- http: //www.tuicool.com/articles/jMRVb2http://www.cnblogs.com/xdp-gacl/tag/Maven学习总结/
【MAVEN】常用配置记录
来源: http://www.bubuko.com/infodetail-2002570.html