Maven [1] : Create a new project

检查maven是否正确安装

mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: C:\Users\Scorpion\software\apache-maven-3.3.9
Java version: 1.8.0_71, vendor: Oracle Corporation
Java home: C:\Program Files (x86)\Java\jdk1.8.0_71\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "x86", family: "dos"

Create a new project

mvn archetype:generate -DgroupId=com.fzclass.helloworld -DartifactId=helloworld -Dpackag=com.fzclass.helloworld -Dversion=1.0-SNAPSHOT

执行效果

C:.
└─src
    ├─main
    │  └─java
    │      └─com
    │          └─fzclass
    │              └─helloworld
    └─test
        └─java
            └─com
                └─fzclass
                    └─helloworld

自动生成了两个文件夹
1.main 开发用例
2.test 测试用例

在main下自动生成了App.java

App.java

Paste_Image.png

在test目录下面生成了AppTest.java

AppTest.java

Paste_Image.png

Package the Project

当你第一次运行 maven 的时候,它会从网上的 maven 库 (repository) 下载需要的程序,存放在你电脑的本地库 (local repository) 中,所以这个时候你需要有 Internet 连接。Maven 默认的本地库是 ~/.m2/repository/ ,在 Windows 下是 %USER_HOME%.m2\repository\ 。

cd helloworld

mvn package
Paste_Image.png

然而不幸的事情发生了

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project helloworld: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test failed: Plugin org.apache.maven.plugins:maven-surefire-plugin:2.12.4 or one of its dependencies could
not be resolved: Failed to collect dependencies at org.apache.maven.plugins:maven-surefire-plugin:jar:2.12.4 -> org.apache.maven.surefire:surefire-booter:jar:2.12.4: Failed to read artifact descriptor for org.apache.maven.surefire:surefire-booter:jar:2.12.4: Could not transfer artifact org.apache.maven.surefire:surefire-booter:pom:2.12.4 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/199.27.79.215] failed: Read timed out -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

错误提示
In your error code he didn't find surefire plugin
缺少这个名字叫做surefire的插件所以在pom.xml 中的
添加代码片段

  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.0</version>
              <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
      </plugins>
  </build>
Paste_Image.png
Paste_Image.png

好那么我们重新执行一次 mvn package

(其实是又一次的惨不忍睹,看报错信息喽 )缺哪个plugin然后就添加到<build>里面

 <build>
    <!--这里的helloworld-demo是自己起的名字 ,最终打包之后在target目录下面会生成一个helloworld-demo.jar -->
    <finalName>helloworld-demo</finalName>
    <plugins>
      
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.2.1.v20140609</version>
        <configuration>
          <scanIntervalSeconds>5</scanIntervalSeconds>
          <webAppConfig>
            <contextPath>/${project.artifactId}</contextPath>
          </webAppConfig>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
      </plugin>
    </plugins>
  </build>  

这次终于可以了
执行 mvn pakcage 之后
在Windows系统下,maven在不停的下载对应的jar包到 %USER_HOME%.m2\repository
(如果之前没有下载过的话)

Paste_Image.png

密密麻麻终于下完了,让我们看一下文件的变化

├─src
│  ├─main
│  │  └─java
│  │      └─com
│  │          └─fzclass
│  │              └─helloworld
│  └─test
│      └─java
│          └─com
│              └─fzclass
│                  └─helloworld
└─target
    ├─classes
    │  └─com
    │      └─fzclass
    │          └─helloworld
    ├─maven-archiver
    ├─maven-status
    │  └─maven-compiler-plugin
    │      ├─compile
    │      │  └─default-compile
    │      └─testCompile
    │          └─default-testCompile
    ├─surefire-reports
    └─test-classes
        └─com
            └─fzclass
                └─helloworld

多出了target这个文件夹

这个时候, maven 在 helloworld 下面建立了一个新的目录 target/ ,构建打包后的 jar 文件 helloworld-1.0-SNAPSHOT.jar 就存放在这个目录下。编译后的 class 文件放在 target/classes/ 目录下面,测试 class 文件放在 target/test-classes/ 目录下面。

测试一下效果

~\helloworld>java -cp target/helloworld-demo.jar com.fzclass.helloworld.App

Hello World!

Note: cp
-cp <目录和 zip/jar 文件的类搜索路径>

mvn clean

接下来我想删除刚刚编译生成的文件所以执行

mvn clean

文件目录又恢复了原样

C:.
└─src
    ├─main
    │  └─java
    │      └─com
    │          └─fzclass
    │              └─helloworld
    └─test
        └─java
            └─com
                └─fzclass
                    └─helloworld

Reference Links

Maven的安装、配置及使用入门
Apache Maven 入门篇 ( 上 )
Apache Maven 入门篇(下)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,165评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,503评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,295评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,589评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,439评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,342评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,749评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,397评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,700评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,740评论 2 313
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,523评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,364评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,755评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,024评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,297评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,721评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,918评论 2 336

推荐阅读更多精彩内容