从零创建maven工程

1.在某目录下创建maven工程根目录,如:HelloStudy文件夹
2.在HelloStudy文件夹下创建maven核心文件pom.xml
3.编写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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wqw.study</groupId>
    <artifactId>hellosutdy</artifactId>
    <packaging>jar</packaging>
    <name>wqw hellostudy</name>
    <version>1.0-SNAPSHOT</version>
</project>

modelVersion指定了当前pom的版本,对于maven3来说只能是4.0.0;
groupId指定了工程的归属,如某公司或某基金会;
artifactId指定了项目在组中的唯一ID;
packaging指定了项目的打包方式;
name指定了maven工程的名字,可以省略;
version指定了当前工程的版本号。

4.编写项目代码
maven项目的代码路径为src/main/java,在此路径下创建HelloStudy类

package com.wqw.study.hellostudy;

public class HelloStudy{

    public static void main(String[] args){

        System.out.println("hello study");

    }

}

maven一般会自动扫描src/main/java文件夹下的包和代码,包路径应当与groupId、artifactId相符,如代码所示。
5.maven初次编译
在项目根部路下运行命令mvn clean compile

F:\Study\HelloStudy>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building wqw hellostudy 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hellosutdy ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hellosutdy
 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\Study\HelloStudy\src\main\resource
s
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hellosutdy ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to F:\Study\HelloStudy\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.262 s
[INFO] Finished at: 2016-04-11T16:02:16+08:00
[INFO] Final Memory: 14M/199M
[INFO] ------------------------------------------------------------------------

编译,clean是清理target目录,compile是进行编译。

6.编写测试代码
maven项目的测试代码路径为src/test/java,在此路径下创建HelloStudyTest类

package com.wqw.study.hellostudy;

public class HelloStudyTest{

    @org.junit.Test
    public void test(){

        System.out.println("hello study test");

    }

}

7.maven运行测试代码

在项目根部路下运行命令mvn clean test

F:\Study\HelloStudy>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building wqw hellostudy 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hellosutdy ---
[INFO] Deleting F:\Study\HelloStudy\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hellosutdy
 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\Study\HelloStudy\src\main\resource
s
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hellosutdy ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to F:\Study\HelloStudy\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
llosutdy ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\Study\HelloStudy\src\test\resource
s
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hellosu
tdy ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to F:\Study\HelloStudy\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hellosutdy ---
[INFO] Surefire report directory: F:\Study\HelloStudy\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.wqw.study.hellostudy.HelloStudyTest
hello study test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.264 s
[INFO] Finished at: 2016-04-11T16:24:38+08:00
[INFO] Final Memory: 15M/166M

8.maven打包和运行

打包命令:mvn clean package
输出:hellosutdy-1.0-SNAPSHOT.jar
如何让其他项目可以引用此包:
install命令:mvn clean install
输出:在.m2文件夹下可以找到.m2\repository\com\wqw\study\helloworld文件夹
执行jar,默认打包的jar是不能够直接运行的,需要添加maven-shade-plugin插件进行编译,最终pom文件变为了:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wqw.study</groupId>
    <artifactId>hellosutdy</artifactId>
    <packaging>jar</packaging>
    <name>wqw hellostudy</name>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
        <build>
    <plugins>
        <plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-shade-plugin</artifactId>  
    <version>1.4</version>  
    <executions>  
      <execution>  
        <phase>package</phase>  
        <goals>  
          <goal>shade</goal>  
        </goals>  
        <configuration>  
          <transformers>  
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
              <mainClass>com.wqw.study.hellostudy.HelloStudy</mainClass>  
            </transformer>  
          </transformers>  
        </configuration>  
      </execution>  
    </executions>  
  </plugin>  
    </plugins>
  </build>
</project>

编译完成后执行jar:

java -jar hellosutdy-1.0-SNAPSHOT.jar
输出:hello study

本文结束。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值