从零创建maven工程
2016-04-11 16:40
232 查看
1.在某目录下创建maven工程根目录,如:HelloStudy文件夹
2.在HelloStudy文件夹下创建maven核心文件pom.xml
3.编写pom.xml
modelVersion指定了当前pom的版本,对于maven3来说只能是4.0.0;
groupId指定了工程的归属,如某公司或某基金会;
artifactId指定了项目在组中的唯一ID;
packaging指定了项目的打包方式;
name指定了maven工程的名字,可以省略;
version指定了当前工程的版本号。
4.编写项目代码
maven项目的代码路径为src/main/java,在此路径下创建HelloStudy类
maven一般会自动扫描src/main/java文件夹下的包和代码,包路径应当与groupId、artifactId相符,如代码所示。
5.maven初次编译
在项目根部路下运行命令mvn clean compile
编译,clean是清理target目录,compile是进行编译。
6.编写测试代码
maven项目的测试代码路径为src/test/java,在此路径下创建HelloStudyTest类
7.maven运行测试代码
在项目根部路下运行命令mvn clean test
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文件变为了:
编译完成后执行jar:
java -jar hellosutdy-1.0-SNAPSHOT.jar
输出:hello study
本文结束。
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
本文结束。
相关文章推荐
- 过程神经元网络
- maven部署应用到tomcat
- COGS738 [网络流24题] 数字梯形(最小费用最大流)
- 径向渐变
- Hibernate-Native SQL
- ASP.NET跨服务器上传文件的相关解决方案
- JDK源码阅读——ArrayList(2)
- 关闭程序
- Android中的Toast重复显示的问题
- Linux 下系统调用的三种方法
- window.location 与window.open的区别
- 比较两个数据库所有表的字段
- SQLite Page Cache之事务处理(2)
- 开启线程池 加入任务 传入参数 此任务能够在执行完成时得到返回值
- 【转载】 Java线程面试题 Top 50
- Javascript模块化编程(一):模块的写法
- IntelliJ IDEA 12 创建Web项目 教程 超详细版
- 设计模式六大原则(2):里氏替换原则
- AppDomain (转)
- ubuntu上传代码github