您的位置:首页 > 其它

项目构建工具 maven构建项目目录结构与mvn命令基本使用

2019-03-01 21:35 393 查看

1.maven 项目目录结构

项目:   |-src

|main:主业务逻辑

|-java

|-resources :资源目录

|-test:  测试目录

|-java: 测试代码目录

|-resources:测试代码依赖的配置文件

|-target:   包括源码和测试编译输出目录,打包输出目录,测试报告等

|-pom.xml    # maven配置文件

2.pom.xml配置文件详解

假如我们的项目需要用到spring jdbc的jar包

<?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.mycat</groupId>
<artifactId>mvn-test</artifactId>
<version>1.0</version>  <!-- 大版本.小版本.小幅bug修改-->
<!--
GA/RELEASE:稳定版(推荐)
BEAT:公测
SNAPSHOT:快照版(不稳定版)
-->
<properties>
<spring.version>4.3.8.RELEASE</spring.version>
</properties>
<dependencies>
<dependency><!-- 配置spring jdbc的依赖 -->
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

其中

modelVersion
标签声明pom的xml模板的版本,与上面的shcema依赖版本对应,不改。

modelVersion
紧接着下面的
groupId
artifactId
以及
version
则定义的是本项目打包后在仓库的坐标。

properties
标签通常用来定义统一的版本信息,例如上述的
<spring.version> 4.3.8.RELEASE <spring.version>
配置定义了spring的版本信息,因此下面使用spring-jdbc的坐标时,版本信息
${spring.version}
则采用的此配置。

dependencies
定义了引用的依赖的jar包的坐标的集合,主要引用来源是本地仓库和远程仓库,其中重要仓库引用网址是:
https://mvnrepository.com/tags/maven
,可以通过该网址直接搜索需要的maven依赖坐标,快速构建项目。

build
标签里面主要存放的是编译时的配置,包括插件的配置(
plugins
)等。

2.maven常用命令的使用

1) 本地创建maven测试项目:

项目结构

hellomvnuse目录:(项目根目录)(我把无关打印信息删除了)

C:\Users\mycat\Desktop\hellomvnuse>dir
2019/03/01  11:25               701 pom.xml
2019/03/01  11:09    <DIR>          src

src目录:(包括主程序目录和测试程序主目录)

C:\Users\mycat\Desktop\hellomvnuse\src>dir
2019/03/01  10:42    <DIR>          main
2019/03/01  11:03    <DIR>          test

main目录:(主程序目录)

C:\Users\mycat\Desktop\hellomvnuse\src\main>dir
2019/03/01  11:02    <DIR>          java

test目录:(测试目录)

C:\Users\mycat\Desktop\hellomvnuse\src\test>dir
2019/03/01  11:04    <DIR>          java

而main和test的java目录下均有com目录,com目录下有mycat目录,mycat下有core目录,对于main来说其最里面的core目录中有HelloWorld.java文件,而test最里面的core目录下有TestHelloWorld.java(均为文本编辑器写的)

C:\Users\mycat\Desktop\hellomvnuse\src\main\java\com\mycat\core>dir
2019/03/01  10:46               160 HelloWorld.java

C:\Users\mycat\Desktop\hellomvnuse\src\test\java\com\mycat\core>dir
2019/03/01  11:18               153 TestHelloWorld.java

其中HelloWorld.java:

package com.mycat.core;
public class HelloWorld{
public static void main(String[] args){

}
public static String sayHello(){
return "hello";
}
}

而TestHelloWorld.java:(使用单元测试)

package com.mycat.core;

import org.junit.*;   // 注意需要别忘了添加

public class TestHelloWorld{
@Test
public void m(){
System.out.println("Hello World!!!");
}
}

由于在测试程序中使用到了Junit4,所以需要在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.mycat</groupId>
<artifactId>mvn-test</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2)使用mvn的命令进行编译(pom.xml所在目录,即项目根目录下)

对主程序代码执行编译:

mvn  compile

显示结果:

