环境:
ubuntu server 14.04.04 amd64 jdk1.8
1. 安装:
http://www.jikexueyuan.com/course/571_4.html?ss=1下载、解压、M2_HOME、PATH
mvn -version
2. 使用
http://www.jikexueyuan.com/course/maven/
2.1 java代码
package com.jikexueyuan
public class App{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
2.2 构建maven项目骨架
root@spark:~# mvn archetype:generate
结果:
1606: remote -> us.fatehi:schemacrawler-archetype-plugin-command (-)
1607: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
1608: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 792:
第一次用mvn会下载很多jar文件,比较慢,可以看到中央仓库有1608个插件,默认提供792编号的插件,enter选择默认;
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
版本中默认选择6 quickstart,回车
groupId项目所属组标识
artifactId项目名称
version项目的版本,用默认,回车
package项目名称,默认和artifactId一样的,回车
输入Y确认以上配置
Define value for property 'groupId': : com.jikexueyuan
Define value for property 'artifactId': : mvnstudy
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.jikexueyuan: :
Confirm properties configuration:
groupId: com.jikexueyuan
artifactId: maven
version: 1.0-SNAPSHOT
package: com.jikexueyuan
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: /root
[INFO] Parameter: package, Value: com.jikexueyuan
[INFO] Parameter: groupId, Value: com.jikexueyuan
[INFO] Parameter: artifactId, Value: maven
[INFO] Parameter: packageName, Value: com.jikexueyuan
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /root/maven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:51 min
[INFO] Finished at: 2016-06-10T13:55:08+08:00
[INFO] Final Memory: 14M/52M
[INFO] ------------------------------------------------------------------------
root@spark:~# ls
cleanjob hadoop.sh input sbt-0.13.11.deb WordCount
css.sh hello metastore_db target
derby.log image.jpg mvnstudy testtable.java
root@spark:~/mvnstudy/src/main/java/com/jikexueyuan# ls
App.java
打包
root@spark:~/mvnstudy# mvn package
结果:
utils/3.0/plexus-utils-3.0.jar (221 KB at 130.3 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.jar (181 KB at 90.5 KB/sec)
[INFO] Building jar: /root/mvnstudy/target/mvnstudy-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:30 min
[INFO] Finished at: 2016-06-10T14:04:01+08:00
[INFO] Final Memory: 17M/41M
[INFO] ------------------------------------------------------------------------
查看jar文件
root@spark:~/mvnstudy# ls
pom.xml src target
root@spark:~/mvnstudy# ls target/
classes maven-status surefire-reports
maven-archiver mvnstudy-1.0-SNAPSHOT.jar test-classes
root@spark:~/mvnstudy#
运行
root@spark:~/mvnstudy# java -cp target/mvnstudy-1.0-SNAPSHOT.jar com.jikexueyuan.App
Hello World!
成功。
参考:
eclipse maven:http://www.jikexueyuan.com/course/580_1.html
放自己的java代码:比如MvnTest.java
内容如下:
package com.test;
/**
* Hello world!
*
*/
import java.util.Date;
public class MvnTest {
public static void main(String[] args) throws Exception {
Date d=new Date();
Class cl=d.getClass();
String name=cl.getName();
System.out.println(name);
String className="java.util.Date";
Class cl2=Class.forName(className);
System.out.println(cl2);
Class cl11=Date.class;
System.out.println(cl11);
Class cl12=int.class;
Class cl13=Double[].class;
System.out.println(cl12);
System.out.println(cl13);
}
}
root@spark:~/mvntest/src/main/java/com/test# ls
App.java MvnTest.java
打包
cd ~/mvntest
mvn package
root@spark:~/mvntest/target# ls
classes maven-status surefire-reports
maven-archiver mvntest-1.0-SNAPSHOT.jar test-classes
运行
root@spark:~/mvntest# java -cp target/mvntest-1.0-SNAPSHOT.jar com.test.MvnTest
结果:
java.util.Date
class java.util.Date
class java.util.Date
int
class [Ljava.lang.Double;
成功