时间很长;现在很短;距离很长;相遇很短
Posts tagged maven
教学贴:如何编写Maven2插件
May 26th
Maven插件的编写非常容易,所以,简单的几个流水帐吧。
创建插件工程
# mvn archetype:generate
选择12,回答groupId,artifactId之类的问题,工程就OK了。导入到喜欢的IDE吧。
定义插件参数
完成的定义请参考Maven官方教程,这里简要介绍。
Maven2使用了很多注释中的Annotation来定义插件行为,完整文档点这里,常见的如:
|
Annotation |
功能介绍 |
|---|---|
| @goal <goalName> | 所谓目标了。每个Mojo类包含一个目标,一个Plugin可能若干Mojo类也就有若干目标了 |
|
@parameter expression="${aSystemProperty}" default-value="${anExpression}" |
这是用于Mojo的属性的,可以通过表达式来获取系统参数,项目参数,以及从命令行输入参数。 |
| @required | 同样用于Mojo的属性,定义该参数为必须的。 |
下面是简单的代码
package com.yuchengtech.emp;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
/**
* 用于编译EMP相关文件,生成部署资源
*
* @goal emp-compile
*
* @phase process-sources
*/
public class EmpCompileMojo extends AbstractMojo {
/**
* 输出文件路径
*
* @parameter expression=
* "${project.build.directory}/${project.build.finalName}/WEB-INF/tables"
* @required
*/
private File outputDirectory;
/**
* EMP设计文件路径
*
* @parameter expression="${emp.design.dir}"
* @required
*/
private File empDesignDir;
public void execute() throws MojoExecutionException {
getLog().info("Design Files Folder: " + empDesignDir.getAbsolutePath());
getLog().info(
"Output Table Files Folder: "
+ outputDirectory.getAbsolutePath());
URL rule = this.getClass().getClassLoader().getResource(
"rule-table.xml");
Digester digester = DigesterLoader.createDigester(rule);
Collection files = FileUtils.listFiles(empDesignDir,
new String[] { "table" }, true);
for (Iterator it = files.iterator(); it.hasNext();) {
File table = (File) it.next();
getLog().info("EMP: covert file - " + table.getName());
ModelConverter convert = new ModelConverter(table, outputDirectory,
digester);
StringBuffer s = new StringBuffer();
boolean result = convert.convert(s);
if (!result) {
throw new MojoExecutionException(s.toString());
}
}
}
}
发布插件
使用简单的mvn install 之后就可以在本地使用这个插件了。或者发布到公共服务器上供更多人使用。
可以在项目中用命令行 mvn groupId:artifactId:goal来执行插件操作,也可以在项目POM中配置。
请继续参看Maven官方教程。
Maven2 二三事
Jul 17th
说起Maven,真的是很好用的工具啊!啊啊!啊啊啊!
本文只涉及笔者认为有必要提及的小细节,Maven的使用指引请参考官方文档和其他相关文章。
Q&A开始
关于Maven
- Maven是什么?
Maven是一个构建工具。当前主要被设计用来构建Java工程,但是和Ant等程序一样,它具有很强的扩展性。 - 使用Maven有什么好处?
好处有很多,简单罗列如下:- 完整的项目描述
Maven的项目配置文件POM.xml可以描述软件项目方方面面的关联信息,包括:软件平台,组件依赖,SCM服务器,开发者,跟踪服务器,部署信息等等。 - 规范的软件生命周期
一个软件的开发过程中需要不断的迭代:编码、测试、分析、部署……Maven可以让这一切自动化,从而解放配置管理负担,提高项目质量。 - 事实标准
Maven有作为开源界事实标准的潜在能力,虽然它对Java以外的程序员不够友好,虽然他没有Ruby Gem那么好用,标准的好处就是,人人都可以轻易的通过解读Maven配置了解项目相关信息。
- 完整的项目描述
恩,我们继续。。
Maven安装
- 如何安装?
参看官方手册;一些建议,请将安装目录加入System PATH,方便使用Maven命令;Maven目录名称可以使用通用一点的。 - 下载的软件包在哪里?
默认Maven会下载到用户目录下,请自行修改mavn/conf/config.xml中相关内容 - Maven的Repository太慢了!
事实上,有很多镜像供你选择,请使用Google搜索maven repository,会有很多WebDAV格式的结果,将他们中最快的添加到你的配置文件即可。
项目组织
- 如何和Eclipse结合
Maven下面有几个IDE用插件,个人比较推荐q4e,使用之后在拥有pom.xml的Eclipse项目中选择使用Maven Dependencies Management就可以引入Maven依赖了。
安装Maven和q4e后的Step by Step:- 使用命令行进入开发目录,一般为Eclipse的工作目录
- 输入命令 mvn archetype:create -DgroupId=org.ave7.example -DartifactId=app-example Maven将按照默认layout创建新项目
- cd app-example 输入 mvn eclipse:eclipse生成eclipse配置文件,如果是wtp项目,请使用命令 mvn eclipse:eclipse -DwtpVersion=1.5 (其他选项1.0 1.5 R7)
- 启动eclipse,选择import -> existing project into workspace 选择刚创建的项目
- 在project上点右键,maven 启用 enable maven dependencies
- 自行调整class path 等
- 如何协调依赖版本
引用不同的依赖时可能造成间接依赖的版本不一致,除了exclude以外,还可以在parent pom中使用<dependencyManagement>将可能用到的artifact及其版本包括其中,在dependencies是,只需指明groupId和artifactId即可。 - 生成测试覆盖率报告
在pom中加入
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
在相应目录命令行输入 mvn site 即可
下期内容预告:Maven与SCM
Maven need to act as Wikipedia
May 23rd
Central Repository is an revolution of Open Source. Now we have a unique, global software artifact standard (although not officially). It’s amazing.
The next question is How to Make it Right.
In the most situation, we cannot request an Open Source Authors take the responsibility to maintain their product to the eternity. We cannot assurance that everybody can understand and use Maven in exactly right way either. Maven is still far away from 1-click use software. And the infrastructure of the distribution system of Maven Artifact Repository is even more complex.
Maven is a collaborating system since everyone has the right to commit their own work. But Maven is lack of collaboration functionality. The POM.xml need to be corrected by other who is more familiar with Maven; the source code need to be uploaded; dependencies on unnecessary artifact should be set to optional…. all those actions need to be well tracked and synchronized around the world. It’s much more difficult since it a distribution system.
Wikipedia is the most greatest thing in the age of Web 2.0. So Maven would learn a lot from the success model.