从零创建maven工程

来源:互联网 发布:java合并两个单链表 编辑:程序博客网 时间:2024/06/06 20:51

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\resources[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. build 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\resources[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. build is platform dependent![INFO] Compiling 1 source file to F:\Study\HelloStudy\target\classes[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ 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\test\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hellosutdy ---[INFO] Changes detected - recompiling the module![WARNING] File encoding has not been set, using platform encoding GBK, i.e. build 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.HelloStudyTesthello study testTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 secResults :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

本文结束。

1 0
原创粉丝点击