maven 基础

http://maven.apache.org

download   —>  install (解压到安装目录中)

tar xzvf apache-maven-3.3.9-bin.tar.gz  -C  /PATH

Check environment variable value

echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

Adding to PATH

export PATH=/opt/apache-maven-3.3.9/bin:$PATH

mvn -v 看到版本

bogon:~ yuanjicai$ echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home

bogon:~ yuanjicai$ echo $PATH

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/apache-maven-3.3.9/bin

bogon:~ yuanjicai$

bogon:~ yuanjicai$ mvn -v

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)

Maven home: /opt/apache-maven-3.3.9

Java version: 1.8.0_91, vendor: Oracle Corporation

Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre

Default locale: zh_CN, platform encoding: UTF-8

OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

bogon:~ yuanjicai$

实例:

bogon:~ yuanjicai$ mkdir maven

bogon:~ yuanjicai$ cd maven

bogon:maven yuanjicai$ mkdir -p src/{test,main/java/yeecall/com/maven}

bogon:maven yuanjicai$ vim pom.xml

bogon:maven yuanjicai$ cat 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 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>yeecall.com.maven</groupId>

<artifactId>maven01-ch</artifactId>

<version>0.0.1-SNAPSHOT</version>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

</project>

bogon:maven yuanjicai$

bogon:maven yuanjicai$ vim src/main/java/yeecall/com/maven/HelloMaven.java

bogon:maven yuanjicai$ cat src/main/java/yeecall/com/maven/HelloMaven.java

package yeecall.com.maven;

public class HelloMaven

{

public String sayHello(String name){

return "hello:"+name;

}

}

bogon:maven yuanjicai$

bogon:maven yuanjicai$ ls

pom.xmlsrc

bogon:maven yuanjicai$ mvn compile

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building maven01-ch 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/src/main/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-ch ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to /Users/yuanjicai/maven/target/classes

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 1.491 s

[INFO] Finished at: 2016-08-29T14:40:26+08:00

[INFO] Final Memory: 13M/155M

[INFO] ------------------------------------------------------------------------

bogon:maven yuanjicai$

bogon:maven yuanjicai$ ls

pom.xmlsrctarget

bogon:maven yuanjicai$ ls target/

classesmaven-status

bogon:maven yuanjicai$ ls target/classes/

yeecall

bogon:maven yuanjicai$ ls target/classes/yeecall/com/maven/

HelloMaven.class

bogon:maven yuanjicai$ ls src/test/

bogon:maven yuanjicai$

bogon:maven yuanjicai$ mkdir -p src/test/java/yeecall/com/maven/

bogon:maven yuanjicai$ vim src/test/java/yeecall/com/maven/TestHelloMaven.java

bogon:maven yuanjicai$ cat src/test/java/yeecall/com/maven/TestHelloMaven.java

package yeecall.com.maven;

import org.junit.*;

import static org.junit.Assert.*;

public class TestHelloMaven

{

@Test

public void testSayHello() {

HelloMaven hm = new HelloMaven();

String str = hm.sayHello("maven");

assertEquals(str,"hello:maven");

}

}

bogon:maven yuanjicai$

bogon:maven yuanjicai$ vim pom.xml

bogon:maven yuanjicai$ cat 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 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>yeecall.com.maven</groupId>

<artifactId>maven01-ch</artifactId>

<version>0.0.1-SNAPSHOT</version>

<dependencies>

    <dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.10</version>

    </dependency>

  </dependencies>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

</project>

bogon:maven yuanjicai$

bogon:maven yuanjicai$ mvn test

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building maven01-ch 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/src/main/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-ch ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to /Users/yuanjicai/maven/target/classes

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven01-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/src/test/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven01-ch ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to /Users/yuanjicai/maven/target/test-classes

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven01-ch ---

[INFO] Surefire report directory: /Users/yuanjicai/maven/target/surefire-reports

-------------------------------------------------------

T E S T S

-------------------------------------------------------

