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

《Maven [1] : Create a new project》 Paste_Image.png

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

AppTest.java

《Maven [1] : Create a new project》 Paste_Image.png

Package the Project

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

cd helloworld

mvn package

《Maven [1] : Create a new project》 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>

《Maven [1] : Create a new project》 Paste_Image.png
《Maven [1] : Create a new project》 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
(如果之前没有下载过的话)

《Maven [1] : Create a new project》 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 入门篇(下)

    原文作者:1Z实验室阿凯
    原文地址: https://www.jianshu.com/p/6e18db893088
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