大數據爬蟲基礎(三) MAVEN的安裝配置和使用(上) -开发者知识库

大數據爬蟲基礎(三) MAVEN的安裝配置和使用(上) -开发者知识库,第1张

環境:

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;
成功

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复