一.简单的maven入门项目
maven下载地址:http://maven.apache.org/download.html。
1)创建一个简单的Maven项目命令:
mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook。
注:本学习用的是maven2.0.9版本,如果使用最新版本需要注意jdk和创建命令更改为:
mvn archetype:generate -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook。
2)打包项目命令:mvn install(需要在生产项目的pom.xml的目录下执行)
3)执行命令:java -cp target/simple-1.0-SNAPSHOT.jar org.sonatype.mavenbook.App 看是否出现Helloworld:成功则算命创建打包成功。
4)maven 打包细化步骤 mvn resources:resources \compiler:compile \resources:testResources \compiler:testCompile \surefire:test \ jar:jar与运行这些指令同效。
5)当一个项目被安装到本地的Maven仓库,它立刻能被任何其它的项目所使用。而我们所需要做的只是,在其它项目用使用Maven的唯一坐标来加入对这个特定构件的依赖。
6)maven仓库目录存储格式为:/<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging>
7)站点生成和报告命令:mvn site
8)只测试测试类:mvn test
9)忽略测试失败配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
也可以在执行测试的时候用:mvn test -Dmaven.test.failure.ignore=true
10)忽略测试打包指令:mvn install -Dmaven.test.skip=true
也可以通过配置实现:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
11)Maven Assembly 插件的配置
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build
配置后用mvn assembly:assembly 来构建这个装配。
11)如果希望部署到容器中需要配置一下jetty插件配置如下
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
配置后执行jetty:run 就能启动项目。
mvn compile:编码项目命令
12)JNDI, JDBC, Servlet, JSP, JTA的依赖配置:
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.4_spec</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
只需把想要的依赖替换“servlet_2.4”