Maven 实战 阅读 摘要

最近拜读了许晓斌的<<Maven 实战>> ,这篇文章是个人的学习摘要,作回顾用。
大家有兴趣的话,这里有些样章下载:http://www.juvenxu.com/mvn-in-action/

[b]1.下载地址&仓库&插件[/b]

下载地址:http://maven.apache.org/download.html

中央仓库: http://repo1.maven.org/maven2/
中央仓库中国镜像:http://maven.net.cn/content/groups/public
Java.net :http://download.java.net/maven/2/
JBoss: http://repository.jboss.com/maven2/

定义本地仓库:~/.m2/setting.xml

<settings>
<localRepository>D:\.....</localRepository>
</settings>


配置远程仓库:pom.xml中

<respositories>
<repository>
<id>jboss</id>
<name>JBoss Repository</>
<url>http://repository.jboss.com/maven2/</url>
<releases>//发布版本下载控制
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</releases>
<snapshots>//快照版本下载控制
<enabled>false</enabled>
<updatePolicy>daily</updatePolicy>//下载间隔,可选 daily, never, always//每次构建都检查, interval:x //每隔x分钟检查一次更新
</snapshots>
<layout>default</layout>
</repository>
</respositories>


[b] pom.xml远程部署配置[/b]

<distributionManagement>
<repository>
<id>***</id>//远程仓库id
<name>***</name>//方便阅读
<url>***</url>
</respository>
<snapshotRepository>
<id>***</id>//远程仓库id
<name>***</name>//方便阅读
<url>***</url>
</snapshotRepository>
</distributionManagement>


[b]setting.xml中配置认证信息[/b]
			
<servers>
<server>
<id>my-proj</id>//对应远程仓库id
<username>***</username>
<password>***</password>
</server>
</servers>

[b]镜像配置: setting.xml中,所有的请求都会转发到镜像中[/b]
			    
<mirrors>
<mirror>
<id>***</id>
<name>***</name>
<url>***</url>
<mirrorOf>central</mirrorOf>//仓库id,这里表示是中央仓库的镜像,如果是*则表示转接所有远程仓库请求,也可以多个,中间用逗号隔开:repo1,repo2
//*,!repo1 , 所有远程仓库除repo1外
</mirror>
</mirrors>


[b]仓库搜索工具[/b]
Sonatype Nexus : http://repository.sonatype.org/
Jarvana : http://www.jarvana.com/jarvana/
MVNbrowser: http://www.mvnbrowser.com
MVNrepository: http://mvnrepository.com/

插件信息:
http://maven.apache.org/pugins/index.html http:mojo.codehaus.org/plugins.html

有用插件:
Maven Release Plugin 版本发布;
cargo-maven2-plugin 自动化部署;
jetty-maven-plugin web测试;
maven-gpg-plugin, GPG:http://www.gnupg.org/download/ 给maven构件加密 hkp://pgp.mit.edu 美国麻省理工大学公钥服务器
maven-surefire-plugin: 单元测试,集成测试
maven-site-plugin:生成站点 //mvn site
maven-javadoc-plugin
maven-jxr-plugin:在生成站点时配置该插件,会以Web页面的形式将Java源代码展现出来
maven-checkstyle-plugin
maven-pmd-plugin:分析Java源代码,查找代码中的问题
maven-changelog-plugin
cobertura-maven-plugin

私服工具: Archiva 开源; Artifactory 核心开源; Nexus 核心开源, 专业版本需要购买

[b]2.环境变量:[/b]
M2_HOME
path:%M2_HOME%\bin
MAVEN_OPTS -Xms128m -Xmx512m 增加jvm内容,优化Maven运行,Maven一般需要较大内存

[b]3.常用命令:[/b]

mvn -v
mvn help:system
mvn clean compile
mvn clean package
mvn clean test
mvn clean deploy //部署到版本仓库
mvn clean install //使其他项目使用这个jar,会安装到maven本地仓库中
mvn archetype:generate //创建项目架构
mvn dependency:list //查看已解析依赖
mvn dependency:tree
mvn dependency:analyze
mvn install -Dmaven.test.skip=true// -D 参数的使用,这里是跳过test阶段
-am, --also-make :同时构建所列模块的依赖模块
-amd -also-make-dependents 同时构建依赖于所列模块的模块 mvn clean install -pl account-parent -amd -rf account-email
-pl, --projects <arg> 构建指定的模块,模块间用逗号分隔 mvn clean install -pl account-email,account-persist
-rf -resume-from <arg> 从指定的模块回复反应堆 mvn clean install -rf account-email
mvn help:active-profiles :查看当前激活的profiles
mvn help:all-profiles : 查看所有profiles
mvn help:effective -pom 查看完整的pom信息


[b]4.配置代理:[/b]
ping repo1.maven.org
如果需要使用代理:~/.m2/settings.xml (可以从M2_HOME/conf/settings.xml复制,这里是用户级别的配置,升级不受影响。前者是全局配置,升级会受影响)
增加

