《Apache Maven 3 Cookbook》读书笔记

注:ISBN 978-1-849512-44-2  First published: August 2011


 

Chapter 1 maven基础 

使用原型生成工程:

mvn archetype:generate

参数指定模板:mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

 

pom文件结构:

- The basics: This section contains project co-ordinates, dependency management, and inheritance details. Additionally, it also contains modules and project level properties.

- Build settings: This section contains the build details.

- Project metadata: This section contains project-specific details such as name, organization, developers, URL, inception year, and so on.

- Environment: This section contains all information regarding the environment including details of the version control being, issue

示例:

<project … >
 <modelVersion>4.0.0</modelVersion>

 <!-- The Basics -->
 <groupId>...</groupId>
 <artifactId>...</artifactId>
 <version>...</version>
 <packaging>...</packaging>
 <dependencies>...</dependencies>
 <parent>...</parent>
 <dependencyManagement>...</dependencyManagement>
 <modules>...</modules>
 <properties>...</properties>

 <!-- Build Settings -->
 <build>...</build>
 <reporting>...</reporting>

 <!-- Project Meta Data -->
 <name>...</name>
 <description>...</description>
 <url>...</url>
 <inceptionYear>...</inceptionYear>
 <licenses>...</licenses>
 <organization>...</organization>
 <developers>...</developers>
 <contributors>...</contributors>

 <!-- Environment -->
 <issueManagement>...</issueManagement>
 <ciManagement>...</ciManagement>
 <mailingLists>...</mailingLists>
 <scm>...</scm>
 <prerequisites>...</prerequisites>
 <repositories>...</repositories>
 <pluginRepositories>...</pluginRepositories>
 <distributionManagement>...</distributionManagement>
 <profiles>...</profiles>
</project>

 

build lifecycle(构建生命周期)

更多列表参考官网:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

default:

- Validate: validates that all project information is available and is correct
- Compile: compiles the source code
- Test: runs unit tests within a suitable framework
- Package: packages the compiled code in its distribution format
- Integration-test: processes the package in the integration-test environment
- Verify: runs checks to verify that the package is valid
- Install: installs the package in the local repository
- Deploy: installs the final package in a remote repository

clean:

- Pre-clean: executes processes required before project cleaning
- Clean: removes all files generated by previous builds
- Post-clean: executes processes required to finalize project cleaning

site(生成web文档):

- Pre-site: executes processes required before generation of the site
- Site: generates the project’s site documentation
- Post-site: executes processes required to finalize the site generation and prepares the
site for deployment
- Site-deploy: deploys the site documentation to the specified web server

 

Build profiles(构建选项)被指定的时机:

- 命令行参数-P xxx (-P !xxx)

- 配置文件中activeProfiles-activeProfile指定

- 配置文件中指定环境变量触发profiles-profile-activation-property-name/value

 

 


Chapter 2 软件工程支持

软件工程重要功能(可以在其他方面作为参考):

- Build automation
+ Compilation of source code to binary code
+ Packaging of binary code
+ Running tests
+ Deployment to remote systems
+ Creation of documentation and release notes - Project modularization - Dependency management - Source code quality checks - Test driven development - Acceptance testing automation - Deployment automation

 

子模块的生成:

在父工程(packaging=pom)中,使用mvn archetype:generate,可以生成子工程并自动添加module配置项。

 

dependency scopes(依赖作用范围):

- Compile: This is the default scope. Compile dependencies are available in
the classpaths.
- Provided: This scope assumes that the JDK or the environment provides
dependencies at runtime.
- Runtime: Dependencies that are required at runtime and are specified in the
runtime classpaths.
- Test: Dependencies required for test compilation and execution.
- System: Dependency is always available, but the JAR is provided nonetheless.
- Import: Imports dependencies specified in POM included via the
<dependencyManagement/> element.

 

dependency-plugin goals:

$ mvn dependency:analyze
Analyzes dependencies (used, unused, declared, undeclared)

$ mvn dependency:analyze-duplicate
Determines duplicate dependencies

$ mvn dependency:resolve
Resolves all dependencies

$ mvn dependency:resolve-plugin
Resolves all plugins

$ mvn dependency:tree
Displays dependency trees

 

依赖的缺省scope为compile,缺省type为jar

 

system scope的依赖必须指定systemPath属性(jar包的具体路径)

 

pmd-plugin goals:

- pmd:pmd creates a PMD site report based on the rulesets and configuration set in
the plugin
- pmd:cpd generates a report for PMD's Copy/Paste Detector (CPD) tool
- pmd:check verifies that the PMD report is empty and fails the build if it is not
- pmd:cpd-check verifies that the CPD report is empty and fails the build if it is not

 

