Maven常用参数

1.常用命令参数
进行编译

mvn compile

进行打包

#测试并打包
mvn package
#直接打包,不进行测试
mvn package -DskipTests=true
#打Jar包
mvn jar:jar
#打War包
mvn war:war
#打War包但不执行最后的压缩
mvn war:exploded

进行测试

#编译单元测试
mvn test-compile
#编译并允许单元测试
mvn test

安装到本地repository

mvn install
mvn install -DskipTests=true
mvn install -DskipTests=true --fail-at-end
#安装jar包到本地仓库
mvn install:install-file -DgroupId=groupId -DartifactId=artifactId -Dversion=1.0.0 -Dpackaging=jar -Dfile=xxx.jar

安装到远程repository

mvn deploy

进行清理

mvn clean

生成网站

#生成网站
mvn site
#生成网站并发布
mvn site-deploy

打包源码包

#打包源码
mvn source:jar
#下载项目源码
mvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true

打包测试源码包

mvn source:test-jar

拷贝依赖到target目录

#拷贝依赖
mvn dependency:copy-dependencies
#依赖分析
mvn dependency:analyze
mvn dependency:tree

生成ecplise项目

mvn eclipse:eclipse

生成ecplise项目

mvn idea:idea

生成文档

#单模块项目生成文档
mvn javadoc:javadoc
#多模块项目生成文档
mvn javadoc:aggregate

创建Jar项目

mvn archetype:create -DgroupId=gourpid -DartifactId=artifactId

创建War项目

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=gourpid  -DartifactId=artifactId

2.settings.xml

<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"]

<localRepository>X:/Maven/repository</localRepository>

<servers>
<server>
<id>neo-public</id>
<username>username</username>
<password>password</password>
</server>
<server>
<id>neo-releases</id>
<username>username</username>
<password>password</password>
</server>
<server>
<id>neo-snapshots</id>
<username>username</username>
<password>password</password>
</server>
</servers>

<mirrors>
<mirror>
<id>neo-public</id>
<name>Neohope Nexux Public Mirror</name>
<url>http://192.168.xxx.xxx:8081/nexus/content/groups/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

<profiles>
<profile>
<id>myprofile</id>
<repositories>
<repository>
<id>neo-public</id>
<name>Neohope Nexux Public Mirror</name>
<url>http://192.168.xxx.xxx:8081/nexus/content/groups/public</url>
</repository>
</repositories>
</profile>
</profiles>

<activeProfiles>
<activeProfile>myprofile</activeProfile>
</activeProfiles>

</settings>

3.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.neohope.xxx</groupId>
<artifactId>xxx-xxx</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>xxx-xxx</name>
<url>http://neohope.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nexus.url>http://192.168.xxx.xxx:8081</nexus.url>
</properties>

<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-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>neo-releases</id>
<name>Internal Releases</name>
<url>${nexus.url}/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>neo-snapshots</id>
<name>Internal Snapshots</name>
<url>${nexus.url}/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>

Maven Lifecycles

Maven有三大生命周期
default:默认生命周期,用于软件构建
clean:用于项目清理
site:用于生成项目网站

1、default

<phases>
  <phase>validate</phase>
  <phase>initialize</phase>
  <phase>generate-sources</phase>
  <phase>process-sources</phase>
  <phase>generate-resources</phase>
  <phase>process-resources</phase>
  <phase>compile</phase>
  <phase>process-classes</phase>
  <phase>generate-test-sources</phase>
  <phase>process-test-sources</phase>
  <phase>generate-test-resources</phase>
  <phase>process-test-resources</phase>
  <phase>test-compile</phase>
  <phase>process-test-classes</phase>
  <phase>test</phase>
  <phase>prepare-package</phase>
  <phase>package</phase>
  <phase>pre-integration-test</phase>
  <phase>integration-test</phase>
  <phase>post-integration-test</phase>
  <phase>verify</phase>
  <phase>install</phase>
  <phase>deploy</phase>
</phases>

2、clean

<phases>
  <phase>pre-clean</phase>
  <phase>clean</phase>
  <phase>post-clean</phase>
</phases>
<default-phases>
  <clean>
    org.apache.maven.plugins:maven-clean-plugin:2.5:clean
  </clean>
</default-phases>

3、site

<phases>
  <phase>pre-site</phase>
  <phase>site</phase>
  <phase>post-site</phase>
  <phase>site-deploy</phase>
</phases>
<default-phases>
  <site>
    org.apache.maven.plugins:maven-site-plugin:3.3:site
  </site>
  <site-deploy>
    org.apache.maven.plugins:maven-site-plugin:3.3:deploy
  </site-deploy>
</default-phases>