首先,创建项目,创建一个文件夹:mkdir yakov
进入yakov目录,然后创建一个pom.xml:touch pom.xml,这个xml文件的结构会在另外的章节详细说一下。
使用vi编辑pom.xml,写入基本的项目信息,如下图:
单单是这些还是不够的,接下来需要,配置一些私服和集成。
注:上面的version改为3.0
有关的私服和集成服务在上一篇中写过:http://www.cnblogs.com/yakov/archive/2011/11/19/maven2_shi_jian.html
设置Maven从Nexus私服下载构件
可以设置某个项目从私服下载,设置项目的pom.xml如下:
1 <project>
2 ...
3 <repositories>
4 <repository>
5 <id>nexus</id>
6 <name>Nexus</name>
7 <url>http://202.117.15.193:8010/nexus/content/groups/public/</url>
8 <releases><enabled>true</enabled></releases>
9 <snapshots><enabled>true</enabled></snapshots>
10 </repository>
11 </repositories>
12 <pluginRepositories>
13 <pluginRepository>
14 <id>nexus</id>
15 <name>Nexus</name>
16 <url>http://202.117.15.193:8010/nexus/content/groups/public/</url>
17 <releases><enabled>true</enabled></releases>
18 <snapshots><enabled>true</enabled></snapshots>
19 </pluginRepository>
20 </pluginRepositories>
21 ...
22 </project>
但是这需要为每个项目配置一下,有可能你仅仅需要为你开发的所有项目都用这同一个私服,那么很好,settings.xml提供了profile来设置:
1 <settings>
2 ...
3 <profiles>
4 <profile>
5 <id>nexus</id>
6 <repositories>
7 <repository>
8 <id>nexus</id>
9 <name>Nexus</name>
10 <url>http://202.117.15.193:8010/nexus/content/groups/public/</url>
11 <releases><enabled>true</enabled></releases>
12 <snapshots><enabled>true</enabled></snapshots>
13 </repository>
14 </repositories>
15 <pluginRepositories>
16 <pluginRepository>
17 <id>nexus</id>
18 <name>Nexus</name>
19 <url>http://202.117.15.193:8010/nexus/content/groups/public/</url>
20 <releases><enabled>true</enabled></releases>
21 <snapshots><enabled>true</enabled></snapshots>
22 </pluginRepository>
23 </pluginRepositories>
24 </profile>
25 </profiles>
26 <activeProfiles>
27 <activeProfile>nexus</activeProfile>
28 </activeProfiles>
29 ...
30 </settings>
上面的配置是针对下载构件的,如果所有的下载都从私服上进行,就需要配置镜像了!如下所示:
1 <settings>
2 ...
3 <mirrors>
4 <mirror>
5 <id>nexus</id>
6 <mirrorOf>*</mirrorOf>
7 <url>http://202.117.15.193:8010/nexus/content/groups/public/</url>
8 </mirror>
9 </mirrors>
10 <profiles>
11 <profile>
12 <id>nexus</id>
13 <repositories>
14 <repository>
15 <id>central</id>
16 <url>http://central</url>
17 <releases><enabled>true</enabled></releases>
18 <snapshots><enabled>true</enabled></snapshots>
19 </repository>
20 </repositories>
21 <pluginRepositories>
22 <pluginRepository>
23 <id>central</id>
24 <url>http://central</url>
25 <releases><enabled>true</enabled></releases>
26 <snapshots><enabled>true</enabled></snapshots>
27 </pluginRepository>
28 </pluginRepositories>
29 </profile>
30 </profiles>
31 <activeProfiles>
32 <activeProfile>nexus</activeProfile>
33 </activeProfiles>
34 ...
35 </settings>
以上几个任选一种就可以了,我这里使用了最后一种。
部署自己的构件至Nexus
直接在要部署的项目的pom.xml中写入如下代码:
还需要在settings.xml中设置用户名和密码,因为Nexus的仓库对于匿名用户是readonly的:
至此,有关私服已经设置好了!
在目录src/main/java下编写plugin
在yakov下创建src/main/java目录
写一个YakovMojo的类:
1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileReader;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7 import org.apache.maven.model.Resource;
8 import org.apache.maven.plugin.AbstractMojo;
9 import org.apache.maven.plugin.MojoExecutionException;
10 import org.apache.maven.plugin.MojoFailureException;
11 /**
12 *
13 * @author org.omylab.yakov
14 * @goal yakov
15 */
16 public class YakovMojo extends AbstractMojo{
17 private final String[] INCLUDES_DEFAULT={"java","xml","properties"};
18
19 /**
20 * @parameter expression="${project.basedir}"
21 * @required
22 * @readonly
23 */
24 private File basedir;
25 /**
26 * @parameter expression ="${project.build.sourceDirectory}"
27 * @required
28 * @readonly
29 */
30 private File sourceDirectory;
31 /**
32 * @parameter expression ="${project.biuld.testSourceDirectory}"
33 * @required
34 * @readonly
35 */
36 private File testSourceDirectory;
37 /**
38 * @parameter expression ="${project.build.resources}"
39 * @required
40 * @readonly
41 */
42 private List<Resource> resources;
43 /**
44 * @parameter expression "${project.build.testResources}"
45 * @required
46 * @readonly
47 */
48 private List<Resource> testResources;
49 /**
50 * The file types which will be included for counting
51 *
52 * @parameter
53 */
54 private String[] includes;
55 public void execute() throws MojoExecutionException, MojoFailureException{
56 if(includes==null||includes.length==0){
57 includes=INCLUDES_DEFAULT;
58 }
59 try{
60 countDir(sourceDirectory);
61 countDir(testSourceDirectory);
62 for(Resource resource:resources){
63 countDir(new File(resource.getDirectory()));
64 }
65 for(Resource resource:testResources){
66 countDir(new File(resource.getDirectory()));
67 }
68 }catch(IOException e){
69 throw new MojoExecutionException("Unable to count lines of code.",e);
70 }
71 }
72
73 private void countDir(File dir)throws IOException{
74 if(!dir.exists())return;
75 List<File> collected=new ArrayList<File>();
76 collectFiles(collected,dir);
77 int lines=0;
78 for(File sourceFile:collected){
79 lines+=countLine(sourceFile);
80 }
81 String path=dir.getAbsolutePath().substring(basedir.getAbsolutePath().length());
82 getLog().info(path+" : "+lines+" lines of code in "+collected.size()+" files");
83 }
84
85 private void collectFiles(List<File> collected,File file){
86 if(file.isFile()){
87 for(String include:includes){
88 if(file.getName().endsWith("."+include)){
89 collected.add(file);
90 break;
91 }
92 }
93 }else{
94 for(File sub:file.listFiles()){
95 collectFiles(collected,sub);
96 }
97 }
98 }
99 private int countLine(File file)throws IOException{
100 BufferedReader reader=new BufferedReader(new FileReader(file));
101 int line =0;
102 try{
103 while(reader.ready()){
104 reader.readLine();
105 line++;
106 }
107 }finally{
108 reader.close();
109 }
110 return line;
111 }
112
113 }
然后运行mvn clean compile,运行结果如下:
编译完成,这里可移执行安装了,事实上,还应该有对应的测试代码,以后再讲。
运行mvn clean install完后就安装成功了。
最后运行mvn clean deploy 完成发布,查看Nexus如下:
Note:以上内容是《Maven实战》ISBN:978-7-111-32154-5的学习笔记,推荐购买!
其作者博客:http://www.juvenxu.com