(1)创建web项目
mvn archetype:create -DgroupId=com.mycompany.webapp -DartifactId=myweb -DarchetypeArtifactId=maven-archetype-webapp
(2)转化成java项目
cmd 进入项目目录下,执行:
mvn eclipse:eclipse
(3)打包而不测试
mvn package -DskipTests=true
mvn clean install -DskipTests=true
(4)打jar包时,复制依赖包:
mvn dependency:copy-dependencies
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin
</artifactId>
<configuration>
<outputDirectory>${basedir}/app/lib
</outputDirectory>
</configuration>
</plugin>
(5)运行测试:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
mvn jetty:run
(6)安装第三方包:
mvn install:install-file -DgroupId=org.slf4j -DartifactId=slf4j-log4j12 -Dversion=1.5.6 -Dfile=slf4j-log4j12-1.5.6.jar -Dpackaging=jar -DgeneratePom=true
备注:
mvn install:install-file
-DgroupId=<your_group_name>
-DartifactId=<your_artifact_name>
-Dversion=<snapshot>
-Dfile=<path_to_your_jar_file>
-Dpackaging=jar
-DgeneratePom=true
简单解释一下:
archetype 是一个内建插件,他的create任务将建立项目骨架
archetypeArtifactId 项目骨架的类型
可用项目骨架有:
* maven-archetype-archetype
* maven-archetype-j2ee-simple
* maven-archetype-mojo
* maven-archetype-portlet
* maven-archetype-profiles (currently under development)
* maven-archetype-quickstart
* maven-archetype-simple (currently under development)
* maven-archetype-site
* maven-archetype-site-simple, and
* maven-archetype-webapp
(7)解决maven传递依赖中的版本冲突
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>
maven-project-info-reports-plugin
</artifactId>
</plugin>
mvn project-info-reports:dependencies 。生成项目依赖的报表,这样你就能够在报表中找出你版本冲突的相关性依赖了。
最后在相应的dependency中加上exclusions来排除相关的传递依赖。
例:
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
<exclusions>
<exclusion>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
备注pom的简要例子:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>mypro</groupId>
<artifactId>mypro</artifactId>
<version>0.0.1</version>
<description></description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${basedir}/app</outputDirectory>
<archive>
<manifestFile>${basedir}/MANIFEST.MF</manifestFile>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypro.Main </mainClass>
</manifest>
</archive>
<excludes>
<exclude>system.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin
</artifactId>
<configuration>
<outputDirectory>${basedir}/app/lib
</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.5</version>
</dependency>
<!-- javax/activation/activation/1.1 org.apache.geronimo.modules/geronimo-activation/1.2-beta-->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<!-- com/cedarsoft/commons/mail/1.4.3:-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>14</version>
</dependency>
</dependencies>
</project>