《Maven实战》阅读笔记
java -version
mvn -v
mvn help:system
m2eclipse maven->install
MAVEN_OPTS: -Xms128m -Xmx512m
mvn clean compile
mvn clean test ----> default:clean default:resources, default:compile,default:testResources,default:testCompile
mvn clean package (packaging类型) ---->default-jar
mvn clean install ----> default-install
mvn archetype:generate
解析依赖
mvn dependency:list
依赖树
mvn dependency:tree
分析直接依赖(complie,test,检测不到运行时jar)
mvn dependency:analyze
${project.artifactId}-${project.version}
<finalName>account</finalName>
坐标 :arififactId-version[-classifier].packaging
groupId
artifactId
version
packaging
classifier 不能直接定义项目,附属构件是右插件生成
jar仲裁链路,越短越优先;如果链路长度一样,排前面的优先
optional不会传递(一般不使用,用于实现多种特性的适配)
mvn jetty:run -Djetty.port=9999
mvn cargo:start
mvn cargo:redeploy
强制mvn检查更新 mvn clean install-U
三套生命周期 clean default site
pre-clean clean post-clean
default validate initialize generate-source process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources process-test-sources generate-test-resources process-test-resources test-compile process-test-classes test prepare-package
package pre-integration-test integration-test post-integration-test verify install deploy
pre-site site post-site site-deploy
阶段与命令
mvn clean
mvn test
mvn clean install
mvn clean deploy site-deploy
插件目标:
dependency:analyze dependency:tree dependency:list
compile:compiler surefire:test
生命周期是和插件绑定
打包类型jar, war, pom, maven-plugin, ear
配置-Dkey=value
mvn install-Dmaven.test.skip=true
插件主要来源:apache和codehaus
查看帮助
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1
mvn help:describe -Dplugin=compiler
mvn help:describe -Dplugin=compiler -Dgoal=compile
maven3 插件是寻找release,而不是lastest
仓库 mirrorOf 匹配
测试代码配置
测试代码位于src/test/java/
测试资源位于src/test/resources/
默认输出目录为target/test-classes
persist.file=${project.build.testOutputDirectory}/persist-data.xml
打包pom级别的modules构建
聚合模块(可以是
父子目录
<modules>
<module>account-email</module>
<module>account-persist</module>
</modules>
平行目录
<modules>
<module>../account-email</module>
<module>../account-persist</module>
</modules>)
pom之间的继承
account-parent的pom
<parent>
<relativePath>../account-parent/pom.xml</relativePath> 默认是../pom.xml
<parent>
其中还支持scope import导入配置
约定优于编码,继承超级pom.xml
编译reactor 裁剪反应堆
-am 由叶寻根构建 -amd 由根寻叶构建 -pl 构建指定模块 -rf 由指定模块开始
mvn clean install -pl account-parent -amd -rf account-email
Nexus 搭建私服 proxy setting的profile 手动上传jar
跳过测试
mvn pachage -DskipTests 跳过测试运行
mvn pachage -Dmaven.test.skip=true 跳过测试编译和运行
也可以修改surefire的skipTests测试
指定测试
mvn test -Dtest=RandomGeneratorTest
mvn test -Dtest=Test1,Test2
mvn test -Dtest=Random*Test,Test2 前后缀匹配
mvn test -Dtest -DfailIfNotTests=false 也是一种跳过
代码目录下 mvn cobertura:cobertura 可生成测试覆盖率报告
TestNG @Test{group = {"util","medium"}} 支持分组的概念
Hudson持续集成
持续编译,持续数据库集成,持续测试,持续审查,持续部署,持续反馈
email send 负责人收到错误报告,PM收到所有报告
java -jar hudson.war --httpPort=8082
流行的代码管理系统svnserve mac和linux已经集成
git流行的gitlab
mkdir svn-repos
svnadmin create svn-repos\account 创建 维护 监测
svn import -m "initial import" . file://///
启动服务 svnserve -d -r svn-repos - - listen -host 0.0.0.0 强制绑定ipv4
svn list svn://192..../account/trunk
HUDSON_HOME目录会消耗大量磁盘
web模块的pom 用<finalName>account</finalName>
打包出来就像account.war
mvn jetty:run
cargo 实现自动化部署,configuration配置c:/tomcat和目标目录tomcat6x
mvn cargo:start 就好将c:/tomcat拷贝到tomcat6x子目录
mvn release:prepare -DautoVersionSubmodules=true
mvn release:perform 会包括jar source-jar doc-jar 部署
mvn clean install -DperformRelease=true
GnuPG 是PGP标准的一个免费实现
设置发布的时候才需要,不然耗时
mvn clean deplou -Dgpg.passphrase=yourpass
资源过滤相当于autoconfig
针对开发环境dev, test环境
<profiles>
<profile>
<id>dev</id>
<properties>
<db.driver>com.mysql.jdbc.Driver<db.diver>
<db.url>...
</profiles>
开启resource的过滤,不同目录不同类型
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${project.basedir}/src/main/sql</directory>
<filtering>false</filtering>
</resource>
</resources>
<TestResources>
<TestResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</TestResource>
</TestResources>
命令:mvn clean install -Pdev
mvn clean install -Dtest=x
webapp的过来,如不同的logo,不同的theme
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>..
<configuration>
<webResources>
<resource>
<filtering>true
<directory>src/main/webapp
<includes>
<include>**/*.css<
<include>**/*.js<
</plugin>
单元测试成本低,集成测试成本高
TestNG 中的@Test(group={"unit"})标示为单元,@Test(group={"integration"})
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration
<groups>unit</groups> <!--测试组 -->
<testNGArtifactName>test.testng</testNGArtifactName> <!-- testng 指定
<argLine>-javaagent:"${settings.localRepository}/test.jmockit/0.999.10/test.jmockit-0.999.10.jar"</argLine> <!-- 启动jvm
<useSystemClassLoader>true</useSystemClassLoader>
<testFailureIgnore>true</testFailureIgnore>
<parallel>false</parallel>
<forkMode>once</forkMode> <!--单进程回归所有测试
<!--
junit4.7+ surefire2.6
<forkMode>always</forkMode>
<parallel>methods</parallel>
<threadCount>4</threadCount>
-->
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>full</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5<version>
<configuration>
<groups>unit,integration</groups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<project>
集成的时候可以配置15一次unit,一天两次full
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="plan-project-test">
<test name="plan-project-test">
<packages>
<package name="com.alibaba.lp.permission.dal.cache.test" />
<package name="com.alibaba.lp.permission.dal.impl.test" />
</packages>
</test>
</suite>
项目报告插件
一般插件<project><build><plugins>...
报告插件<project><reporting><plugins>...
JavaDoc <groupId>org.apache.maven.plugins ..
<artifactId>maven-javadoc-plugin ..
<version>2.7
源码在线
<groupId>org.apache.maven.plugins ..
<artifactId>maven-jxr-plugin ..
<version>2.2
<configuration
<aggregate>true
</configuration>
编码规范
<groupId>org.apache.maven.plugins ..
<artifactId>maven-chechstyle-plugin ..
<version>2.5
<configuration
<configLocation>config/maven_checks.xml
</configuration>
PMD
<groupId>org.apache.maven.plugins ..
<artifactId>maven-pmd-plugin ..
<version>2.5
<configuration
<rulesets <!-- 配置其他规则
<ruleset>rulesets/braces.xml
<ruleset>rulesets/strings.xml
</configuration>
ChangeLog
<groupId>org.apache.maven.plugins ..
<artifactId>maven-changelog-plugin ..
<version>2.2
<configuration
<type>range
<range>60
</configuration>
测试覆盖
<groupId>org.codehaus.mojo
<artifactId>cobertura-maven-plugin ..
<version>2.2
自定义站点外观
http://maven.apache.org/xsd/decoration-1.0.0.xsd
资源位于src/site/resources
站点皮肤site.xml com.googlecode.fluido-skin
站点远程部署 支持scp(setting.xml 配置passphrase和privateKey), ftp (wagon-ftp),dav:
maven插件本身也是maven项目,packaging必须是maven-plugin
mvn archetype:generate \
-DgroupId=org.sonatype.mavenbook \
-DartifactId=quickstart \
-Dversion=1.0-SNAPSHOT \
-DpackageName=org.sonatype.mavenbook \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.0 \
-DinteractiveMode=false
1 限定只使用内置的archetypes
mvn archetype:generate -DarchetypeCatalog=internal
2 使用Filter
mvn archetype:generate -Dfilter=maven-archetype-plugin
mojo的注解采用java1.4风格 继承AbstractMojo 实现execute 提供@goal标注
maven-invoker-plugin key支持插件的自动化集成测试
编写Archetype 默认打包类型jar,编写自己的胶水架
pom.xml
src/main/resources/archetype-resources/pom.xml
src/main/resources/META-INF/maven/archetype-metadata.xml
src/main/resources/archetype-resources/**