Running yeecall.com.maven.TestHelloMaven

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.086 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 2.715 s

[INFO] Finished at: 2016-08-29T14:54:32+08:00

[INFO] Final Memory: 15M/168M

[INFO] ------------------------------------------------------------------------

bogon:maven yuanjicai$

bogon:maven yuanjicai$ ls target/

classessurefire-reports

maven-statustest-classes

bogon:maven yuanjicai$ ls target/surefire-reports/

TEST-yeecall.com.maven.TestHelloMaven.xml

yeecall.com.maven.TestHelloMaven.txt

bogon:maven yuanjicai$ cat target/surefire-reports/yeecall.com.maven.TestHelloMaven.txt

-------------------------------------------------------------------------------

Test set: yeecall.com.maven.TestHelloMaven

-------------------------------------------------------------------------------

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.086 sec

bogon:maven yuanjicai$

bogon:maven yuanjicai$ mvn clean

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building maven01-ch 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven01-ch ---

[INFO] Deleting /Users/yuanjicai/maven/target

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 0.361 s

[INFO] Finished at: 2016-08-29T15:01:05+08:00

[INFO] Final Memory: 6M/123M

[INFO] ------------------------------------------------------------------------

bogon:maven yuanjicai$ ls

pom.xmlsrc

bogon:maven yuanjicai$

bogon:maven yuanjicai$ mvn package

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building maven01-ch 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/src/main/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-ch ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to /Users/yuanjicai/maven/target/classes

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven01-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/src/test/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven01-ch ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to /Users/yuanjicai/maven/target/test-classes

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven01-ch ---

[INFO] Surefire report directory: /Users/yuanjicai/maven/target/surefire-reports

-------------------------------------------------------

T E S T S

-------------------------------------------------------

Running yeecall.com.maven.TestHelloMaven

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.083 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]

[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven01-ch ---

[INFO] Building jar: /Users/yuanjicai/maven/target/maven01-ch-0.0.1-SNAPSHOT.jar

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 3.114 s

[INFO] Finished at: 2016-08-29T15:02:47+08:00

[INFO] Final Memory: 17M/207M

[INFO] ------------------------------------------------------------------------

bogon:maven yuanjicai$ ls target/maven01-ch-0.0.1-SNAPSHOT.jar

target/maven01-ch-0.0.1-SNAPSHOT.jar

bogon:maven yuanjicai$

bogon:maven yuanjicai$ mvn install

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building maven01-ch 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/src/main/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-ch ---

[INFO] Nothing to compile - all classes are up to date

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven01-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/src/test/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven01-ch ---

[INFO] Nothing to compile - all classes are up to date

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven01-ch ---

[INFO] Surefire report directory: /Users/yuanjicai/maven/target/surefire-reports

-------------------------------------------------------

T E S T S

-------------------------------------------------------

Running yeecall.com.maven.TestHelloMaven

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.096 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]

