从零创建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
本文结束。
- 从零创建MAVEN工程
- 从零创建maven工程
- 从零基础认识maven工程构建
- 从现有maven工程创建maven archetype
- 从现有maven工程创建maven archetype .
- 5分钟从零搭建一个maven工程
- 【源】从零自学Hadoop(09):使用Maven构建Hadoop工程
- maven 创建的工程 或是从Svn上导出的maven工程不能被eclipse识别为web工程
- maven 创建的工程 或是从Svn上导出的maven工程不能被eclipse识别为web工程
- 创建maven工程
- 使用maven创建工程
- maven创建工程
- maven 创建工程
- 创建maven web 工程
- maven 创建springMVC工程
- MyEclipse创建Maven工程
- Maven 创建springmvc工程
- MyEclipse创建Maven工程
- 二、2440 裸机 中断原理分析
- [从头学数学] 第173节 圆与方程
- 亿级高并发系统的监控与报警
- 在部署iis的web服务过程中遇到的问题
- 百度地图只出现网格的问题
- 从零创建maven工程
- STL-string(4.12)
- DPM Voc release 5在CentOS 6上运行
- caffe源码分析:softmax_layer.cpp && softmax_loss_layer.cpp
- px 与 dp, sp换算公式?
- decimal double
- oracle 基本查询
- API常用类(String类)
- 利用简洁的图片预加载组件提升h5移动页面的用户体验