Maven 笔记
Maven 下载
http://maven.apache.org/
安装Maven
解压
![](https://img-blog.csdn.net/20151201142635394)
配置环境变量
测试是否安装成功
cmd mvn -version
使用命令行工具构建一个 Maven 项目
mvn archetype:generate
打包
mvn package
java -cp target/mvnstudy-1.0-SNAPSHOT.jar com.andean.App
使用 Eclipse 构建一个 Maven 项目
安装Maven插件
设置环境
创建项目
打包
可以设一个环境变量M2_HOME指向你的maven安装目录
M2_HOME=D:\Maven\apache-maven-3.3.9
然后在Window->Preference->Java->Installed JREs->Edit
在Default VM arguments中设置
-Dmaven.multiModuleProjectDirectory=$M2_HOME
M2_HOME=D:\Maven\apache-maven-3.3.9
然后在Window->Preference->Java->Installed JREs->Edit
在Default VM arguments中设置
-Dmaven.multiModuleProjectDirectory=$M2_HOME
运行
导入Maven项目
使用 IntelliJIDEA 构建一个 Maven 项目
修改本地仓库
D:\Maven\apache-maven-3.3.9\conf\settings.xml
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
添加远程仓库
E:\workspace_maven\mvnstudy\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>com.andean</groupId>
<artifactId>mvnstudy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mvnstudy</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>oschina</id>
<name>oschina repository</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
项目站点报告
cd E:\workspace_maven\mvnstudy
mvn site
生成 site文件夹
E:\workspace_maven\mvnstudy\target\site
修改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>com.andean</groupId>
<artifactId>mvnstudy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mvnstudy</name>
<description>这是一个测试项目。</description><!-- 描述 -->
<url>http://www.baidu.com</url><!-- 项目的URL -->
<organization><!-- 项目维护组的信息 -->
<name>MyProject</name>
<url>http://www.baidu.com</url>
</organization>
<developers><!-- 项目组团队的信息 -->
<developer>
<id>zhangsang</id>
<name>zhangsang</name>
<email>zhangsang@qq.com</email>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>oschina</id>
<name>oschina repository</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 配置设置 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<configuration>
<locales>zh_CN</locales>
</configuration>
</plugin>
</plugins>
</build>
</project>
E:\workspace_maven\mvnstudy>mvn clean site
运行E:\workspace_maven\mvnstudy\target\site\index.html 查看效果
java -cp target/mvnstudy-0.0.1-SNAPSHOT.jar com.andean.App
Hello World!
要添加主清单属性
E:\IdeaProjects\mvnstudy>java -jar target/mvnstudy-0.0.1-SNAPSHOT.jar
Hello World!
Hello World!
要添加主清单属性
<!--主清单属性-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.andean.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
E:\IdeaProjects\mvnstudy>java -jar target/mvnstudy-0.0.1-SNAPSHOT.jar
Hello World!
其他命令
运行清理及编译命令:mvn clean compile
运行测试命令:mvn clean test
运行打包命令:mvn clean package
运行安装命令:mvn clean install
运行测试命令:mvn clean test
运行打包命令:mvn clean package
运行安装命令:mvn clean install