本文是个人工作过程中收集整理的maven内容,希望对他人也能有所帮助,其中会结合实际项目给出基本框架,后续可根据需要扩展。
关于Maven的基础知识可以参考《Maven权威指南》及本文后面的参考资料。网络上有很多文章,原理性太强,我结合自己的工作会给出一些实际的配置,当不符合需要时再查阅资料进行修改即可。
阅读本文最好具备一些Maven的概念,比如pom是什么。
1、Maven入门
1.1 Maven基础知识
生命周期:maven有生命周期的概念。
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war |
install | install:install |
deploy | deploy:deploy |
继承与聚合:用来组织基于maven管理的目录结构
继承是一个父子结构,父Pom使用<dependencyManagement>定义依赖,子pom视需要进行添加或修改引用信息
注意点:继承的<relativePath> 不是必须,但是如果parent在其他路径的话,这个路径一定要正确
<!-- 继承pom范例 -->
<parent>
<groupId>com.xx.xx</groupId>
<artifactId>base_pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../base_pom</relativePath> <!-- base_pom在其他路径时一定要设置relativePath -->
</parent>
聚合模块的内容仅仅是一个pom.xml文件,它不包含src/main/java、src/test/java等目录,因为它只是用来帮助其它模块构建的工具,本身并没有实质的内容。
<!-- 聚合pom范例 -->
<modules>
<module>db_jpa_permission</module>
<module>db_jpa_algorithm</module>
<module>db_module_jdbc_example</module>
<module>db_jpa_permission_model</module>
</modules>
聚合与继承是可以混合使用的。
1.2 安装Maven & Repository
推荐 附录参考资料中的 Maven 那点事儿
1.3 mvn 常用指令:
$mvn clean :该命令调用clean生命周期的clean阶段。实际执行的阶段为clean生命周期的pre-clean和clean阶段。
$mvn test:该命令调用default生命周期的test阶段。实际调用的是default生命周期的validate、initialize等,直到test的所有阶段。
$mvn clean package:该命令调换用clean生命周期的clean阶段和default生命周期的package阶段。
$mvn clean package -Dmaven.test.skip=true :跳过上述命令的test周期
$mvn clean install:该命令调换用clean生命周期的clean阶段和default生命周期的instal阶段。
2、Maven实践
在使用maven的过程中,一定会利用其聚合与继承的功能,从而对项目的构建进行有效管理。
a、用一个最顶层的pom完成最基本的配置(我称呼其为base_pom),如确定jdk版本、修正中文字符乱码等问题
b、n个抽象功能pom模块继承base_pom,并添加特定属性,如DB型pom、webservice型pom等
c、具体功能模块pom继承抽象pom模块,并完成面向实际功能的依赖配置工作
2.1 maven 基本配置(base_pom)
定义全局属性
<properties>
<manifest.location>target/META-INF</manifest.location>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java-version>1.7</java-version>
<junit.version>4.12</junit.version>
<log4j-version>1.2.16</log4j-version>
<slf4j-version>1.7.7</slf4j-version>
</properties>
定义插件
<plugins>
<!-- 统一设定jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 解决maven test命令时console出现中文乱码乱码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
2.2 maven for jetty
当某个模块需要web容器支持时可以使用
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.10.v20150310</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<httpConnector>
<port>9090</port>
</httpConnector>
<webAppConfig>
<contextPath>/${project.artifactId}</contextPath>
</webAppConfig>
</configuration>
<!-- 这里适合8.1.16 版本
<configuration>
<scanIntervalSecond>10</scanIntervalSecond>
<webApp>
<contextPath>/${project.artifactId}</contextPath>
</webApp>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8787</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
-->
</plugin>
2.3 maven for osgi bundle(karaf)
以下给出了base_pom中的基本设置,在各个bundle中还需要单独根据war、bundle等不同package属性设置下。
具体可参考附录中的Apache Felix - Apache Felix Maven Bundle Plugin (BND)
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.3</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>manifest</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
<goal>unpack</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
3、附录
3.1 参考资料
利用maven中resources插件的copy-resources目标进行资源copy和过滤
Apache Felix - Apache Felix Maven Bundle Plugin (BND)
Configuring the Jetty Maven Plugin
3.2 maven的完整生命周期
maven有三个相互独立的生命周期,分别是clean、default、site。clean生命周期目的是清理项目,default生命周期目的是构建项目,而site生命周期目的是建立项目站点。
clean生命周期包含三个阶段,主要负责清理项目,如下:
pre-clean | executes processes needed prior to the actual project cleaning |
clean | remove all files generated by the previous build |
post-clean | executes processes needed to finalize the project cleaning |
default生命周期定义了真正构建时所需要执行的所有步骤,包含的阶段如下:
validate | validate the project is correct and all necessary information is available. |
initialize | initialize build state, e.g. set properties or create directories. |
generate-sources | generate any source code for inclusion in compilation. |
process-sources | process the source code, for example to filter any values. |
generate-resources | generate resources for inclusion in the package. |
process-resources | copy and process the resources into the destination directory, ready for packaging. |
compile | compile the source code of the project. |
process-classes | post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
generate-test-sources | generate any test source code for inclusion in compilation. |
process-test-sources | process the test source code, for example to filter any values. |
generate-test-resources | create resources for testing. |
process-test-resources | copy and process the resources into the test destination directory. |
test-compile | compile the test source code into the test destination directory |
process-test-classes | post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above. |
test | run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
prepare-package | perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above) |
package | take the compiled code and package it in its distributable format, such as a JAR. |
pre-integration-test | perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test | process and deploy the package if necessary into an environment where integration tests can be run. |
post-integration-test | perform actions required after integration tests have been executed. This may including cleaning up the environment. |
verify | run any checks to verify the package is valid and meets quality criteria. |
install | install the package into the local repository, for use as a dependency in other projects locally. |
deploy | done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
site生命周期的目的是建立和发布项目站点,maven能够基于POM所包含的信息,自动生成一个友好的站点,方便团队交流和发布项目信息,包含的阶段如下:
pre-site | executes processes needed prior to the actual project site generation |
site | generates the project's site documentation |
post-site | executes processes needed to finalize the site generation, and to prepare for site deployment |
site-deploy | deploys the generated site documentation to the specified web server |
3.3 maven属性
1、内置属性:如${basedir}表示项目根目录,${version}表示项目版本
2、POM属性:用户可以引用pom文件中对应的值。如:
${basedir} 项目根目录
${project.build.directory} 构建目录,缺省为target
${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
${project.packaging} 打包类型,缺省为jar
${project.xxx} 当前pom文件的任意节点的内容
3、自定义属性:用户可以在pom的<properties>元素下自定义maven属性。
4、setting属性:用户可以使用以settings开头的属性引用settings.xml中xml元素的值,如${settings.localRepository}指向用户本地仓库的地址。
5、java系统属性:maven可以使用当前java系统的属性,如${user.home}指向了用户目录。
6、环境变量属性:所有环境变量都可以使用以env.开头的属性。如:${env.JAVA_HOE}。