[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven01-ch ---

[INFO]

[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven01-ch ---

[INFO] Installing /Users/yuanjicai/maven/target/maven01-ch-0.0.1-SNAPSHOT.jar to /Users/yuanjicai/.m2/repository/yeecall/com/maven/maven01-ch/0.0.1-SNAPSHOT/maven01-ch-0.0.1-SNAPSHOT.jar

[INFO] Installing /Users/yuanjicai/maven/pom.xml to /Users/yuanjicai/.m2/repository/yeecall/com/maven/maven01-ch/0.0.1-SNAPSHOT/maven01-ch-0.0.1-SNAPSHOT.pom

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 2.658 s

[INFO] Finished at: 2016-08-29T15:10:28+08:00

[INFO] Final Memory: 12M/220M

[INFO] ------------------------------------------------------------------------

bogon:maven yuanjicai$ ls ~/.m2/repository/yeecall/com/maven/maven01-ch/0.0.1-SNAPSHOT/maven01-ch-0.0.1-SNAPSHOT.jar

/Users/yuanjicai/.m2/repository/yeecall/com/maven/maven01-ch/0.0.1-SNAPSHOT/maven01-ch-0.0.1-SNAPSHOT.jar

bogon:maven yuanjicai$

bogon:~ yuanjicai$ mkdir -p maven02-ch/src/{test,main}/java/yeecall/com/maven/ch02

bogon:~ yuanjicai$ vim maven02-ch/src/main/java/yeecall/com/maven/ch02/Hello.java

bogon:~ yuanjicai$ cat maven02-ch/src/main/java/yeecall/com/maven/ch02/Hello.java

package yeecall.com.maven.ch02;

import yeecall.com.maven.HelloMaven;

public class Hello

{

public String say(String name){

HelloMaven hm = new HelloMaven();

return hm.sayHello(name);

}

}

bogon:~ yuanjicai$ vim maven02-ch/pom.xml

bogon:~ yuanjicai$ cat maven02-ch/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 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>yeecall.com.maven</groupId>

<artifactId>maven02-ch</artifactId>

<version>0.0.1-SNAPSHOT</version>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.10</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.17</version>

</dependency>

<dependency>

<groupId>yeecall.com.maven</groupId>

<artifactId>maven01-ch</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

</dependencies>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

</project>

bogon:~ yuanjicai$

bogon:maven02-ch yuanjicai$ mvn compile

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building maven02-ch 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven02-ch ---

[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory /Users/yuanjicai/maven/maven02-ch/src/main/resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven02-ch ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to /Users/yuanjicai/maven/maven02-ch/target/classes

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 2.251 s

[INFO] Finished at: 2016-08-29T15:34:44+08:00

[INFO] Final Memory: 14M/210M

[INFO] ------------------------------------------------------------------------

bogon:maven02-ch yuanjicai$

小结:

mvn compile 命令会根据 pom.xml 中定义的dependencies 依赖,去maven 中心下载相关的包并进行编译,将编译后的文件放在 target/classes/目录中;

mvn test  命令会根据test目录中定义的测试文件对类进行编译测试,并把生成的测试报告存放在target/surefire-reports/目录中;

mvn clean 命令清除target 目录

mvn package 命令生成相关的jar包存放在 target目录中

mvn install 命令将生成的*.jar包复制到本地库中(~/.m2/repository/)

关于:mvn repository

本地工厂的位置 :

bogon:conf yuanjicai$ pwd

/opt/apache-maven-3.3.9/conf

bogon:conf yuanjicai$ grep "localRepository" settings.xml

<!-- localRepository

<localRepository>/path/to/local/repo</localRepository>

bogon:conf yuanjicai$

bogon:conf yuanjicai$ vim settings.xml

找到中央repository

bogon:lib yuanjicai$ pwd

/opt/apache-maven-3.3.9/lib

bogon:lib yuanjicai$ ls maven-model-builder-3.3.9.jar

maven-model-builder-3.3.9.jar

bogon:lib yuanjicai$

—> org —> apache —> maven —> model —> pom-4.0.0.xml

<repositories>

</repositories>

自动生成 pom.xml  

mvn  archetype:generate

mvn  archetype:generate -DgroupId=yeecall.com.maven -DartifactId=maven03-ch -Dversion=0.0.1-SNAPSHOT

时间: 2024-10-13 00:56:02

maven 基础的相关文章

Maven基础知识

本人博客文章网址:https://www.peretang.com/basic-knowledge-of-maven/ Maven简介 Maven , 用于Java项目的构建 , 依赖管理 , 项目信息管理的项目管理工具. 是Apache组织旗下的开源项目. Maven核心功能是一个具有跨平台特性的构建工具 , 帮助我们自动化构建 , 并提供一个完整的优化的构建生命周期模型 , 和构建任务所需的基础组件/插件. Maven还是一个依赖管理工具和项目信息管理工具 , 方便开发人员更好的管理依赖与避

Maven基础

maven是一个项目管理和构建自动化工具,最直接的讲,就是可以通过配置pom.xml文件关联jar仓库中的jar包,方便项目的管理.一般情况下,maven项目的结构都如下所示: Maven安装 1.下载maven安装包,例如:apache-maven-3.0.5(官网:http://maven.apache.org/)2.maven环境变量配置,配置方式跟jdk有些类似.新建环境变量MAVEN_HOME(值为maven的根目录).然后在PATH环境变量里加入%MAVEN_HOME%\bin;即可

(一)maven之——maven基础及本地仓库的配置

一.初步了解maven Apache Maven是一个软件项目管理的综合工具.基于项目对象模型(POM)的概念,提供了帮助管理构建.文档.报告.依赖.发布等方法,Maven简化和标准化项目建设过程.处理编译,分配,文档,团队协作和其他任务的无缝连接. Maven增加可重用性并负责建立相关的任务. maven的好处在于可以将项目过程规范化.自动化.高效化以及强大的可扩展性,利用maven自身及其插件还可以获得代码检查报告.单元测试覆盖率.实现持续集成等等. Maven的基本原理很简单,采用远程仓库

Maven基础知识和环境搭建

基本概念和生命周期 Maven是现在流行的构建自动化工具,提供了软件构建过程中全生命周期的管理. 基础目录结构 基础目录结构如下: 根目录:存放pom.xml 和所有的子目录 ${basedir}/src/main/java: java源代码 ${basedir}/src/main/resources: 资源文件,比如属性文件,配置文件 ${basedir}/src/test/java:单元测试代码 ${basedir}/src/test/resources:测试资源 编译打包以后会默认的产生

Maven 基础配置

pom.xml基础配置: maven中,最让我迷惑的还是那一堆配置! 就拿这个属性配置来说: Xml代码   <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.framework.version>3.0.5.RELEASE</spring.framework.version> <cxf.version>2.3.

Maven基础02

目录 第一章:IDEA中使用Maven 1.1-创建普通Java项目 1.2-创建JavaWeb项目 第二章:Maven常用命令 2.1-常用命令 2.2-IDEA中的Maven命令 2.3-Maven指令的生命周期 第三章:Maven的概念模型 第四章:依赖管理 4.1-Maven插件 4.2-导入依赖 4.3依赖范围 第五章:自定义MavenWeb骨架 第一章:IDEA中使用Maven 1.1-创建普通Java项目 打开IDEA,创建空的项目 步骤1: 步骤2: 步骤3: 创建模块 步骤1:

Maven基础知识(转)

文章摘自http://www.cnblogs.com/xing901022/p/4170248.html 谢谢楼主的总结,界面设计的很好看! 一.什么是Maven Maven是一个用于项目构建的工具,通过它便捷的管理项目的生命周期.即项目的jar包依赖,开发,测试,发布打包. 下面我自己总结一下它的几个特点,看了这些特点,也许对maven有更多的了解. 1 jar包依赖 这个也许会maven最突出的特点了使用maven不需要上网单独下载jar包,只需要在配置文件pom.xml中配置jar包的依赖

maven(基础介绍一)

maven:提供的作用有以下几点: 1 jar包依赖 这个也许会maven最突出的特点了使用maven不需要上网单独下载jar包,只需要在配置文件pom.xml中配置jar包的依赖关系,就可以自动的下载jar包到我们的项目中.这样,别人开发或者使用这个工程时,不需要来回的拷贝jar包,只需要复制这个pom.xml就可以自动的下载这些jar包. 而且,我们自己下载jar包,还有可能造成版本的不一致,这样在协同开发的过程中就有可能造成代码运行的不一致.通过使用maven精确的匹配jar包,就不会出现

maven基础知识(一)

在我们使用Maven之前首先需要下载Maven  下载地址:http://maven.apache.org/download.cgi 配置Maven环境变量 :path=apache-maven-3.1.1-bin\apache-maven-3.1.1\bin(bin目录所在的路径) 通过cmd命令打开docs窗口输入命令:mvn -v    显示一下信息,则说明安装成功 Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a;