构建时默认集成pmd-plugin的配置示例:

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>2.5</version>
    <executions>
     <execution>
      <goals>
       <goal>check</goal>
       <goal>cpd-check</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>

 

ui自动化测试工具selenium,暂未接触过,需要再查。

 

deploy goals:

- deploy:deploy: To deploy a project and all its artifacts
- deploy:deploy-file: To deploy a single artifact file

 

mvn deploy的依赖配置:

1. 在pom.xml中:

<distributionManagement>
 <repository>
  <id>srirangan.repository</id>
  <name>MyPrivateRepository</name>
  <url>...</url>
 </repository>
</distributionManagement>

2. 在<user_home>/. m2/settings.xml 或 <m2_home>/conf/settings.xml 中:

<server>
 <id>srirangan.repository</id>
 <username>srirangan</username>
 <password>myTopSecretPassword</password>
</server>

 


Chapter 3 协作相关

添加额外的仓库,setting.xml中配置:

<mirrors>
 <mirror>
 <id>TestRepository</id>
 <name>My test repository</name>
 <url>repo url here</url>
 <mirrorOf>*</mirrorOf>
 </mirror>
</mirrors>

 

nexus(私有仓库的搭建)、hudson部署(略)

hudson、team信息维护在POM中显得有些累赘,这里不作记录。

 

离线工作模式:

a. 离线包安装

mvn install:install-file -DgroupId=%GROUP_ID% -DartifactId=%ARTIFACT_ ID% -Dversion=%VERSION% -Dfile=%COMPONENT%.jar -Dpackaging=jar -DgeneratePom=true

b. 离线模式运行

命令行参数"-o"或者在settings.xml中设置settings.offline标签为"true"。

 


Chapter 4 文档与报告相关插件

生成并运行文档服务:

mvn site:run

书籍对site的编辑介绍过于简单,需要时参考官网:https://maven.apache.org/guides/mini/guide-site.html

 

javadoc命令:

- javadoc:javadoc: Generates documents for the project
- javadoc:test-javadoc: Generates documents for the test classes
- javadoc:aggregate: Generates documents for an aggregator project
- javadoc:test-aggregate: Generates documents for tests of an aggregator project
- javadoc:jar: Creates an archive of the documents
- javadoc:test-jar: Creates an archive of the tests’ docs
- javadoc:aggregate-jar: Archives documents of an aggregated project
- javadoc:test-aggregate-jar: Archives test documents of an aggregated project
- javadoc:fix: Fixes documents and tags for Java files
- javadoc:test-fix: Fixes documents and tags for Java test files

将javadoc集成仅site构建过程中的配置方式:

在pom.xml中配置project.reporting.plugins节点,配置内容:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-javadoc-plugin</artifactId>
 <version>2.7</version>
 <configuration>
 </configuration>
</plugin>

 

测试报告插件(surefile)配置:

<project>
 <build>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.6</version>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <reportFormat>brief</reportFormat>
     <useFile>false</useFile>
<skip>true</skip> </configuration> </plugin> </plugins> </build> </project>

命令行参数说明:

-Dmaven.test.skip=true 跳过测试的投建和运行

-DskipTests=true 跳过测试的运行

 

surefile默认测试类pattern:

- **/Test*.java
- **/*Test.java
- **/*TestCase.java

不符合约定pattern的类可以在pom.xml的plugin配置中设置例外(plugin.configuration.includes.include节点及plugin.configuration.excludes.exclude节点)。支持"**"等通配符。

 

