maven 备忘

[size=large]一.配置[/size]
[size=medium]
1.有的项目的pom.xml中没有配置maven远程远程仓库,maven的server.xml中也没有配置。那么项目中的依赖是在哪里下载的呢?
肯定有一个默认的远程仓库,叫做中央仓库,他配置在maven的超级pom中,而所有的maven项目都继承该pom,这个超级pom集成在${M2_HOME}/lib/maven-model-builder-3.0.5.jar/org/apache/maven/model/pom-4.0.0.xml中,打开该文件,可以看到这个:[/size]

<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

[size=medium]其中<url>http://repo.maven.apache.org/maven2</url>就是默认的远程仓库。就是因为有了它我们才可以不进行仓库配置也能下载依赖。
当然,我们也可以使用其他的远程仓库,比如自己公司内部的仓库。配置如下:
[/size]

<repositories>
<repository>
<id>repository.engineering.redhat.com</id>
<name>RedHat Engineering Operations</name>
<url>https://repository.engineering.redhat.com/nexus/content/repositories/hss-tools</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</repository>
</repositories>

[size=medium]配置在server.xml中会作用于所有项目,在自己的pom.xml中则只作用于该项目

[size=medium]
2.导入插件
[/size]

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warName>pay</warName>
</configuration>
</plugin>
</plugins>
</build>

[size=medium]上面这个插件可以用于去掉打包时候的版本号,warName为包名
也可以用[/size]

<build>
<finalName>pay</finalName>
</build>

[size=medium]
[size=medium]3.指定编码方式[/size]
<properties> 
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

[size=medium]4.自定义变量[/size]

<properties>
<spring.version>1.2.6</spring.version>
</properties>


[size=medium]用EL表达式方式引用,方便日后统一修改版本号,如[/size]

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>

[size=medium]5.继承与聚合
继承:概念与java中的继承类似。目的是抽出公共的元素,在开发中尽量减少项目的配置简化开发。例如:要并行开发好几个项目,而这些项目中又有很多相同的配置,那么就可以建立一个父项目A,包含这些配置,而其他项目只需要继承父项目A,不用再进行这些重复的配置。
B要继承A需要在B的pom.xml中添加配置如下:[/size]

<parent><!--父项目的groupId、artifactId、version -->
<groupId>com.lngtop.piaohao</groupId>
<artifactId>piaohao</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- <relativePath>父pom.xml的相对路径</relativePath> -->
</parent>

[size=medium]子项目的pom文件会继承父pom文件的所有元素

聚合:目的是把聚合的各个module(项目)一起构建。例如:要打包多个项目A、B、C时,如果用聚合的话就不必分别打包,可以直接打包父项目(被聚合的项目)便会打包所有项目
把B聚合到A中需要在A的pom.xml中添加如下配置[/size]

<modules><!--pay为聚合的项目相对目录 -->
<module>pay</module>
</modules>


[size=medium]实际开发中一般会将继承与聚合混合使用,此时只要同时加上以上配置即可。

6.排除传递依赖
例如我们的项目A依赖B,而B依赖C,而A不依赖C。此时可在A中排除C,起到项目瘦身作用[/size]

<dependency>
<groupId>B的groupId</groupId>
<artifactId>B的artifactId</artifactId>
<version>${B.version}</version>
<exclusions>
<exclusion>
<groupId>C的groupId</groupId>
<artifactId>C的artifactId</artifactId>
</exclusion>
</exclusions>
</dependency>



[size=large]二.命令[/size]

[size=medium]1.导入官网没有的jar包:
先安装mvn install:install-file -DgroupId=*** -DartifactId=*** -Dversion=*** -Dpackaging=jar -Dfile=***.jar
2.创建web项目:
mvn archetype:create -DgroupId=packageName -DartifactId=webappName -Dversion=0.1 -DarchetypeArtifactId=maven-archetype-webapp
3. 编译: mvn compile
4. 编译测试代码:mvn test-compile
5. 运行测试:mvn test
6. 打包:mvn package
7. 清除mvn生成的文件:mvn clean
8. 生成eclipse项目:mvn eclipse:eclipse
9. 编译测试的内容:mvn test-compile
10. 只打jar包: mvn jar:jar
11. 清除eclipse的一些系统设置:mvn eclipse:clean
12.下载依赖的源代码:mvn dependency:sources -DdownloadSources=true (下载源代码jar) -DdownloadJavadocs=true (下载javadoc包)
[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值