maven 基础

  1. 云栖社区>
  2. 博客>
  3. 正文

maven 基础

余二五 2017-11-14 18:18:00 浏览545
展开阅读全文

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










本文转自 meteor_hy 51CTO博客,原文链接:http://blog.51cto.com/caiyuanji/1843971,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
余二五
+ 关注