覆盖率报告插件Cobertura(书中链接失效,可以参考这个:http://www.mojohaus.org/cobertura-maven-plugin/)

不在书中但常用的插件还有JaCoCo:https://www.jacoco.org/ or https://www.eclemma.org/jacoco/ (for maven: https://www.eclemma.org/jacoco/trunk/doc/maven.html)

 

编码风格检查插件:http://maven.apache.org/plugins/maven-checkstyle-plugin/

 

大盘插件dashboard(很多年没有更新了):https://mvnrepository.com/artifact/org.codehaus.mojo/dashboard-maven-plugin

 

SonarQube:https://www.sonarqube.org/

 

 


Chapter 5 Java方面的支持(本书在这方面有些陈旧,很多细节不再作摘录,后续自行补充一些较新的经验或另开个贴子说明)

IOC和AOP

Inversion of Control
The Inversion of Control (IoC) container is a central concept of the Spring Framework. It provides a consistent mechanism to configure and manage JavaBeans. Typically, the IoC container is configured using XML files, which contain the bean definitions.

Aspect Oriented Programming
Aspect Oriented Programming focuses on core business logic programming by isolating supporting functions and logic. The Spring Framework supports Aspect Oriented Programming (AOP) based on interceptions and runtime configuration. Compared to AspectJ, Spring AOP framework is much simpler while supporting all major features.

 


Chapter 6 Google开发相关支持(Android、GWT(Google Web Toolkit)、GAE(Google App Engine)开发),工作不涉及,暂时略过。

 


Chapter 7 Scala,Groovy,Flex支持

可以使用mvn archetype:generate命令选择scala-archetype-simple模板来生产scala工程。(插件:maven-scala-plugin)

goals for scala

scala:compile Compiles the Scala source directory
scala:console Launches the Scala REPL with all project dependencies made available
scala:doc Generates the documentation for all Scala sources
scala:help Displays the Scala compiler help
scala:run Executes a Scala class
scala:script Executes a Scala script
scala:testCompile Compiles the Scala test sources

 

类似的,groovy工程的模板:gmaven-archetype-basic。(插件:gmaven-plugin)

goals for groovy

groovy:shell
groovy:console
...

注意到gmaven-plugin这个名字没有遵循maven-xxx-plugin的约定。研究一下。

下载plugin的jar包,在META-INF/maven/plugin.xml文件的开头几行找到了这个配置:

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
 <name>GMaven Plugin</name>
 <description>Provides support for execution, compilation and other facets of Groovy development.</description>
 <groupId>org.codehaus.gmaven</groupId>
 <artifactId>gmaven-plugin</artifactId>
 <version>1.5</version>
 <goalPrefix>groovy</goalPrefix> <!-- 注意这里 -->

...

 

类似的,flex工程的模板:flexmojosarchetypes-application。(插件:flexmojos-maven-plugin)

goals for flex

flexmojos:asdoc Generates documentation for ActionScript source files
flexmojos:asdocreport Generates documentation for ActionScript source files as a report to be included in the Maven site
flexmojos:compile-swc Compiles MXML and ActionScript sources into a SWC library
flexmojos:compile-swf Compiles MXML and ActionScript sources into a SWF executable
flexmojos:coypflex-resources Copies Flex resources into a web application project–used in a multi-modular project setup
flexmojos:flexbuilder Generates Adobe Flash Builder (previous known as Flex Builder) configuration files
flexmojos:generate Generates ActionScript classes based on Java classes using GraniteDS
flexmojos:optimize Runs post-line SWF optimization on SWC library files
flexmojos:sources Creates a JAR containing all the sources
flexmojos:testcompile Compiles all the test classes
flexmojos:testrun Runs the tests in the Adobe Flash Player
flexmojos:testswc Builds an SWC file containing the test sources
flexmojos:wrapper Generates an HTML wrapper for an SWF application

 


Chapter 8 在IDE中使用maven(略)

 


Chapter 9 开发maven插件

MOJO(Maven plain Old Java Object):每个mojo对应maven中的一个goal。

 

使用archetype:generate生成maven mojo工程:

mvn archetype:generate -DgroupId=net.srirangan.packt.maven -DartifactId=maven-plug101-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo 

 

mvn clean install 安装插件到本地仓库后,配置pom.xml中的plugin配置,然后就可以使用插件。比如按照上面的命令行生成的插件,调用方式为:

mvn net.srirangan.packt.maven:maven-plug101-plugin:1.0-SNAPSHOT:mygoal

其中mygoal为定义在Mojo类javadoc中的@goal属性的值。(Mojo类继承 org.apache.maven.plugin.AbstractMojo类)

 

要定义插件参数,对Mojo的属性添加javadoc,并以@parameter注解。要指定参数名,可以参照示例:

@parameter expression="${project.build.directory}"

如果是必选参数,可以额外增加javadoc的@required注解。

 

短命令配置导引:

1. artifact命名遵从规约:maven-{plugin_name}-plugin

2. 将groupId配入settings.xml,配置形式如下:

<pluginGroups>
 <pluginGroup>net.srirangan.packt.maven</pluginGroup>
 /pluginGroups>

 

将插件嵌入默认的声明周期:

1. 在pom.xml中配置:

<plugin> 
 <executions>
  <execution>
   <phase>compile</phase>
   <goals>
    <goal>mygoal</goal>
   </goals>
  </execution>
 </executions>
</plugin>

2. 在Mojo javadoc中使用注解@phase指定,形如:

@phase compile

 

使用maven-plugin-tools-ant插件可以基于ant生成mvn插件 。

使用jruby-maven-plugin插件可以基于jruby生成mvn插件。

posted @ 2019-03-28 22:12  rainforwind  阅读(155)  评论(0编辑  收藏  举报