<proxies>
<proxy> 可以有多个proxy,默认使用第一个
<id>my-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>218.14.227.197</host>
<!--
<username>***</username>
<password>***</password>
<nonProxyHosts>repository.mycom.com |*.google.com</nonProxyHosts> 哪些主机不需要代理
-->
</proxy>
</proxies>


[b]5.eclipse maven插件[/b]:
http://m2eclipse.sonatype.org/sites/m2e
注意:插件的maven可能和本机安装的不一样,最好指定本地的. Windows-Preferences Maven-Installation

[b]6.历史原因,compiler插件默认只支持编译Java1.3,因为需要配置该插件使其支持Java5[/b]
 
<project>
...
<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>
...
</project>


[b]7.为了生成可执行的jar文件,需要借助maven-shade-plugin,配在plugins中最下方[/b]

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
<goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.......HelloWorld</mainClass>
<transformers>
</configuration>
</execution>
<executions>


[b]8.classpath & 依赖范围:[/b] 编译classpath , 测试classpath , 运行classpath
依赖范围: compile(默认值), test, provided(对运行时无效,如servlet-api,最终打包时不会被打入), runtime,
import:该依赖范围不会对三种classpath产生实际影响
system(慎用,和provided依赖范围相同,此类依赖不通过Maven 仓库解析,往往和本机系统绑定,可以造成构建的不可移植)

[b]9.依赖调解[/b]
路径最近者优先
路径相同者,第一声明者优先

[b]10.生命周期[/b]
a.clean : pre-clean,clean,post-clean
b.default:validate, initialize,generate-sources,process-sources,generate-resources,process-resources,compile,process-classes,generate-test-sources,process-test-sources
,generate-test-resources,process-test-resources,test-compile,process-test-classes,test,prepare-paceage,package,pre-integration-test,integration-test,post-integration-test
verify,install,deploy
c.site:建立和发布站点. pre-site,site,post-site,site-deploy

绑定插件到生命周期某个阶段pom.xml
  
<build>//这里是绑定打源代码jar包到verify阶段
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
<goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


[b]11.聚合&继承[/b]
聚合:聚合模块通常放在项目目录的最顶层,其pom.xml中 packageing 属性必须为pom
继承:子模块中pom.xml parent写法
           
<parent>
<groupId>***</groupId>
<artifactId>***</artifactId>
<version>***</version>
<relativePath>../account-parent/pom.xml</relativePath>//父模块pom.xml位置,默认为../pom.xml
</parent>


可被继承的pom元素: groupId, version, description, organization, inceptionYear, url //项目的url地址
developers, contributors, disributionManagement //项目的部署配置, issueManagement, ciManagement, scm// 项目版本控制信息
mailingLists //项目的邮件列表信息, properties , dependecies, repositories, dependencyManagement, build, reporting

在父pom中声明 dependencyManagement 中的依赖不会实际引入jar包,只有在子pom中声明了相应的dependency才会实际引入,这样的好处在于控制引入jar包的版本信息
子pom中不需要声明版本信息. 当然也可以在父pom中将dependencies 中的dependency scope 声明为import, type必须为pom,达到相同效果

[b]12.Maven 属性 & Profile[/b]

内置属性: ${basedir}项目根目录; ${version} 项目版本号

Pom属性: ${project.artifactId}, ${project.build.sourceDirectory}, ${project.build.testSourceDirectory},${project.build.directory}, ${project.outputDirectory}, ${project.testOutputDirectory}, ${project.groupId}, ${project.version}, ${project.build.finalName}

自定义属性
Settings: ${settings.localRepository} ,引用settings.xml文件中的XML元素的值

Java系统属性: ${user.home}
环境变量属性: ${env.JAVA_HOME}

注意:默认情况下,maven属性只能被pom文件解析,如果要在项目中使用,需要额外配置
   
<profiles>
<profile>
<id>dev</id>
<properties>
<db.driver>com.mysql.jdbc.Driver</db.dirver>
<db.url>jdbc:mysql://***</db.url>
<db.username>***</db.username>
<db.password>***</db.password>
</properties>
</profile>
</profiles>
...
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>//是否启用过滤
</resource>
</resources>

...
mvn clean install -Pdev //这里也可以激活多个profile,中间用逗号隔开
mvn clean install -Pdev1,dev2



如果要某个profile默认一直处于激活状态,settings.xml
   
<settings>
...
<activeProfiles>
<activeProfile>dev1</activeProfile>
</activeProfiles>
...
</settings>


系统属性激活

<profiles>
<profile>
<activation>
<property>
<name>test</name>
<value>x</value> //可选,加上则表示test=x时才激活, mvn clean install -Dtest=x
</property>
</activation>
</profile>
</profiles>


操作系统激活

<profiles>
<profile>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
</profiles>


文件存在与否激活
 
<profiles>
<profile>
<activation>
<file>
<missing>x.properties</missing>
<exists>y.properties</exists>
</file>
</activation>
</profile>
</profiles>


默认激活
   
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>


Profile种类
pom.xml :只对当前项目有效
用户settings.xml //对项目的移植可能会产生问题
全局settings.xml //对项目的移植可能会产生问题

Maven可以对资源文件进行过滤,也可以在profile中激活集成测试
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值