C:\Users\mycat\Desktop\hellomvnuse>mvn compile
[INFO] Scanning for projects...
[INFO]
[
19953
INFO] -------------------------< com.mycat:mvn-test >-------------------------
[INFO] Building mvn-test 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvn-test ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\mycat\Desktop\hellomvnuse\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvn-test ---
[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 C:\Users\mycat\Desktop\hellomvnuse\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.038 s
[INFO] Finished at: 2019-03-01T21:04:42+08:00
[INFO] ------------------------------------------------------------------------

看到 BUILD SUCCESS即代表编译成功。更明显的效果是当前目录下会生成一个target目录,

C:\Users\mycat\Desktop\hellomvnuse\target>dir
2019/03/01  21:04    <DIR>          classes
2019/03/01  21:04    <DIR>          generated-sources
2019/03/01  21:04    <DIR>          maven-status

其中:

classes目录
:主程序编译目录

generated-sources目录
:注解项目编译结果

maven-status目录
:maven状态插件

对测试代码执行编译:

切换回根目录

mvn  test
C:\Users\mycat\Desktop\hellomvnuse>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.mycat:mvn-test >-------------------------
[INFO] Building mvn-test 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvn-test ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\mycat\Desktop\hellomvnuse\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvn-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mvn-test ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\mycat\Desktop\hellomvnuse\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvn-test ---
[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 C:\Users\mycat\Desktop\hellomvnuse\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvn-test ---
[INFO] Surefire report directory: C:\Users\mycat\Desktop\hellomvnuse\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycat.core.TestHelloWorld
Hello World!!!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.064 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.911 s
[INFO] Finished at: 2019-03-01T21:11:28+08:00
[INFO] ------------------------------------------------------------------------

出现 BUILD SUCCESS即表示成功,同时target目录多了三个目录generated-test-sources, surefire-reports(测试报告), test-classes(测试生成的class字节码文件),我尝试把target删除掉再执行上述命令,发现还是会编译主程序代码,可见这个命令是联通源码一起编译。

C:\Users\mycat\Desktop\hellomvnuse\target>dir
2019/03/01  21:04    <DIR>          classes
2019/03/01  21:04    <DIR>          generated-sources
2019/03/01  21:11    <DIR>          generated-test-sources
2019/03/01  21:04    <DIR>          maven-status
2019/03/01  21:11    <DIR>          surefire-reports
2019/03/01  21:11    <DIR>          test-classes

接下来(清空编译结果 => 最直观的结果是清空target目录,并删除target目录):

mvn clean

打成jar包:(

中间clean
可省,此处是为了演示清空编译后重新编译再打包)(命令配合着使用)

mvn clean package
C:\Users\mycat\Desktop\hellomvnuse>mvn clean package[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.mycat:mvn-test >-------------------------
[INFO] Building mvn-test 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mvn-test ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvn-test ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\mycat\Desktop\hellomvnuse\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvn-test ---
[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 C:\Users\mycat\Desktop\hellomvnuse\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mvn-test ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\mycat\Desktop\hellomvnuse\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvn-test ---
[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 C:\Users\mycat\Desktop\hellomvnuse\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvn-test ---
[INFO] Surefire report directory: C:\Users\mycat\Desktop\hellomvnuse\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycat.core.TestHelloWorld
Hello World!!!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.067 sec

Results :

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

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvn-test ---
[INFO] Building jar: C:\Users\mycat\Desktop\hellomvnuse\target\mvn-test-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.204 s
[INFO] Finished at: 2019-03-01T21:22:00+08:00
[INFO] ------------------------------------------------------------------------

此项打包的动作:先编译主程序源码,再编译测试程序,然后运行,最后执行打包。但是很多时候是不需要对测试程序进行测试的。

使用下面命令跳过测试进行打包:

mvn package -Dmaven.test.skip=true

注:

mvn pakage 打包,动态 web工程打 war包,Java工程打 jar 包

最后:

mvn install

该命令的作用是将本项目打包后发布到本地maven仓库,以便别的模块调用

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: