初始化一个 maven 项目
在命令行中运行如下的命令mvn archetype:generate
使用交互式的方式生成项目。
也可以使用单行的命令mvn archetype:generate -DgourpId = your groupId -DartifactId=your artifactId -Dversion=your version -Dpackage=your package
Maven 依赖出现的冲突
可以使用如下的命令,如下的命令可以显示详细的依赖信息,包括冲突的和重复的依赖,内容是以一颗依赖树的形式展示的,如果出现冲突,生效的是离树根近的,那个包,远的依赖包将会被忽略。
mvn dependency:tree -Dverbose
Maven 工程中报 Missing artifact jdk.tools:jdk.tools
- jdk.tools:jdk.tools 是与 JDK 一起分发的一个 JAR 文件,可以如下方式加入到 Maven 项目中:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
详见 Maven FAQ for adding dependencies to tools.jar
- 也可以在本地仓库中手动安装 tools.jar,如下 mvn install:install-file -DgroupId=jdk.tools -DartifactId=jdk.tools -Dpackaging=jar -Dversion=1.7 -Dfile=tools.jar -DgeneratePom=true 然后在 pom.xml 中添加:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.6</version>
</dependency>
- How do I include tools.jar in my dependencies? The following code includes tools.jar for JDKs on Windows, Linux and Solaris (it is already included in the runtime for Mac OS X and some free JDKs).
<profiles>
<profile>
<id>default-tools.jar</id>
<activation>
<property>
<name>java.vendor</name>
<value>Sun Microsystems Inc.</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
...
参考:Frequently Asked Technical Questions
设置 jdk 的版本
设置全局的 jdk,在 setting.xml 文件中的 profiles 元素下添加如下 profile 元素。
<profile>
<id>jdk18</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
设置局部的 jdk,在项目的 pom,xml 文件中添加如下 build 元素
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Maven 导出依赖包
从 Maven 仓库中导出 jar 包:
mvn dependency:copy-dependencies
会导出到 Project 的 targed/dependency 下面
可以在 Project 创建 lib 文件夹,输入以下命令:
mvn dependency:copy-dependencies -DoutputDirectory=lib
这样 jar 包都会 copy 到 Project 目录下的 lib 里面
可以设置依赖级别,通常用编译需要的 jar
mvn dependency:copy-dependencies -DoutputDirectory=lib -DincludeScope=compile
Maven 配置自动下载源码
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
Maven 依赖中的 optional 元素
使用<optional>true</optional>
来避免传递依赖。
安装使用 maven wrapper
mvn -N io.takari:maven:wrapper -Dmaven=3.3.6
使用 maven 运行一个类
一、从命令行运行
1、运行前先编译代码,exec:java 不会自动编译代码,你需要手动执行 mvn compile 来完成编译。
mvn compile
2、编译完成后,执行 exec 运行 main 方法。
不需要传递参数:
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"
需要传递参数:
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"
指定对 classpath 的运行时依赖:
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
二、在 pom.xml 中指定某个阶段执行
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
将 CodeGenerator.main()方法的执行绑定到 maven 的 test 阶段,通过下面的命令可以执行 main 方法:
mvn test
三、在 pom.xml 中指定某个配置来执行
<profiles>
<profile>
<id>code-generator</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
将 2 中的配置用标签包裹后就能通过指定该配置文件来执行 main 方法,如下:
mvn test -Pcode-generator
注:通过以下命令可以获取 mvn exec 的其他配置参数说明。
mvn exec:help -Ddetail=true -Dgoal=java
Maven 下载所有的依赖的源码
mvn dependency:resolve -Dclassifier=sources
或者使用 mvn dependency:sources
Maven 下载所有依赖包的 doc 文档
mvn dependency:resolve -Dclassifier=javadoc
Maven 国内镜像
可以使用阿里的镜像
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>