參考 & 推薦
- Eleven_Lee - 項目管理利器——maven imooc的視頻教程(2小時), 推薦看一遍, 快速入門.
配置maven的中央庫
因為種種原因, 使用國內的源會快很多, 所以這一步先做好比較省時間.
編輯~/.m2/settings.xml
, 沒有該文件的話直接創(chuàng)建即可.
gedit ~/.m2/settings.xml # 直接復制下面這段內容即可
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<proxies>
<proxy>
<id>ss</id>
<active>true</active>
<protocol>socks5</protocol>
<username></username>
<password></password>
<host>127.0.0.1</host>
<port>1080</port>
<nonProxyHosts>127.0.0.1</nonProxyHosts>
</proxy>
</proxies>
<mirrors>
<!-- 阿里云倉庫 -->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!--
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
-->
</mirrors>
</settings>
約定的目錄格式
假設當前項目的根目錄是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基本配置
最簡單配置
<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>me.xiaofud</groupId>
<artifactId>mabatis-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
- 根元素是
<project>
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文件中加了<dependencies>
, 其中添加了junit
和mybatis
以及mysql-connector-java
的依賴. 添加依賴的最基本設置, 就是在<dependencies>
內添加<dependency>
. 然后輸入該依賴的groupdId
, artifactId
, 以及version
即可. 依賴的這些屬性可以到maven中央倉庫搜索, 然后將groudId
這些配置信息復制過來即可.
<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>me.xiaofud</groupId>
<artifactId>mabatis-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!--The full name of the project.-->
<name>mabatis-learning</name>
<!--URL-->
<url>your web site here</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
</project>
mvn compile 命令
在src/main/java
中寫幾個Java類, 然后往src/main/resources
文件中加一些資源文件, 就能很快使用maven
進行一次編譯. 將命令行的工作目錄設定到項目的根目錄, 輸入:
mvn compile
即可執(zhí)行編譯.
輸出結果:
[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] ------------------------------------------------------------------------
此時發(fā)現(xiàn)當前目錄下出現(xiàn)了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容器中運行.
項目結構
├── 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
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.xiaofud</groupId>
<artifactId>springmvc101</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvc101 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc101</finalName>
</build>
</project>
注意<packaging>war</packaging>
, 指明了打包目標為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>
. 其中finalName
是<artifactId>-<version>
, 可以被配置覆蓋.
在<build>
標簽中加入<finalName>
標簽, 即可覆蓋該屬性.
<build>
<finalName>springmvc101</finalName>
</build>
mvn clean
用于刪除上一次build
過后產生的文件.
mvn archetype的簡單使用
introduction-to-archetypes
archetype - usage
mkyong - How to create a Web Application Project with Maven
使用archetype
可以快速創(chuàng)建指定類型的項目骨架.
以創(chuàng)建webapp
骨架為例, 介紹使用mvn archetype
的使用.
在希望存放項目的目錄中運行:
mvn archetype:generate -DgroupId=your_group_id -DartifactId=your_project_name -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
如果希望進入交互模式創(chuàng)建項目, 那么直接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