参考 & 推荐
- Eleven_Lee - 项目管理利器——maven imooc的视频教程(2小时), 推荐看一遍, 快速入门.
配置maven的中央库
因为种种原因, 使用国内的源会快很多, 所以这一步先做好比较省时间.
编辑~/.m2/settings.xml
, 没有该文件的话直接创建即可.
gedit ~/.m2/settings.xml # 直接复制下面这段内容即可
ss
true
socks5
127.0.0.1
1080
127.0.0.1
alimaven
central
aliyun maven
http://maven.aliyun.com/nexus/content/repositories/central/
约定的目录格式
假设当前项目的根目录是App
.
- App
- pom.xml
maven项目的配置文件, 每个项目应该只有一个pom.xml - source code
src/main/java
存放源代码 - resources
src/main/resources
资源文件, 比如说MyBatis
等的XML配置文件. 该目录下的文件编译之后会被复制到target/classes
目录中, 所以在代码中直接用getResourceAsStrem()
方法就能得到相应的资源文件. - Test
src/test
存放测试用例 - target
target
存放各种目标文件 - target/classes
target/classes
存放编译后的class
和resources
文件夹中的文件.
class
按照package
建立相应的目录结构
src/main/resources
文件夹中的文件和目录被拷贝到该目录下, 比如说有src/main/resources/spring/config.xml
和src/main/resources/user.properties
文件在src/main/resources
目录下, 那么编译之后target/classes
中就会有spring/config.xml
和user.properties
.
- pom.xml
pom.xml基本配置
最简单配置
4.0.0
me.xiaofud
mabatis-learning
1.0-SNAPSHOT
jar
- 根元素是
You need to specify the basic schema settings such as apache schema and w3.org specification.
- Model version
写4.0.0即可 - groupId
类似包名
This is an Id of project's group. This is generally unique amongst an organization or a project. For example, a banking group com.company.bank has all bank related projects.
- artifactId
This is an Id of the project. This is generally name of the project. For example, consumer-banking. Along with the groupId, the artifactId defines the artifact's location within the repository.
- version
This is the version of the project. Along with the groupId, It is used within an artifact's repository to separate versions from each other. For example,
- com.company.bank:consumer-banking:1.0
- com.company.bank:consumer-banking:1.1.
添加依赖
往pom.xml文件中加了
, 其中添加了junit
和mybatis
以及mysql-connector-java
的依赖. 添加依赖的最基本设置, 就是在
内添加
. 然后输入该依赖的groupdId
, artifactId
, 以及version
即可. 依赖的这些属性可以到maven中央仓库搜索, 然后将groudId
这些配置信息复制过来即可.
4.0.0
me.xiaofud
mabatis-learning
1.0-SNAPSHOT
jar
mabatis-learning
your web site here
UTF-8
junit
junit
4.12
compile
org.mybatis
mybatis
3.4.2
mysql
mysql-connector-java
6.0.6
mvn compile 命令
在src/main/java
中写几个Java类, 然后往src/main/resources
文件中加一些资源文件, 就能很快使用maven
进行一次编译. 将命令行的工作目录设定到项目的根目录, 输入:
mvn compile
即可执行编译.
输出结果:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mabatis-learning 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ mabatis-learning ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mabatis-learning ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /home/smallfly/programming_projects/java/spring/mabatislearning/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.377 s
[INFO] Finished at: 2017-11-07T23:51:47+08:00
[INFO] Final Memory: 13M/156M
[INFO] ------------------------------------------------------------------------
此时发现当前目录下出现了target
文件夹, 里面的目录树如下:
target/
├── classes
│ ├── mapping
│ │ ├── job-mapper.xml
│ │ └── syllabus-mapper.xml
│ ├── me
│ │ └── xiaofud
│ │ ├── dao
│ │ │ ├── JobDao.class
│ │ │ └── SyllabusDao.class
│ │ ├── entity
│ │ │ ├── Comment.class
│ │ │ ├── Job.class
│ │ │ ├── Post.class
│ │ │ └── User.class
│ │ └── tests
│ │ ├── JobDaoTest.class
│ │ ├── SyllabusTest.class
│ │ └── TestWithoutXML.class
│ ├── mybatis-config.xml
│ ├── mybatis-syllabus-config.xml
│ └── properties
│ └── database.properties
对比src/main
文件
src/
├── main
│ ├── java
│ │ └── me
│ │ └── xiaofud
│ │ ├── dao
│ │ │ ├── JobDao.java
│ │ │ └── SyllabusDao.java
│ │ ├── entity
│ │ │ ├── Comment.java
│ │ │ ├── Job.java
│ │ │ ├── Post.java
│ │ │ └── User.java
│ │ └── tests
│ │ ├── JobDaoTest.java
│ │ ├── SyllabusTest.java
│ │ └── TestWithoutXML.java
│ └── resources
│ ├── mapping
│ │ ├── job-mapper.xml
│ │ └── syllabus-mapper.xml
│ ├── mybatis-config.xml
│ ├── mybatis-syllabus-config.xml
│ └── properties
│ └── database.properties
└── test
└── java
└── me
└── xiaofud
└── AppTest.java
使用maven生成WAR文件, 方便直接部署到Web容器中运行.
Maven-war-plugin/usage
项目结构
├── pom.xml
├── springmvc101.iml
└── src
└── main
├── java
│ └── me
│ └── xiaofud
│ └── spring101
│ └── controllers
│ └── MainController.java
├── resources
│ └── dispatcher-servlet.xml
├── test
└── webapp
├── index.html
├── index.jsp
└── WEB-INF
└── web.xml
pom.xml
4.0.0
me.xiaofud
springmvc101
war
1.0-SNAPSHOT
springmvc101 Maven Webapp
http://maven.apache.org
junit
junit
4.12
compile
org.springframework
spring-core
4.3.8.RELEASE
org.springframework
spring-beans
4.3.8.RELEASE
org.springframework
spring-aspects
4.3.8.RELEASE
org.springframework
spring-webmvc
4.3.8.RELEASE
org.springframework
spring-context
4.3.8.RELEASE
springmvc101
注意
, 指明了打包目标为war
类型.
在pom.xml
所在目录, 直接运行:
mvn package
输出:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springmvc101 Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ springmvc101 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ springmvc101 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/smallfly/programming_projects/java/spring/springmvc101/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ springmvc101 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/smallfly/programming_projects/java/spring/springmvc101/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ springmvc101 ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ springmvc101 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ springmvc101 ---
[INFO] Packaging webapp
[INFO] Assembling webapp [springmvc101] in [/home/smallfly/programming_projects/java/spring/springmvc101/target/springmvc101]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/smallfly/programming_projects/java/spring/springmvc101/src/main/webapp]
[INFO] Webapp assembled in [56 msecs]
[INFO] Building war: /home/smallfly/programming_projects/java/spring/springmvc101/target/springmvc101.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.125 s
[INFO] Finished at: 2017-11-09T00:04:25+08:00
[INFO] Final Memory: 18M/191M
[INFO] ------------------------------------------------------------------------
target
目录结构:
├── classes
│ ├── dispatcher-servlet.xml
│ └── me
│ └── xiaofud
│ └── spring101
│ └── controllers
│ └── MainController.class
├── maven-archiver
│ └── pom.properties
├── maven-status
│ └── maven-compiler-plugin
│ └── compile
│ └── default-compile
│ ├── createdFiles.lst
│ └── inputFiles.lst
├── springmvc101
│ ├── index.html
│ ├── index.jsp
│ ├── META-INF
│ └── WEB-INF
│ ├── classes
│ │ ├── dispatcher-servlet.xml
│ │ └── me
│ │ └── xiaofud
│ │ └── spring101
│ │ └── controllers
│ │ └── MainController.class
│ ├── lib
│ │ ├── aspectjweaver-1.8.9.jar
│ │ ├── commons-logging-1.2.jar
│ │ ├── hamcrest-core-1.3.jar
│ │ ├── junit-4.12.jar
│ │ ├── spring-aop-4.3.8.RELEASE.jar
│ │ ├── spring-aspects-4.3.8.RELEASE.jar
│ │ ├── spring-beans-4.3.8.RELEASE.jar
│ │ ├── spring-context-4.3.8.RELEASE.jar
│ │ ├── spring-core-4.3.8.RELEASE.jar
│ │ ├── spring-expression-4.3.8.RELEASE.jar
│ │ ├── spring-web-4.3.8.RELEASE.jar
│ │ └── spring-webmvc-4.3.8.RELEASE.jar
│ └── web.xml
└── springmvc101.war
可以看到, mvn帮我们生成了war
文件, 以及war-exploded
形式的文件夹(springmvc101
).
我们将.war
文件或者war-exploded
文件夹直接拷贝到Tomcat
容器的webapps
目录下, 即完成了部署.
如果packaging的值不是war
- 使用
war:war
goal
mvn compile war:war # 将编译项目, 以及生成`war`和`war-exploded`
- 使用
war:exploded
mvn compile war:exploded # 仅仅生成`war-exploded`形式
- 使用
war:inplace
Another variation of war:exploded is war:inplace. With war:inplace the exploded WAR is created in the webapp source, which defaults to src/main/webapp
即将war-exploded
存到src/main/webapp
中.
mvn compile war:inplace
finalName
存放war-exploded
的文件夹名称, 默认是target/
. 其中finalName
是
, 可以被配置覆盖.
在
标签中加入
标签, 即可覆盖该属性.
springmvc101
mvn clean
用于删除上一次build
过后产生的文件.
mvn archetype的简单使用
introduction-to-archetypes
archetype - usage
mkyong - How to create a Web Application Project with Maven
使用archetype
可以快速创建指定类型的项目骨架.
以创建webapp
骨架为例, 介绍使用mvn archetype
的使用.
在希望存放项目的目录中运行:
mvn archetype:generate -DgroupId=your_group_id -DartifactId=your_project_name -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
如果希望进入交互模式创建项目, 那么直接mvn archetype:generate
即可.
实例:
mvn archetype:generate -DgroupId=me.xiaofu.d -DartifactId=amazingapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
生成的项目骨架:
tree amazingapp/
amazingapp/
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── index.jsp
└── WEB-INF
└── web.xml